/
npm-install.ts
68 lines (58 loc) · 1.84 KB
/
npm-install.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { logging, terminal } from '@angular-devkit/core';
import { spawn } from 'child_process';
export type NpmInstall = (packageName: string,
logger: logging.Logger,
packageManager: string,
projectRoot: string,
save?: boolean) => Promise<void>;
export default async function (packageName: string,
logger: logging.Logger,
packageManager: string,
projectRoot: string,
save = true) {
const installArgs: string[] = [];
switch (packageManager) {
case 'cnpm':
case 'npm':
installArgs.push('install', '--quiet');
break;
case 'yarn':
installArgs.push('add');
break;
default:
packageManager = 'npm';
installArgs.push('install', '--quiet');
break;
}
logger.info(terminal.green(`Installing packages for tooling via ${packageManager}.`));
if (packageName) {
installArgs.push(packageName);
}
if (!save) {
installArgs.push('--no-save');
}
const installOptions = {
stdio: 'inherit',
shell: true,
};
await new Promise((resolve, reject) => {
spawn(packageManager, installArgs, installOptions)
.on('close', (code: number) => {
if (code === 0) {
logger.info(terminal.green(`Installed packages for tooling via ${packageManager}.`));
resolve();
} else {
const message = 'Package install failed, see above.';
logger.info(terminal.red(message));
reject(message);
}
});
});
}