Skip to content

Commit baa14e7

Browse files
committed
Introducing "installRushOnlyIfNeeded.js" script.
1 parent 759c769 commit baa14e7

2 files changed

Lines changed: 91 additions & 15 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"use strict"
2+
3+
// This script is invoked by the CI build, via a build definition step.
4+
//
5+
// "npm install @microsoft/rush -g" will always delete and recreate the rush
6+
// global folder, even if it is already up to date. This causes a race condition
7+
// when multiple builds are running simultaneously on the same build machine.
8+
//
9+
// As a workaround, this script checks whether Rush is up to date before
10+
// running the command.
11+
12+
const child_process = require('child_process');
13+
const fs = require('fs');
14+
const os = require('os');
15+
const path = require('path');
16+
17+
debugger;
18+
const rushJsonContents = fs.readFileSync(path.join(__dirname, '..', '..', 'rush.json'), 'UTF-8');
19+
const rushJsonMatches = rushJsonContents.match(/\"rushMinimumVersion\"\s*\:\s*\"([0-9\.]+)\"/);
20+
const expectedVersion = rushJsonMatches[1];
21+
const packageName = "@microsoft/rush";
22+
23+
let npmPath;
24+
if (process.env.NODIST_PREFIX) {
25+
// Nodist is installed
26+
npmPath = path.join(process.env.NODIST_PREFIX, 'bin', 'npm.exe');
27+
} else if (os.platform() === 'win32') {
28+
// We're on Windows
29+
const whereOutput = child_process.execSync('where npm', { stdio: [] }).toString();
30+
const lines = whereOutput.split(os.EOL).filter((line) => !!line);
31+
npmPath = lines[lines.length - 1];
32+
} else {
33+
// We aren't on Windows - assume we're on 'NIX or Darwin
34+
npmPath = child_process.execSync('which npm', { stdio: [] }).toString();
35+
}
36+
37+
npmPath = npmPath.trim();
38+
39+
console.log(os.EOL + `NPM executable is "${npmPath}"`);
40+
41+
if (!fs.existsSync(npmPath)) {
42+
console.error('The NPM executable does not exist');
43+
process.exit(1);
44+
}
45+
46+
const buildDirectory = path.join(__dirname, '..', '..');
47+
const rushPath = path.join(buildDirectory, 'common', 'local-rush');
48+
const rushPackagePath = path.join(rushPath, 'package.json');
49+
50+
if (!fs.existsSync(rushPath)) {
51+
fs.mkdirSync(rushPath);
52+
}
53+
54+
if (!fs.existsSync(rushPackagePath)) {
55+
const packageContents = {
56+
"dependencies": {
57+
[packageName]: expectedVersion
58+
}
59+
};
60+
61+
fs.writeFileSync(rushPackagePath, JSON.stringify(packageContents, undefined, 2));
62+
}
63+
64+
// Check for the Rush version
65+
let installedVersion = undefined;
66+
console.log(os.EOL + `Expected Rush version is ${expectedVersion}`);
67+
68+
try {
69+
const output = child_process.execSync(`"${npmPath}" list ${packageName} version`,
70+
{ cwd: rushPath, stdio: ['pipe', 'pipe', 'pipe'] });
71+
const matches = /@microsoft\/rush\@([0-9.]+)/.exec(output);
72+
if (matches && matches.length === 2) {
73+
installedVersion = matches[1];
74+
}
75+
}
76+
catch (error) {
77+
// (this happens if we didn't find the installed package)
78+
}
79+
80+
if (installedVersion) {
81+
console.log(os.EOL + `Installed version is ${installedVersion}`);
82+
} else {
83+
console.log(os.EOL + 'Rush does not appear to be installed');
84+
}
85+
86+
if (installedVersion !== expectedVersion) {
87+
console.log(os.EOL + 'Installing Rush...');
88+
child_process.execSync(`"${npmPath}" install ${packageName}@${expectedVersion}`,
89+
{ cwd: rushPath, stdio: ['ignore', 'ignore', 'ignore'] });
90+
console.log(os.EOL + `Successfully installed Rush ${expectedVersion}`);
91+
}

package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)