UNPKG

4.97 kBJavaScriptView Raw
1"use strict";
2// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
3// See LICENSE in the project root for license information.
4Object.defineProperty(exports, "__esModule", { value: true });
5const path = require("path");
6const semver = require("semver");
7const node_core_library_1 = require("@microsoft/node-core-library");
8const Utilities_1 = require("@microsoft/rush-lib/lib/utilities/Utilities");
9const rush_lib_1 = require("@microsoft/rush-lib");
10const RushCommandSelector_1 = require("./RushCommandSelector");
11const MAX_INSTALL_ATTEMPTS = 3;
12class RushVersionSelector {
13 constructor(currentPackageVersion) {
14 this._rushGlobalFolder = new rush_lib_1._RushGlobalFolder();
15 this._currentPackageVersion = currentPackageVersion;
16 }
17 ensureRushVersionInstalled(version, configuration, executeOptions) {
18 const isLegacyRushVersion = semver.lt(version, '4.0.0');
19 const expectedRushPath = path.join(this._rushGlobalFolder.nodeSpecificPath, `rush-${version}`);
20 const installMarker = new rush_lib_1._LastInstallFlag(expectedRushPath, { node: process.versions.node });
21 let installPromise = Promise.resolve();
22 if (!installMarker.isValid()) {
23 installPromise = installPromise.then(() => {
24 // Need to install Rush
25 console.log(`Rush version ${version} is not currently installed. Installing...`);
26 const resourceName = `rush-${version}`;
27 console.log(`Trying to acquire lock for ${resourceName}`);
28 return node_core_library_1.LockFile.acquire(expectedRushPath, resourceName)
29 .then((lock) => {
30 if (installMarker.isValid()) {
31 console.log('Another process performed the installation.');
32 }
33 else {
34 Utilities_1.Utilities.installPackageInDirectory({
35 directory: expectedRushPath,
36 packageName: isLegacyRushVersion ? '@microsoft/rush' : '@microsoft/rush-lib',
37 version: version,
38 tempPackageTitle: 'rush-local-install',
39 maxInstallAttempts: MAX_INSTALL_ATTEMPTS,
40 // This is using a local configuration to install a package in a shared global location.
41 // Generally that's a bad practice, but in this case if we can successfully install
42 // the package at all, we can reasonably assume it's good for all the repositories.
43 // In particular, we'll assume that two different NPM registries cannot have two
44 // different implementations of the same version of the same package.
45 // This was needed for: https://github.com/microsoft/rushstack/issues/691
46 commonRushConfigFolder: configuration ? configuration.commonRushConfigFolder : undefined,
47 suppressOutput: true
48 });
49 console.log(`Successfully installed Rush version ${version} in ${expectedRushPath}.`);
50 // If we've made it here without exception, write the flag file
51 installMarker.create();
52 lock.release();
53 }
54 });
55 });
56 }
57 return installPromise.then(() => {
58 if (semver.lt(version, '3.0.20')) {
59 // In old versions, requiring the entry point invoked the command-line parser immediately,
60 // so fail if "rushx" was used
61 RushCommandSelector_1.RushCommandSelector.failIfNotInvokedAsRush(version);
62 require(path.join(expectedRushPath, 'node_modules', '@microsoft', 'rush', 'lib', 'rush'));
63 }
64 else if (semver.lt(version, '4.0.0')) {
65 // In old versions, requiring the entry point invoked the command-line parser immediately,
66 // so fail if "rushx" was used
67 RushCommandSelector_1.RushCommandSelector.failIfNotInvokedAsRush(version);
68 require(path.join(expectedRushPath, 'node_modules', '@microsoft', 'rush', 'lib', 'start'));
69 }
70 else {
71 // For newer rush-lib, RushCommandSelector can test whether "rushx" is supported or not
72 // eslint-disable-next-line @typescript-eslint/no-var-requires
73 const rushCliEntrypoint = require(path.join(expectedRushPath, 'node_modules', '@microsoft', 'rush-lib', 'lib', 'index'));
74 RushCommandSelector_1.RushCommandSelector.execute(this._currentPackageVersion, rushCliEntrypoint, executeOptions);
75 }
76 });
77 }
78}
79exports.RushVersionSelector = RushVersionSelector;
80//# sourceMappingURL=RushVersionSelector.js.map
\No newline at end of file