forked from webpack/webpack-cli
/
spawn-child.ts
44 lines (37 loc) · 1.18 KB
/
spawn-child.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import path from 'path';
import fs from 'fs';
import { ExecaSyncReturnValue, sync } from 'execa';
import { getPathToGlobalPackages } from './global-packages-path';
import { utils } from 'webpack-cli';
const { getPackageManager } = utils;
/**
*
* Spawns a new process using the respective package manager
*
* @param {String} pkg - The dependency to be installed
* @param {Boolean} isNew - indicates if it needs to be updated or installed
* @returns {Function} spawn - Installs the package
*/
function spawnWithArg(pkg: string, isNew: boolean): ExecaSyncReturnValue {
const packageManager: string = getPackageManager();
let options = [];
if (packageManager === 'npm') {
options = [isNew ? 'install' : 'update', '-g', pkg];
} else {
options = ['global', isNew ? 'add' : 'upgrade', pkg];
}
return sync(packageManager, options, {
stdio: 'inherit',
});
}
/**
*
* Spawns a new process
*
*/
export function spawnChild(pkg: string): ExecaSyncReturnValue {
const rootPath: string = getPathToGlobalPackages();
const pkgPath: string = path.resolve(rootPath, pkg);
const isNew = !fs.existsSync(pkgPath);
return spawnWithArg(pkg, isNew);
}