forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.ts
More file actions
173 lines (156 loc) · 5.49 KB
/
executor.ts
File metadata and controls
173 lines (156 loc) · 5.49 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* @license
* Copyright Google LLC 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.dev/license
*/
import { BaseException } from '@angular-devkit/core';
import { SpawnOptions, spawn } from 'node:child_process';
import * as path from 'node:path';
import ora from 'ora';
import { TaskExecutor, UnsuccessfulWorkflowExecution } from '../../src';
import { NodePackageTaskFactoryOptions, NodePackageTaskOptions } from './options';
interface PackageManagerProfile {
commands: {
installAll?: string;
installPackage: string;
};
}
const packageManagers: { [name: string]: PackageManagerProfile } = {
'npm': {
commands: {
installAll: 'install',
installPackage: 'install',
},
},
'yarn': {
commands: {
installAll: 'install',
installPackage: 'add',
},
},
'bun': {
commands: {
installAll: 'install',
installPackage: 'add',
},
},
'pnpm': {
commands: {
installAll: 'install',
installPackage: 'install',
},
},
};
export class UnknownPackageManagerException extends BaseException {
constructor(name: string) {
super(`Unknown package manager "${name}".`);
}
}
export default function (
factoryOptions: NodePackageTaskFactoryOptions = {},
): TaskExecutor<NodePackageTaskOptions> {
const packageManagerName = factoryOptions.packageManager || 'npm';
const packageManagerProfile = packageManagers[packageManagerName];
if (!packageManagerProfile) {
throw new UnknownPackageManagerException(packageManagerName);
}
const rootDirectory = factoryOptions.rootDirectory || process.cwd();
return (options: NodePackageTaskOptions = { command: 'install' }) => {
let taskPackageManagerProfile = packageManagerProfile;
let taskPackageManagerName = packageManagerName;
if (factoryOptions.allowPackageManagerOverride && options.packageManager) {
taskPackageManagerProfile = packageManagers[options.packageManager];
if (!taskPackageManagerProfile) {
throw new UnknownPackageManagerException(options.packageManager);
}
taskPackageManagerName = options.packageManager;
}
const bufferedOutput: { stream: NodeJS.WriteStream; data: Buffer }[] = [];
const spawnOptions: SpawnOptions = {
shell: false,
cwd: path.join(rootDirectory, options.workingDirectory || ''),
};
if (options.hideOutput) {
spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'pipe'] : 'pipe';
} else {
spawnOptions.stdio = options.quiet ? ['ignore', 'ignore', 'inherit'] : 'inherit';
}
const args: string[] = [];
if (options.packageName) {
if (options.command === 'install') {
args.push(taskPackageManagerProfile.commands.installPackage);
}
args.push(options.packageName);
} else if (options.command === 'install' && taskPackageManagerProfile.commands.installAll) {
args.push(taskPackageManagerProfile.commands.installAll);
}
if (!options.allowScripts) {
// Yarn requires special handling since Yarn 2+ no longer has the `--ignore-scripts` flag
if (taskPackageManagerName === 'yarn') {
spawnOptions.env = {
...process.env,
// Supported with yarn 1
'npm_config_ignore_scripts': 'true',
// Supported with yarn 2+
'YARN_ENABLE_SCRIPTS': 'false',
};
} else {
args.push('--ignore-scripts');
}
}
if (factoryOptions.registry) {
args.push('--registry', factoryOptions.registry);
}
if (factoryOptions.force) {
args.push('--force');
}
return new Promise<void>((resolve, reject) => {
const spinner = ora({
text: `Installing packages (${taskPackageManagerName})...`,
// Workaround for https://github.com/sindresorhus/ora/issues/136.
discardStdin: process.platform != 'win32',
}).start();
// SECURITY FIX (CWE-78): never concatenate args as a raw shell string.
// On Windows, package managers are .cmd scripts requiring a shell, but
// instead of shell:true + string concat (injection vector), we invoke
// cmd.exe directly with shell:false and pass each arg as an array element.
// Node.js then controls quoting — metacharacters in args are never
// interpreted by cmd.exe as shell operators.
const isWin32 = process.platform === 'win32';
const childProcess = (
isWin32
? spawn(
'cmd.exe',
['/d', '/s', '/c', taskPackageManagerName, ...args],
{ ...spawnOptions, shell: false },
)
: spawn(taskPackageManagerName, args, { ...spawnOptions, shell: false })
).on(
'close',
(code: number) => {
if (code === 0) {
spinner.succeed('Packages installed successfully.');
spinner.stop();
resolve();
} else {
if (options.hideOutput) {
bufferedOutput.forEach(({ stream, data }) => stream.write(data));
}
spinner.fail('Package install failed, see above.');
reject(new UnsuccessfulWorkflowExecution());
}
},
);
if (options.hideOutput) {
childProcess.stdout?.on('data', (data: Buffer) =>
bufferedOutput.push({ stream: process.stdout, data: data }),
);
childProcess.stderr?.on('data', (data: Buffer) =>
bufferedOutput.push({ stream: process.stderr, data: data }),
);
}
});
};
}