Created
May 25, 2020 21:16
-
-
Save merelinguist/86e9ecbb443b035724e68fb663988f06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { BaseExecutor, executorArgument, getExecutorArgument } from './executor' | |
| import * as fs from 'fs-extra' | |
| import * as path from 'path' | |
| import spawn from 'cross-spawn' | |
| import { log } from '@blitzjs/server' | |
| interface NpmPackage { | |
| name: string | |
| // defaults to latest published | |
| version?: string | |
| // defaults to false | |
| isDevDep?: boolean | |
| } | |
| export interface AddDependencyExecutor extends BaseExecutor { | |
| packages: executorArgument<NpmPackage[]> | |
| } | |
| export function isAddDependencyExecutor(executor: BaseExecutor): executor is AddDependencyExecutor { | |
| return (executor as AddDependencyExecutor).packages !== undefined | |
| } | |
| async function getPackageManager(): Promise<'yarn' | 'npm'> { | |
| if (fs.existsSync(path.resolve('package-lock.json'))) { | |
| return 'npm' | |
| } | |
| return 'yarn' | |
| } | |
| const installDeps = async (pkgs: NpmPackage[], isDevDep?: boolean) => { | |
| const packageManager = await getPackageManager() | |
| const args: string[] = ['add'] | |
| const pkgNameList = pkgs.map(pkg => pkg.name).join(', ') | |
| log.meta(`Installing ${pkgNameList} ${isDevDep !== false ? 'as dev dependencies' : ''}`) | |
| for (const pkg of pkgs) { | |
| pkg.version ? args.push(`${pkg.name}@${pkg.version}`) : args.push(pkg.name) | |
| log.meta(`Installing ${pkg.name} ${pkg.isDevDep !== false ? 'as a dev dependency' : ''}`) | |
| } | |
| spawn.sync(packageManager, args, { | |
| stdio: ['inherit', 'pipe', 'pipe'], | |
| }) | |
| log.progress(`${pkgs.length} packages installed successfully`) | |
| } | |
| export async function addDependencyExecutor(executor: AddDependencyExecutor, cliArgs: any): Promise<void> { | |
| const packagesToInstall = getExecutorArgument(executor.packages, cliArgs) | |
| const dependencies = packagesToInstall.filter(pkg => !pkg.isDevDep) | |
| const devDependencies = packagesToInstall.filter(pkg => pkg.isDevDep) | |
| await installDeps(dependencies) | |
| await installDeps(devDependencies, true) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment