-
Notifications
You must be signed in to change notification settings - Fork 12k
/
package-manager.ts
334 lines (292 loc) · 10.2 KB
/
package-manager.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* @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.io/license
*/
import { isJsonObject, json } from '@angular-devkit/core';
import { execSync, spawn } from 'child_process';
import { existsSync, promises as fs, realpathSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
import { satisfies, valid } from 'semver';
import { PackageManager } from '../../lib/config/workspace-schema';
import { AngularWorkspace, getProjectByCwd } from './config';
import { memoize } from './memoize';
import { Spinner } from './spinner';
interface PackageManagerOptions {
saveDev: string;
install: string;
installAll?: string;
prefix: string;
noLockfile: string;
}
export interface PackageManagerUtilsContext {
globalConfiguration: AngularWorkspace;
workspace?: AngularWorkspace;
root: string;
}
export class PackageManagerUtils {
constructor(private readonly context: PackageManagerUtilsContext) {}
/** Get the package manager name. */
get name(): PackageManager {
return this.getName();
}
/** Get the package manager version. */
get version(): string | undefined {
return this.getVersion(this.name);
}
/**
* Checks if the package manager is supported. If not, display a warning.
*/
ensureCompatibility(): void {
if (this.name !== PackageManager.Npm) {
return;
}
try {
const version = valid(this.version);
if (!version) {
return;
}
if (satisfies(version, '>=7 <7.5.6')) {
// eslint-disable-next-line no-console
console.warn(
`npm version ${version} detected.` +
' When using npm 7 with the Angular CLI, npm version 7.5.6 or higher is recommended.',
);
}
} catch {
// npm is not installed.
}
}
/** Install a single package. */
async install(
packageName: string,
save: 'dependencies' | 'devDependencies' | true = true,
extraArgs: string[] = [],
cwd?: string,
): Promise<boolean> {
const packageManagerArgs = this.getArguments();
const installArgs: string[] = [packageManagerArgs.install, packageName];
if (save === 'devDependencies') {
installArgs.push(packageManagerArgs.saveDev);
}
return this.run([...installArgs, ...extraArgs], { cwd, silent: true });
}
/** Install all packages. */
async installAll(extraArgs: string[] = [], cwd?: string): Promise<boolean> {
const packageManagerArgs = this.getArguments();
const installArgs: string[] = [];
if (packageManagerArgs.installAll) {
installArgs.push(packageManagerArgs.installAll);
}
return this.run([...installArgs, ...extraArgs], { cwd, silent: true });
}
/** Install a single package temporary. */
async installTemp(
packageName: string,
extraArgs?: string[],
): Promise<{
success: boolean;
tempNodeModules: string;
}> {
const tempPath = await fs.mkdtemp(join(realpathSync(tmpdir()), 'angular-cli-packages-'));
// clean up temp directory on process exit
process.on('exit', () => {
try {
rmSync(tempPath, { recursive: true, maxRetries: 3 });
} catch {}
});
// NPM will warn when a `package.json` is not found in the install directory
// Example:
// npm WARN enoent ENOENT: no such file or directory, open '/tmp/.ng-temp-packages-84Qi7y/package.json'
// npm WARN .ng-temp-packages-84Qi7y No description
// npm WARN .ng-temp-packages-84Qi7y No repository field.
// npm WARN .ng-temp-packages-84Qi7y No license field.
// While we can use `npm init -y` we will end up needing to update the 'package.json' anyways
// because of missing fields.
await fs.writeFile(
join(tempPath, 'package.json'),
JSON.stringify({
name: 'temp-cli-install',
description: 'temp-cli-install',
repository: 'temp-cli-install',
license: 'MIT',
}),
);
// setup prefix/global modules path
const packageManagerArgs = this.getArguments();
const tempNodeModules = join(tempPath, 'node_modules');
// Yarn will not append 'node_modules' to the path
const prefixPath = this.name === PackageManager.Yarn ? tempNodeModules : tempPath;
const installArgs: string[] = [
...(extraArgs ?? []),
`${packageManagerArgs.prefix}="${prefixPath}"`,
packageManagerArgs.noLockfile,
];
return {
success: await this.install(packageName, true, installArgs, tempPath),
tempNodeModules,
};
}
private getArguments(): PackageManagerOptions {
switch (this.name) {
case PackageManager.Yarn:
return {
saveDev: '--dev',
install: 'add',
prefix: '--modules-folder',
noLockfile: '--no-lockfile',
};
case PackageManager.Pnpm:
return {
saveDev: '--save-dev',
install: 'add',
installAll: 'install',
prefix: '--prefix',
noLockfile: '--no-lockfile',
};
default:
return {
saveDev: '--save-dev',
install: 'install',
installAll: 'install',
prefix: '--prefix',
noLockfile: '--no-package-lock',
};
}
}
private async run(
args: string[],
options: { cwd?: string; silent?: boolean } = {},
): Promise<boolean> {
const { cwd = process.cwd(), silent = false } = options;
const spinner = new Spinner();
spinner.start('Installing packages...');
return new Promise((resolve) => {
const bufferedOutput: { stream: NodeJS.WriteStream; data: Buffer }[] = [];
const childProcess = spawn(this.name, args, {
// Always pipe stderr to allow for failures to be reported
stdio: silent ? ['ignore', 'ignore', 'pipe'] : 'pipe',
shell: true,
cwd,
}).on('close', (code: number) => {
if (code === 0) {
spinner.succeed('Packages successfully installed.');
resolve(true);
} else {
spinner.stop();
bufferedOutput.forEach(({ stream, data }) => stream.write(data));
spinner.fail('Packages installation failed, see above.');
resolve(false);
}
});
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 }),
);
});
}
@memoize
private getVersion(name: PackageManager): string | undefined {
try {
return execSync(`${name} --version`, {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
env: {
...process.env,
// NPM updater notifier will prevents the child process from closing until it timeout after 3 minutes.
NO_UPDATE_NOTIFIER: '1',
NPM_CONFIG_UPDATE_NOTIFIER: 'false',
},
}).trim();
} catch {
return undefined;
}
}
@memoize
private getName(): PackageManager {
const packageManager = this.getConfiguredPackageManager();
if (packageManager) {
return packageManager;
}
const hasNpmLock = this.hasLockfile(PackageManager.Npm);
const hasYarnLock = this.hasLockfile(PackageManager.Yarn);
const hasPnpmLock = this.hasLockfile(PackageManager.Pnpm);
// PERF NOTE: `this.getVersion` spawns the package a the child_process which can take around ~300ms at times.
// Therefore, we should only call this method when needed. IE: don't call `this.getVersion(PackageManager.Pnpm)` unless truly needed.
// The result of this method is not stored in a variable because it's memoized.
if (hasNpmLock) {
// Has NPM lock file.
if (!hasYarnLock && !hasPnpmLock && this.getVersion(PackageManager.Npm)) {
// Only NPM lock file and NPM binary is available.
return PackageManager.Npm;
}
} else {
// No NPM lock file.
if (hasYarnLock && this.getVersion(PackageManager.Yarn)) {
// Yarn lock file and Yarn binary is available.
return PackageManager.Yarn;
} else if (hasPnpmLock && this.getVersion(PackageManager.Pnpm)) {
// PNPM lock file and PNPM binary is available.
return PackageManager.Pnpm;
}
}
if (!this.getVersion(PackageManager.Npm)) {
// Doesn't have NPM installed.
const hasYarn = !!this.getVersion(PackageManager.Yarn);
const hasPnpm = !!this.getVersion(PackageManager.Pnpm);
if (hasYarn && !hasPnpm) {
return PackageManager.Yarn;
} else if (!hasYarn && hasPnpm) {
return PackageManager.Pnpm;
}
}
// TODO: This should eventually inform the user of ambiguous package manager usage.
// Potentially with a prompt to choose and optionally set as the default.
return PackageManager.Npm;
}
private hasLockfile(packageManager: PackageManager): boolean {
let lockfileName: string;
switch (packageManager) {
case PackageManager.Yarn:
lockfileName = 'yarn.lock';
break;
case PackageManager.Pnpm:
lockfileName = 'pnpm-lock.yaml';
break;
case PackageManager.Npm:
default:
lockfileName = 'package-lock.json';
break;
}
return existsSync(join(this.context.root, lockfileName));
}
private getConfiguredPackageManager(): PackageManager | undefined {
const getPackageManager = (source: json.JsonValue | undefined): PackageManager | undefined => {
if (source && isJsonObject(source)) {
const value = source['packageManager'];
if (typeof value === 'string') {
return value as PackageManager;
}
}
return undefined;
};
let result: PackageManager | undefined;
const { workspace: localWorkspace, globalConfiguration: globalWorkspace } = this.context;
if (localWorkspace) {
const project = getProjectByCwd(localWorkspace);
if (project) {
result = getPackageManager(localWorkspace.projects.get(project)?.extensions['cli']);
}
result ??= getPackageManager(localWorkspace.extensions['cli']);
}
if (!result) {
result = getPackageManager(globalWorkspace.extensions['cli']);
}
return result;
}
}