Skip to content

Commit 01f0dae

Browse files
committed
Refinement of installRushOnlyIfNeeded.js
1 parent d3a98b4 commit 01f0dae

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

.vscode/settings.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// Controls after how many characters the editor will wrap to the next line. Setting this to 0 turns on viewport width wrapping
66
// When enabled, will trim trailing whitespace when you save a file.
77
"files.trimTrailingWhitespace": true,
8-
// Auto formats on save
9-
"editor.formatOnSave": true,
108
// Controls if the editor should automatically close brackets after opening them
119
"editor.autoClosingBrackets": false,
1210
// Configure glob patterns for excluding files and folders in searches. Inherits all glob patterns from the file.exclude setting.

common/scripts/InstallRushOnlyIfNeeded.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,40 @@ const fs = require('fs');
1414
const os = require('os');
1515
const path = require('path');
1616

17-
const rushJsonContents = fs.readFileSync(path.join(__dirname, '..', '..', 'rush.json'), 'UTF-8');
18-
const rushJsonMatches = rushJsonContents.match(/\"rushMinimumVersion\"\s*\:\s*\"([0-9\.]+)\"/);
19-
const expectedVersion = rushJsonMatches[1];
2017
const packageName = "@microsoft/rush";
2118

19+
const rushJsonPath = path.join(__dirname, '..', '..', 'rush.json');
20+
let expectedVersion;
21+
try {
22+
const rushJsonContents = fs.readFileSync(rushJsonPath, 'UTF-8');
23+
// Use a regular expression to parse out the rushMinimumVersion value because rush.json supports comments,
24+
// but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script.
25+
const rushJsonMatches = rushJsonContents.match(/\"rushMinimumVersion\"\s*\:\s*\"([0-9\.]+)\"/);
26+
expectedVersion = rushJsonMatches[1];
27+
} catch (e) {
28+
console.error(`Unable to determine the required version of Rush from rush.json (${rushJsonPath}). ` +
29+
'The "rushMinimumVersion" field is either not assigned in rush.json or was specified ' +
30+
'using an unexpected syntax.');
31+
return;
32+
}
33+
2234
let npmPath;
23-
if (process.env.NODIST_PREFIX) {
24-
// Nodist is installed
25-
npmPath = path.join(process.env.NODIST_PREFIX, 'bin', 'npm.exe');
26-
} else if (os.platform() === 'win32') {
27-
// We're on Windows
28-
const whereOutput = child_process.execSync('where npm', { stdio: [] }).toString();
29-
const lines = whereOutput.split(os.EOL).filter((line) => !!line);
30-
npmPath = lines[lines.length - 1];
31-
} else {
32-
// We aren't on Windows - assume we're on 'NIX or Darwin
33-
npmPath = child_process.execSync('which npm', { stdio: [] }).toString();
35+
try {
36+
if (process.env.NODIST_PREFIX) {
37+
// Nodist is installed
38+
npmPath = path.join(process.env.NODIST_PREFIX, 'bin', 'npm.exe');
39+
} else if (os.platform() === 'win32') {
40+
// We're on Windows
41+
const whereOutput = child_process.execSync('where npm', { stdio: [] }).toString();
42+
const lines = whereOutput.split(os.EOL).filter((line) => !!line);
43+
npmPath = lines[lines.length - 1];
44+
} else {
45+
// We aren't on Windows - assume we're on 'NIX or Darwin
46+
npmPath = child_process.execSync('which npm', { stdio: [] }).toString();
47+
}
48+
} catch (e) {
49+
console.error(`Unable to determine the path to the NPM tool: ${e}`);
50+
return;
3451
}
3552

3653
npmPath = npmPath.trim();

0 commit comments

Comments
 (0)