1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const child_process_1 = require("child_process");
|
11 | const fs_1 = require("fs");
|
12 | const os_1 = require("os");
|
13 | const path_1 = require("path");
|
14 | const rimraf = require("rimraf");
|
15 | const schema_1 = require("../lib/config/schema");
|
16 | const color_1 = require("../utilities/color");
|
17 | function installPackage(packageName, logger, packageManager = schema_1.PackageManager.Npm, save = true, extraArgs = [], cwd = process.cwd()) {
|
18 | const packageManagerArgs = getPackageManagerArguments(packageManager);
|
19 | const installArgs = [
|
20 | packageManagerArgs.install,
|
21 | packageName,
|
22 | packageManagerArgs.silent,
|
23 | ];
|
24 | logger.info(color_1.colors.green(`Installing packages for tooling via ${packageManager}.`));
|
25 | if (save === 'devDependencies') {
|
26 | installArgs.push(packageManagerArgs.saveDev);
|
27 | }
|
28 | const { status, stderr, stdout, error } = child_process_1.spawnSync(packageManager, [...installArgs, ...extraArgs], {
|
29 | stdio: 'pipe',
|
30 | shell: true,
|
31 | encoding: 'utf8',
|
32 | cwd,
|
33 | });
|
34 | if (status !== 0) {
|
35 | let errorMessage = ((error && error.message) || stderr || stdout || '').trim();
|
36 | if (errorMessage) {
|
37 | errorMessage += '\n';
|
38 | }
|
39 | throw new Error(errorMessage + `Package install failed${errorMessage ? ', see above' : ''}.`);
|
40 | }
|
41 | logger.info(color_1.colors.green(`Installed packages for tooling via ${packageManager}.`));
|
42 | }
|
43 | exports.installPackage = installPackage;
|
44 | function installTempPackage(packageName, logger, packageManager = schema_1.PackageManager.Npm) {
|
45 | const tempPath = fs_1.mkdtempSync(path_1.join(fs_1.realpathSync(os_1.tmpdir()), 'angular-cli-packages-'));
|
46 |
|
47 | process.on('exit', () => {
|
48 | try {
|
49 | rimraf.sync(tempPath);
|
50 | }
|
51 | catch (_a) { }
|
52 | });
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | fs_1.writeFileSync(path_1.join(tempPath, 'package.json'), JSON.stringify({
|
62 | name: 'temp-cli-install',
|
63 | description: 'temp-cli-install',
|
64 | repository: 'temp-cli-install',
|
65 | license: 'MIT',
|
66 | }));
|
67 |
|
68 | const packageManagerArgs = getPackageManagerArguments(packageManager);
|
69 | const tempNodeModules = path_1.join(tempPath, 'node_modules');
|
70 | const installArgs = [
|
71 | packageManagerArgs.prefix,
|
72 |
|
73 | packageManager === schema_1.PackageManager.Yarn ? tempNodeModules : tempPath,
|
74 | packageManagerArgs.noLockfile,
|
75 | ];
|
76 | installPackage(packageName, logger, packageManager, true, installArgs, tempPath);
|
77 | return tempNodeModules;
|
78 | }
|
79 | exports.installTempPackage = installTempPackage;
|
80 | function runTempPackageBin(packageName, logger, packageManager = schema_1.PackageManager.Npm, args = []) {
|
81 | const tempNodeModulesPath = installTempPackage(packageName, logger, packageManager);
|
82 |
|
83 |
|
84 | const packageNameNoVersion = packageName.substring(0, packageName.lastIndexOf('@'));
|
85 | const pkgLocation = path_1.join(tempNodeModulesPath, packageNameNoVersion);
|
86 | const packageJsonPath = path_1.join(pkgLocation, 'package.json');
|
87 |
|
88 | let binPath;
|
89 | if (fs_1.existsSync(packageJsonPath)) {
|
90 | const content = fs_1.readFileSync(packageJsonPath, 'utf-8');
|
91 | if (content) {
|
92 | const { bin = {} } = JSON.parse(content);
|
93 | const binKeys = Object.keys(bin);
|
94 | if (binKeys.length) {
|
95 | binPath = path_1.resolve(pkgLocation, bin[binKeys[0]]);
|
96 | }
|
97 | }
|
98 | }
|
99 | if (!binPath) {
|
100 | throw new Error(`Cannot locate bin for temporary package: ${packageNameNoVersion}.`);
|
101 | }
|
102 | const argv = [binPath, ...args];
|
103 | const { status, error } = child_process_1.spawnSync('node', argv, {
|
104 | stdio: 'inherit',
|
105 | shell: true,
|
106 | env: {
|
107 | ...process.env,
|
108 | NG_DISABLE_VERSION_CHECK: 'true',
|
109 | NG_CLI_ANALYTICS: 'false',
|
110 | },
|
111 | });
|
112 | if (status === null && error) {
|
113 | throw error;
|
114 | }
|
115 | return status || 0;
|
116 | }
|
117 | exports.runTempPackageBin = runTempPackageBin;
|
118 | function getPackageManagerArguments(packageManager) {
|
119 | return packageManager === schema_1.PackageManager.Yarn
|
120 | ? {
|
121 | silent: '--silent',
|
122 | saveDev: '--dev',
|
123 | install: 'add',
|
124 | prefix: '--modules-folder',
|
125 | noLockfile: '--no-lockfile',
|
126 | }
|
127 | : {
|
128 | silent: '--quiet',
|
129 | saveDev: '--save-dev',
|
130 | install: 'install',
|
131 | prefix: '--prefix',
|
132 | noLockfile: '--no-package-lock',
|
133 | };
|
134 | }
|