Repository navigation
Expand file tree
/
Copy pathci-rush.js
More file actions
161 lines (160 loc) · 6.3 KB
/
Copy pathci-rush.js
File metadata and controls
161 lines (160 loc) · 6.3 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
"use strict";
// This script is invoked by the CI build, via a build definition step.
//
// 'npm install @microsoft/rush -g' will always delete and recreate the rush
// global folder, even if it is already up to date. This causes a race condition
// when multiple builds are running simultaneously on the same build machine.
//
// As a workaround, this script checks whether Rush is up to date before
// running the command.
Object.defineProperty(exports, "__esModule", { value: true });
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
const PACKAGE_NAME = '@microsoft/rush';
const RUSH_JSON_FILENAME = 'rush.json';
let rushJsonDirectory = undefined;
let basePath = __dirname;
let tempPath = __dirname;
do {
const testRushJsonPath = path.join(basePath, RUSH_JSON_FILENAME);
if (fs.existsSync(testRushJsonPath)) {
rushJsonDirectory = basePath;
break;
}
else {
basePath = tempPath;
}
} while (basePath !== (tempPath = path.resolve(basePath, '..'))); // Exit the loop when we hit the disk root
if (!rushJsonDirectory) {
console.error('Unable to find rush.json.');
process.exit(1);
}
let expectedVersion = undefined;
const rushJsonPath = path.join(rushJsonDirectory, RUSH_JSON_FILENAME);
try {
const rushJsonContents = fs.readFileSync(rushJsonPath, 'UTF-8');
// Use a regular expression to parse out the rushVersion value because rush.json supports comments,
// but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script.
const rushJsonMatches = rushJsonContents.match(/\"rushVersion\"\s*\:\s*\"([0-9a-zA-Z.+\-]+)\"/);
expectedVersion = rushJsonMatches[1];
}
catch (e) {
console.error(`Unable to determine the required version of Rush from rush.json (${rushJsonDirectory}). ` +
'The \'rushVersion\' field is either not assigned in rush.json or was specified ' +
'using an unexpected syntax.');
process.exit(1);
}
let npmPath = undefined;
try {
if (os.platform() === 'win32') {
// We're on Windows
const whereOutput = childProcess.execSync('where npm', { stdio: [] }).toString();
const lines = whereOutput.split(os.EOL).filter((line) => !!line);
npmPath = lines[lines.length - 1];
}
else {
// We aren't on Windows - assume we're on *NIX or Darwin
npmPath = childProcess.execSync('which npm', { stdio: [] }).toString();
}
}
catch (e) {
console.error(`Unable to determine the path to the NPM tool: ${e}`);
process.exit(1);
}
npmPath = npmPath.trim();
console.log(os.EOL + `NPM executable is '${npmPath}'`);
if (!fs.existsSync(npmPath)) {
console.error('The NPM executable does not exist');
process.exit(1);
}
const rushPathParts = ['common', 'temp', 'ci-rush'];
let rushPath = rushJsonDirectory;
for (const rushPathPart of rushPathParts) {
rushPath = path.join(rushPath, rushPathPart);
try {
if (!fs.existsSync(rushPath)) {
fs.mkdirSync(rushPath);
}
}
catch (e) {
console.error(`Error building local rush installation directory: ${e}`);
process.exit(1);
}
}
console.log(os.EOL + `Expected Rush version is ${expectedVersion}`);
// Check for the Rush version
let installedVersion = undefined;
let installedVersionValid = false;
try {
const spawnResult = childProcess.spawnSync(npmPath, ['list', PACKAGE_NAME, 'version'], { cwd: rushPath, stdio: ['pipe', 'pipe', 'pipe'] });
const output = spawnResult.output.toString();
const matches = /@microsoft\/rush\@([0-9a-zA-Z.+\-]+)/.exec(output);
// If NPM finds the wrong version in node_modules, that version will be in matches[1].
// But if it's not installed at all, then NPM instead uselessly tells us all about
// the version that we DON'T have ('missing:')
if (matches && matches.length === 2 && !output.match(/missing\:/g)) {
installedVersion = matches[1];
if (spawnResult.status === 0) {
installedVersionValid = true;
}
}
}
catch (error) {
// (this happens if we didn't find the installed package)
}
if (installedVersion) {
console.log(os.EOL + `Installed version is ${installedVersion}`);
}
else {
console.log(os.EOL + 'Rush does not appear to be installed');
}
if (!installedVersionValid || installedVersion !== expectedVersion) {
const npmrcPath = path.join(rushJsonDirectory, 'common', 'config', 'rush', '.npmrc');
const rushNpmrcPath = path.join(rushPath, '.npmrc');
if (fs.existsSync(npmrcPath)) {
try {
let npmrcFileLines = fs.readFileSync(npmrcPath).toString().split('\n');
npmrcFileLines = npmrcFileLines.map((line) => (line || '').trim());
const resultLines = [];
// Trim out lines that reference environment variables that aren't defined
for (const line of npmrcFileLines) {
const environmentVariables = line.match(/\$\{([^\}]+)\}/g);
let lineShouldBeTrimmed = false;
if (environmentVariables) {
for (const environmentVariable of environmentVariables) {
if (!process.env[environmentVariable]) {
lineShouldBeTrimmed = true;
break;
}
}
}
if (!lineShouldBeTrimmed) {
resultLines.push(line);
}
}
fs.writeFileSync(rushNpmrcPath, resultLines.join(os.EOL));
}
catch (e) {
console.error(`Error reading or writing .npmrc file: ${e}`);
process.exit(1);
}
}
const packageContents = {
'name': 'ci-rush',
'version': '0.0.0',
'dependencies': {
[PACKAGE_NAME]: expectedVersion
},
'description': 'DON\'T WARN',
'repository': 'DON\'T WARN',
'license': 'MIT'
};
const rushPackagePath = path.join(rushPath, 'package.json');
fs.writeFileSync(rushPackagePath, JSON.stringify(packageContents, undefined, 2));
console.log(os.EOL + 'Installing Rush...');
childProcess.execSync(`"${npmPath}" install ${PACKAGE_NAME}@${expectedVersion}`, { cwd: rushPath });
console.log(os.EOL + `Successfully installed Rush ${expectedVersion}`);
}
//# sourceMappingURL=ci-rush.js.map