-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathInstallHelpers.ts
More file actions
422 lines (365 loc) · 17.5 KB
/
InstallHelpers.ts
File metadata and controls
422 lines (365 loc) · 17.5 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'node:path';
import * as semver from 'semver';
import {
FileConstants,
FileSystem,
type IPackageJson,
JsonFile,
LockFile
} from '@rushstack/node-core-library';
import { Colorize, type ITerminal } from '@rushstack/terminal';
import { LastInstallFlag } from '../../api/LastInstallFlag';
import type { PackageManagerName } from '../../api/packageManager/PackageManager';
import type { RushConfiguration } from '../../api/RushConfiguration';
import type { RushGlobalFolder } from '../../api/RushGlobalFolder';
import { Utilities } from '../../utilities/Utilities';
import type { IConfigurationEnvironment } from '../base/BasePackageManagerOptionsConfiguration';
import type { PnpmOptionsConfiguration } from '../pnpm/PnpmOptionsConfiguration';
import { merge } from '../../utilities/objectUtilities';
import type { Subspace } from '../../api/Subspace';
import { RushConstants } from '../RushConstants';
interface ICommonPackageJson extends IPackageJson {
pnpm?: {
overrides?: typeof PnpmOptionsConfiguration.prototype.globalOverrides;
packageExtensions?: typeof PnpmOptionsConfiguration.prototype.globalPackageExtensions;
peerDependencyRules?: typeof PnpmOptionsConfiguration.prototype.globalPeerDependencyRules;
neverBuiltDependencies?: typeof PnpmOptionsConfiguration.prototype.globalNeverBuiltDependencies;
onlyBuiltDependencies?: typeof PnpmOptionsConfiguration.prototype.globalOnlyBuiltDependencies;
ignoredOptionalDependencies?: typeof PnpmOptionsConfiguration.prototype.globalIgnoredOptionalDependencies;
allowedDeprecatedVersions?: typeof PnpmOptionsConfiguration.prototype.globalAllowedDeprecatedVersions;
patchedDependencies?: typeof PnpmOptionsConfiguration.prototype.globalPatchedDependencies;
minimumReleaseAge?: typeof PnpmOptionsConfiguration.prototype.minimumReleaseAge;
minimumReleaseAgeExclude?: typeof PnpmOptionsConfiguration.prototype.minimumReleaseAgeExclude;
trustPolicy?: typeof PnpmOptionsConfiguration.prototype.trustPolicy;
trustPolicyExclude?: typeof PnpmOptionsConfiguration.prototype.trustPolicyExclude;
trustPolicyIgnoreAfter?: typeof PnpmOptionsConfiguration.prototype.trustPolicyIgnoreAfter;
};
}
export class InstallHelpers {
public static generateCommonPackageJson(
rushConfiguration: RushConfiguration,
subspace: Subspace,
dependencies: Map<string, string> = new Map<string, string>(),
terminal: ITerminal
): void {
const commonPackageJson: ICommonPackageJson = {
dependencies: {},
description: 'Temporary file generated by the Rush tool',
name: 'rush-common',
private: true,
version: '0.0.0'
};
if (rushConfiguration.isPnpm) {
const pnpmOptions: PnpmOptionsConfiguration =
subspace.getPnpmOptions() || rushConfiguration.pnpmOptions;
if (!commonPackageJson.pnpm) {
commonPackageJson.pnpm = {};
}
if (pnpmOptions.globalOverrides) {
commonPackageJson.pnpm.overrides = pnpmOptions.globalOverrides;
}
if (pnpmOptions.globalPackageExtensions) {
commonPackageJson.pnpm.packageExtensions = pnpmOptions.globalPackageExtensions;
}
if (pnpmOptions.globalPeerDependencyRules) {
commonPackageJson.pnpm.peerDependencyRules = pnpmOptions.globalPeerDependencyRules;
}
if (pnpmOptions.globalNeverBuiltDependencies) {
commonPackageJson.pnpm.neverBuiltDependencies = pnpmOptions.globalNeverBuiltDependencies;
}
if (pnpmOptions.globalOnlyBuiltDependencies) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '10.1.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "globalOnlyBuiltDependencies" field in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove this field or upgrade to pnpm 10.1.0 or newer.'
)
);
}
commonPackageJson.pnpm.onlyBuiltDependencies = pnpmOptions.globalOnlyBuiltDependencies;
}
if (pnpmOptions.globalIgnoredOptionalDependencies) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '9.0.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "globalIgnoredOptionalDependencies" field in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove this field or upgrade to pnpm 9.'
)
);
}
commonPackageJson.pnpm.ignoredOptionalDependencies = pnpmOptions.globalIgnoredOptionalDependencies;
}
if (pnpmOptions.globalAllowedDeprecatedVersions) {
commonPackageJson.pnpm.allowedDeprecatedVersions = pnpmOptions.globalAllowedDeprecatedVersions;
}
if (pnpmOptions.globalPatchedDependencies) {
commonPackageJson.pnpm.patchedDependencies = pnpmOptions.globalPatchedDependencies;
}
if (pnpmOptions.minimumReleaseAge !== undefined || pnpmOptions.minimumReleaseAgeExclude) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '10.16.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "minimumReleaseAge" or "minimumReleaseAgeExclude" fields in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove these fields or upgrade to pnpm 10.16.0 or newer.'
)
);
}
if (pnpmOptions.minimumReleaseAge !== undefined) {
commonPackageJson.pnpm.minimumReleaseAge = pnpmOptions.minimumReleaseAge;
}
if (pnpmOptions.minimumReleaseAgeExclude) {
commonPackageJson.pnpm.minimumReleaseAgeExclude = pnpmOptions.minimumReleaseAgeExclude;
}
}
if (pnpmOptions.trustPolicy !== undefined) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '10.21.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "trustPolicy" field in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove this field or upgrade to pnpm 10.21.0 or newer.'
)
);
}
commonPackageJson.pnpm.trustPolicy = pnpmOptions.trustPolicy;
}
if (pnpmOptions.trustPolicyExclude) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '10.22.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "trustPolicyExclude" field in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove this field or upgrade to pnpm 10.22.0 or newer.'
)
);
}
commonPackageJson.pnpm.trustPolicyExclude = pnpmOptions.trustPolicyExclude;
}
if (pnpmOptions.trustPolicyIgnoreAfter !== undefined) {
if (
rushConfiguration.rushConfigurationJson.pnpmVersion !== undefined &&
semver.lt(rushConfiguration.rushConfigurationJson.pnpmVersion, '10.27.0')
) {
terminal.writeWarningLine(
Colorize.yellow(
`Your version of pnpm (${rushConfiguration.rushConfigurationJson.pnpmVersion}) ` +
`doesn't support the "trustPolicyIgnoreAfter" field in ` +
`${rushConfiguration.commonRushConfigFolder}/${RushConstants.pnpmConfigFilename}. ` +
'Remove this field or upgrade to pnpm 10.27.0 or newer.'
)
);
}
commonPackageJson.pnpm.trustPolicyIgnoreAfter = pnpmOptions.trustPolicyIgnoreAfter;
}
if (pnpmOptions.unsupportedPackageJsonSettings) {
merge(commonPackageJson, pnpmOptions.unsupportedPackageJsonSettings);
}
}
// Add any preferred versions to the top of the commonPackageJson
// do this in alphabetical order for simpler debugging
for (const dependency of Array.from(dependencies.keys()).sort()) {
commonPackageJson.dependencies![dependency] = dependencies.get(dependency)!;
}
// Example: "C:\MyRepo\common\temp\package.json"
const commonPackageJsonFilename: string = path.join(
subspace.getSubspaceTempFolderPath(),
FileConstants.PackageJson
);
// Don't update the file timestamp unless the content has changed, since "rush install"
// will consider this timestamp
JsonFile.save(commonPackageJson, commonPackageJsonFilename, { onlyIfChanged: true });
}
public static getPackageManagerEnvironment(
rushConfiguration: RushConfiguration,
options: {
debug?: boolean;
} = {}
): NodeJS.ProcessEnv {
let configurationEnvironment: IConfigurationEnvironment | undefined = undefined;
if (rushConfiguration.packageManager === 'npm') {
if (rushConfiguration.npmOptions && rushConfiguration.npmOptions.environmentVariables) {
configurationEnvironment = rushConfiguration.npmOptions.environmentVariables;
}
} else if (rushConfiguration.isPnpm) {
if (rushConfiguration.pnpmOptions && rushConfiguration.pnpmOptions.environmentVariables) {
configurationEnvironment = rushConfiguration.pnpmOptions.environmentVariables;
}
} else if (rushConfiguration.packageManager === 'yarn') {
if (rushConfiguration.yarnOptions && rushConfiguration.yarnOptions.environmentVariables) {
configurationEnvironment = rushConfiguration.yarnOptions.environmentVariables;
}
}
return InstallHelpers._mergeEnvironmentVariables(process.env, configurationEnvironment, options);
}
/**
* If the "(p)npm-local" symlink hasn't been set up yet, this creates it, installing the
* specified (P)npm version in the user's home directory if needed.
*/
public static async ensureLocalPackageManagerAsync(
rushConfiguration: RushConfiguration,
rushGlobalFolder: RushGlobalFolder,
maxInstallAttempts: number,
restrictConsoleOutput?: boolean
): Promise<void> {
let logIfConsoleOutputIsNotRestricted: (message?: string) => void;
if (restrictConsoleOutput) {
logIfConsoleOutputIsNotRestricted = () => {
/* noop */
};
} else {
logIfConsoleOutputIsNotRestricted = (message?: string) => {
// eslint-disable-next-line no-console
console.log(message);
};
}
// Example: "C:\Users\YourName\.rush"
const rushUserFolder: string = rushGlobalFolder.nodeSpecificPath;
if (!FileSystem.exists(rushUserFolder)) {
logIfConsoleOutputIsNotRestricted('Creating ' + rushUserFolder);
FileSystem.ensureFolder(rushUserFolder);
}
const packageManager: PackageManagerName = rushConfiguration.packageManager;
const packageManagerVersion: string = rushConfiguration.packageManagerToolVersion;
const packageManagerAndVersion: string = `${packageManager}-${packageManagerVersion}`;
// Example: "C:\Users\YourName\.rush\pnpm-1.2.3"
const packageManagerToolFolder: string = path.join(rushUserFolder, packageManagerAndVersion);
const packageManagerMarker: LastInstallFlag = new LastInstallFlag(packageManagerToolFolder, {
node: process.versions.node
});
logIfConsoleOutputIsNotRestricted(`Trying to acquire lock for ${packageManagerAndVersion}`);
const lock: LockFile = await LockFile.acquireAsync(rushUserFolder, packageManagerAndVersion);
logIfConsoleOutputIsNotRestricted(`Acquired lock for ${packageManagerAndVersion}`);
if (!(await packageManagerMarker.isValidAsync()) || lock.dirtyWhenAcquired) {
logIfConsoleOutputIsNotRestricted(
Colorize.bold(`Installing ${packageManager} version ${packageManagerVersion}\n`)
);
// note that this will remove the last-install flag from the directory
await Utilities.installPackageInDirectoryAsync({
directory: packageManagerToolFolder,
packageName: packageManager,
version: rushConfiguration.packageManagerToolVersion,
tempPackageTitle: `${packageManager}-local-install`,
maxInstallAttempts: maxInstallAttempts,
// This is using a local configuration to install a package in a shared global location.
// Generally that's a bad practice, but in this case if we can successfully install
// the package at all, we can reasonably assume it's good for all the repositories.
// In particular, we'll assume that two different NPM registries cannot have two
// different implementations of the same version of the same package.
// This was needed for: https://github.com/microsoft/rushstack/issues/691
commonRushConfigFolder: rushConfiguration.commonRushConfigFolder,
// Only filter npm-incompatible properties when the repo uses pnpm or yarn.
// If the repo uses npm, the .npmrc is already configured for npm, so don't filter.
filterNpmIncompatibleProperties: rushConfiguration.packageManager !== 'npm'
});
logIfConsoleOutputIsNotRestricted(
`Successfully installed ${packageManager} version ${packageManagerVersion}`
);
} else {
logIfConsoleOutputIsNotRestricted(
`Found ${packageManager} version ${packageManagerVersion} in ${packageManagerToolFolder}`
);
}
await packageManagerMarker.createAsync();
// Example: "C:\MyRepo\common\temp"
FileSystem.ensureFolder(rushConfiguration.commonTempFolder);
// Example: "C:\MyRepo\common\temp\pnpm-local"
const localPackageManagerToolFolder: string = path.join(
rushConfiguration.commonTempFolder,
`${packageManager}-local`
);
logIfConsoleOutputIsNotRestricted(`\nSymlinking "${localPackageManagerToolFolder}"`);
logIfConsoleOutputIsNotRestricted(` --> "${packageManagerToolFolder}"`);
// We cannot use FileSystem.exists() to test the existence of a symlink, because it will
// return false for broken symlinks. There is no way to test without catching an exception.
try {
FileSystem.deleteFolder(localPackageManagerToolFolder);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
throw error;
}
}
FileSystem.createSymbolicLinkJunction({
linkTargetPath: packageManagerToolFolder,
newLinkPath: localPackageManagerToolFolder
});
lock.release();
}
// Helper for getPackageManagerEnvironment
private static _mergeEnvironmentVariables(
baseEnv: NodeJS.ProcessEnv,
environmentVariables?: IConfigurationEnvironment,
options: {
debug?: boolean;
} = {}
): NodeJS.ProcessEnv {
const packageManagerEnv: NodeJS.ProcessEnv = baseEnv;
if (environmentVariables) {
// eslint-disable-next-line guard-for-in
for (const envVar in environmentVariables) {
let setEnvironmentVariable: boolean = true;
// eslint-disable-next-line no-console
console.log(`\nProcessing definition for environment variable: ${envVar}`);
if (baseEnv.hasOwnProperty(envVar)) {
setEnvironmentVariable = false;
// eslint-disable-next-line no-console
console.log(`Environment variable already defined:`);
// eslint-disable-next-line no-console
console.log(` Name: ${envVar}`);
// eslint-disable-next-line no-console
console.log(` Existing value: ${baseEnv[envVar]}`);
// eslint-disable-next-line no-console
console.log(
` Value set in ${RushConstants.rushJsonFilename}: ${environmentVariables[envVar].value}`
);
if (environmentVariables[envVar].override) {
setEnvironmentVariable = true;
// eslint-disable-next-line no-console
console.log(
`Overriding the environment variable with the value set in ${RushConstants.rushJsonFilename}.`
);
} else {
// eslint-disable-next-line no-console
console.log(Colorize.yellow(`WARNING: Not overriding the value of the environment variable.`));
}
}
if (setEnvironmentVariable) {
if (options.debug) {
// eslint-disable-next-line no-console
console.log(`Setting environment variable for package manager.`);
// eslint-disable-next-line no-console
console.log(` Name: ${envVar}`);
// eslint-disable-next-line no-console
console.log(` Value: ${environmentVariables[envVar].value}`);
}
packageManagerEnv[envVar] = environmentVariables[envVar].value;
}
}
}
return packageManagerEnv;
}
}