forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoinstaller.ts
More file actions
281 lines (237 loc) · 10.9 KB
/
Autoinstaller.ts
File metadata and controls
281 lines (237 loc) · 10.9 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
// 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 {
FileSystem,
type IPackageJson,
JsonFile,
LockFile,
NewlineKind,
PackageName,
type IParsedPackageNameOrError
} from '@rushstack/node-core-library';
import { Colorize } from '@rushstack/terminal';
import { AsyncRecycler } from '../utilities/AsyncRecycler';
import { Utilities } from '../utilities/Utilities';
import type { RushConfiguration } from '../api/RushConfiguration';
import { PackageJsonEditor } from '../api/PackageJsonEditor';
import { InstallHelpers } from './installManager/InstallHelpers';
import type { RushGlobalFolder } from '../api/RushGlobalFolder';
import { RushConstants } from './RushConstants';
import { LastInstallFlag } from '../api/LastInstallFlag';
import { RushCommandLineParser } from '../cli/RushCommandLineParser';
import type { PnpmPackageManager } from '../api/packageManager/PnpmPackageManager';
export interface IAutoinstallerOptions {
autoinstallerName: string;
rushConfiguration: RushConfiguration;
rushGlobalFolder: RushGlobalFolder;
restrictConsoleOutput?: boolean;
}
export class Autoinstaller {
public readonly name: string;
private readonly _rushConfiguration: RushConfiguration;
private readonly _rushGlobalFolder: RushGlobalFolder;
private readonly _restrictConsoleOutput: boolean;
public constructor(options: IAutoinstallerOptions) {
this.name = options.autoinstallerName;
this._rushConfiguration = options.rushConfiguration;
this._rushGlobalFolder = options.rushGlobalFolder;
this._restrictConsoleOutput =
options.restrictConsoleOutput ?? RushCommandLineParser.shouldRestrictConsoleOutput();
Autoinstaller.validateName(this.name);
}
// Example: .../common/autoinstallers/my-task
public get folderFullPath(): string {
return path.join(this._rushConfiguration.commonAutoinstallersFolder, this.name);
}
// Example: .../common/autoinstallers/my-task/package-lock.yaml
public get shrinkwrapFilePath(): string {
return path.join(
this._rushConfiguration.commonAutoinstallersFolder,
this.name,
this._rushConfiguration.shrinkwrapFilename
);
}
// Example: .../common/autoinstallers/my-task/package.json
public get packageJsonPath(): string {
return path.join(this._rushConfiguration.commonAutoinstallersFolder, this.name, 'package.json');
}
public static validateName(autoinstallerName: string): void {
const nameOrError: IParsedPackageNameOrError = PackageName.tryParse(autoinstallerName);
if (nameOrError.error) {
throw new Error(`The specified name "${autoinstallerName}" is invalid: ` + nameOrError.error);
}
if (nameOrError.scope) {
throw new Error(`The specified name "${autoinstallerName}" must not contain an NPM scope`);
}
}
public async prepareAsync(): Promise<void> {
const autoinstallerFullPath: string = this.folderFullPath;
if (!FileSystem.exists(autoinstallerFullPath)) {
throw new Error(
`The autoinstaller ${this.name} does not exist, Please run\nrush init-autoinstaller --name ${this.name}\n`
);
}
await InstallHelpers.ensureLocalPackageManagerAsync(
this._rushConfiguration,
this._rushGlobalFolder,
RushConstants.defaultMaxInstallAttempts,
this._restrictConsoleOutput
);
// Example: common/autoinstallers/my-task/package.json
const relativePathForLogs: string = path.relative(
this._rushConfiguration.rushJsonFolder,
autoinstallerFullPath
);
this._logIfConsoleOutputIsNotRestricted(`Acquiring lock for "${relativePathForLogs}" folder...`);
const lock: LockFile = await LockFile.acquireAsync(autoinstallerFullPath, 'autoinstaller');
try {
// Example: .../common/autoinstallers/my-task/.rush/temp
const lastInstallFlagPath: string = path.join(
autoinstallerFullPath,
RushConstants.projectRushFolderName,
'temp'
);
const packageJsonPath: string = path.join(autoinstallerFullPath, 'package.json');
const packageJson: IPackageJson = JsonFile.load(packageJsonPath);
const lastInstallFlag: LastInstallFlag = new LastInstallFlag(lastInstallFlagPath, {
node: process.versions.node,
packageManager: this._rushConfiguration.packageManager,
packageManagerVersion: this._rushConfiguration.packageManagerToolVersion,
packageJson: packageJson,
rushJsonFolder: this._rushConfiguration.rushJsonFolder
});
// Example: ../common/autoinstallers/my-task/node_modules
const nodeModulesFolder: string = `${autoinstallerFullPath}/${RushConstants.nodeModulesFolderName}`;
const flagPath: string = `${nodeModulesFolder}/rush-autoinstaller.flag`;
const isLastInstallFlagDirty: boolean =
!(await lastInstallFlag.isValidAsync()) || !FileSystem.exists(flagPath);
if (isLastInstallFlagDirty || lock.dirtyWhenAcquired) {
if (FileSystem.exists(nodeModulesFolder)) {
this._logIfConsoleOutputIsNotRestricted('Deleting old files from ' + nodeModulesFolder);
const recycler: AsyncRecycler = new AsyncRecycler(
`${this._rushConfiguration.commonTempFolder}/${RushConstants.rushRecyclerFolderName}`
);
recycler.moveFolder(nodeModulesFolder);
await recycler.startDeleteAllAsync();
}
// Copy: .../common/autoinstallers/my-task/.npmrc
Utilities.syncNpmrc({
sourceNpmrcFolder: this._rushConfiguration.commonRushConfigFolder,
targetNpmrcFolder: autoinstallerFullPath,
supportEnvVarFallbackSyntax: this._rushConfiguration.isPnpm
});
this._logIfConsoleOutputIsNotRestricted(
`Installing dependencies under ${autoinstallerFullPath}...\n`
);
await Utilities.executeCommandAsync({
command: this._rushConfiguration.packageManagerToolFilename,
args: ['install', '--frozen-lockfile'],
workingDirectory: autoinstallerFullPath,
keepEnvironment: true
});
// Create file: ../common/autoinstallers/my-task/.rush/temp/last-install.flag
await lastInstallFlag.createAsync();
FileSystem.writeFile(
flagPath,
'If this file is deleted, Rush will assume that the node_modules folder has been cleaned and will reinstall it.'
);
this._logIfConsoleOutputIsNotRestricted('Auto install completed successfully\n');
} else {
this._logIfConsoleOutputIsNotRestricted('Autoinstaller folder is already up to date\n');
}
} finally {
// Ensure the lockfile is released when we are finished.
lock.release();
}
}
public async updateAsync(): Promise<void> {
await InstallHelpers.ensureLocalPackageManagerAsync(
this._rushConfiguration,
this._rushGlobalFolder,
RushConstants.defaultMaxInstallAttempts,
this._restrictConsoleOutput
);
const autoinstallerPackageJsonPath: string = path.join(this.folderFullPath, 'package.json');
if (!(await FileSystem.existsAsync(autoinstallerPackageJsonPath))) {
throw new Error(`The specified autoinstaller path does not exist: ` + autoinstallerPackageJsonPath);
}
this._logIfConsoleOutputIsNotRestricted(
`Updating autoinstaller package: ${autoinstallerPackageJsonPath}`
);
let oldFileContents: string = '';
if (await FileSystem.existsAsync(this.shrinkwrapFilePath)) {
oldFileContents = FileSystem.readFile(this.shrinkwrapFilePath, { convertLineEndings: NewlineKind.Lf });
this._logIfConsoleOutputIsNotRestricted('Deleting ' + this.shrinkwrapFilePath);
await FileSystem.deleteFileAsync(this.shrinkwrapFilePath);
if (this._rushConfiguration.isPnpm) {
// Workaround for https://github.com/pnpm/pnpm/issues/1890
//
// When "rush update-autoinstaller" is run, Rush deletes "common/autoinstallers/my-task/pnpm-lock.yaml"
// so that a new lockfile will be generated. However "pnpm install" by design will try to recover
// "pnpm-lock.yaml" from "my-task/node_modules/.pnpm/lock.yaml", which may prevent a full upgrade.
// Deleting both files ensures that a new lockfile will always be generated.
const pnpmPackageManager: PnpmPackageManager = this._rushConfiguration
.packageManagerWrapper as PnpmPackageManager;
await FileSystem.deleteFileAsync(
path.join(this.folderFullPath, pnpmPackageManager.internalShrinkwrapRelativePath)
);
}
}
// Detect a common mistake where PNPM prints "Already up-to-date" without creating a shrinkwrap file
const packageJsonEditor: PackageJsonEditor = await PackageJsonEditor.loadAsync(this.packageJsonPath);
if (packageJsonEditor.dependencyList.length === 0) {
throw new Error(
'You must add at least one dependency to the autoinstaller package' +
' before invoking this command:\n' +
this.packageJsonPath
);
}
this._logIfConsoleOutputIsNotRestricted();
Utilities.syncNpmrc({
sourceNpmrcFolder: this._rushConfiguration.commonRushConfigFolder,
targetNpmrcFolder: this.folderFullPath,
supportEnvVarFallbackSyntax: this._rushConfiguration.isPnpm
});
await Utilities.executeCommandAsync({
command: this._rushConfiguration.packageManagerToolFilename,
args: ['install'],
workingDirectory: this.folderFullPath,
keepEnvironment: true
});
this._logIfConsoleOutputIsNotRestricted();
if (this._rushConfiguration.packageManager === 'npm') {
this._logIfConsoleOutputIsNotRestricted(Colorize.bold('Running "npm shrinkwrap"...'));
await Utilities.executeCommandAsync({
command: this._rushConfiguration.packageManagerToolFilename,
args: ['shrinkwrap'],
workingDirectory: this.folderFullPath,
keepEnvironment: true
});
this._logIfConsoleOutputIsNotRestricted('"npm shrinkwrap" completed');
this._logIfConsoleOutputIsNotRestricted();
}
if (!(await FileSystem.existsAsync(this.shrinkwrapFilePath))) {
throw new Error(
'The package manager did not create the expected shrinkwrap file: ' + this.shrinkwrapFilePath
);
}
const newFileContents: string = await FileSystem.readFileAsync(this.shrinkwrapFilePath, {
convertLineEndings: NewlineKind.Lf
});
if (oldFileContents !== newFileContents) {
this._logIfConsoleOutputIsNotRestricted(
Colorize.green('The shrinkwrap file has been updated.') + ' Please commit the updated file:'
);
this._logIfConsoleOutputIsNotRestricted(`\n ${this.shrinkwrapFilePath}`);
} else {
this._logIfConsoleOutputIsNotRestricted(Colorize.green('Already up to date.'));
}
}
private _logIfConsoleOutputIsNotRestricted(message?: string): void {
if (!this._restrictConsoleOutput) {
// eslint-disable-next-line no-console
console.log(message ?? '');
}
}
}