forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardScriptUpdater.ts
More file actions
97 lines (82 loc) · 3.33 KB
/
StandardScriptUpdater.ts
File metadata and controls
97 lines (82 loc) · 3.33 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
// 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 'path';
import { Text, FileSystem } from '@rushstack/node-core-library';
import { RushConfiguration } from '../api/RushConfiguration';
/**
* Checks whether the common/scripts files are up to date, and recopies them if needed.
* This is used by the "rush install" and "rush update" commands.
*/
export class StandardScriptUpdater {
private static readonly _scriptNames: string[] = [
'install-run.js',
'install-run-rush.js',
'install-run-rushx.js'
];
/**
* Recopy the scripts if the scripts are out of date.
* Used by "rush update".
*/
public static update(rushConfiguration: RushConfiguration): boolean {
let anyChanges: boolean = false;
for (const scriptName of StandardScriptUpdater._scriptNames) {
if (StandardScriptUpdater._updateScriptOrThrow(scriptName, rushConfiguration, false)) {
anyChanges = true;
}
}
if (anyChanges) {
console.log(); // print a newline after the notices
}
return anyChanges;
}
/**
* Throw an exception if the scripts are out of date.
* Used by "rush install".
*/
public static validate(rushConfiguration: RushConfiguration): void {
for (const scriptName of StandardScriptUpdater._scriptNames) {
StandardScriptUpdater._updateScriptOrThrow(scriptName, rushConfiguration, true);
}
}
/**
* Compares a single script in the common/script folder to see if it needs to be updated.
* If throwInsteadOfCopy=false, then an outdated or missing script will be recopied;
* otherwise, an exception is thrown.
*/
private static _updateScriptOrThrow(scriptName: string, rushConfiguration: RushConfiguration,
throwInsteadOfCopy: boolean): boolean {
const targetFilePath: string = path.join(rushConfiguration.commonScriptsFolder, scriptName);
const sourceFilePath: string = path.resolve(__dirname, '../scripts', scriptName);
FileSystem.ensureFolder(rushConfiguration.commonScriptsFolder);
// Are the files the same?
let filesAreSame: boolean = false;
if (FileSystem.exists(targetFilePath)) {
const sourceContent: string = FileSystem.readFile(sourceFilePath);
const targetContent: string = FileSystem.readFile(targetFilePath);
const sourceNormalized: string = StandardScriptUpdater._normalize(sourceContent);
const targetNormalized: string = StandardScriptUpdater._normalize(targetContent);
if (sourceNormalized === targetNormalized) {
filesAreSame = true;
}
}
if (!filesAreSame) {
if (throwInsteadOfCopy) {
throw new Error('The standard files in the "common/scripts" folders need to be updated'
+ ' for this Rush version. Please run "rush update" and commit the changes.');
} else {
console.log(`Script is out of date; updating "${targetFilePath}"`);
FileSystem.copyFile({
sourcePath: sourceFilePath,
destinationPath: targetFilePath
});
}
}
return !filesAreSame;
}
private static _normalize(content: string): string {
// Ignore newline differences from .gitattributes
return Text.convertToLf(content)
// Ignore trailing whitespace
.split('\n').map(x => x.trimRight()).join('\n');
}
}