microsoft/typespec
Publicmirrored fromhttps://github.com/microsoft/typespecAvailable
common/scripts/install-run.js
645lines · modecode
| 1 | // THIS FILE WAS GENERATED BY A TOOL. ANY MANUAL MODIFICATIONS WILL GET OVERWRITTEN WHENEVER RUSH IS UPGRADED. |
| 2 | // |
| 3 | // This script is intended for usage in an automated build environment where a Node tool may not have |
| 4 | // been preinstalled, or may have an unpredictable version. This script will automatically install the specified |
| 5 | // version of the specified tool (if not already installed), and then pass a command-line to it. |
| 6 | // An example usage would be: |
| 7 | // |
| 8 | // node common/scripts/install-run.js qrcode@1.2.2 qrcode https://rushjs.io |
| 9 | // |
| 10 | // For more information, see: https://rushjs.io/pages/maintainer/setup_new_repo/ |
| 11 | |
| 12 | /******/ (() => { // webpackBootstrap |
| 13 | /******/ "use strict"; |
| 14 | /******/ var __webpack_modules__ = ({ |
| 15 | |
| 16 | /***/ 679877: |
| 17 | /*!************************************************!*\ |
| 18 | !*** ./lib-esnext/utilities/npmrcUtilities.js ***! |
| 19 | \************************************************/ |
| 20 | /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { |
| 21 | |
| 22 | __webpack_require__.r(__webpack_exports__); |
| 23 | /* harmony export */ __webpack_require__.d(__webpack_exports__, { |
| 24 | /* harmony export */ "syncNpmrc": () => (/* binding */ syncNpmrc) |
| 25 | /* harmony export */ }); |
| 26 | /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! fs */ 657147); |
| 27 | /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_0__); |
| 28 | /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! path */ 371017); |
| 29 | /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_1__); |
| 30 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 31 | // See LICENSE in the project root for license information. |
| 32 | // IMPORTANT - do not use any non-built-in libraries in this file |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * As a workaround, copyAndTrimNpmrcFile() copies the .npmrc file to the target folder, and also trims |
| 37 | * unusable lines from the .npmrc file. |
| 38 | * |
| 39 | * Why are we trimming the .npmrc lines? NPM allows environment variables to be specified in |
| 40 | * the .npmrc file to provide different authentication tokens for different registry. |
| 41 | * However, if the environment variable is undefined, it expands to an empty string, which |
| 42 | * produces a valid-looking mapping with an invalid URL that causes an error. Instead, |
| 43 | * we'd prefer to skip that line and continue looking in other places such as the user's |
| 44 | * home directory. |
| 45 | * |
| 46 | * @returns |
| 47 | * The text of the the .npmrc. |
| 48 | */ |
| 49 | function _copyAndTrimNpmrcFile(logger, sourceNpmrcPath, targetNpmrcPath) { |
| 50 | logger.info(`Transforming ${sourceNpmrcPath}`); // Verbose |
| 51 | logger.info(` --> "${targetNpmrcPath}"`); |
| 52 | let npmrcFileLines = fs__WEBPACK_IMPORTED_MODULE_0__.readFileSync(sourceNpmrcPath).toString().split('\n'); |
| 53 | npmrcFileLines = npmrcFileLines.map((line) => (line || '').trim()); |
| 54 | const resultLines = []; |
| 55 | // This finds environment variable tokens that look like "${VAR_NAME}" |
| 56 | const expansionRegExp = /\$\{([^\}]+)\}/g; |
| 57 | // Comment lines start with "#" or ";" |
| 58 | const commentRegExp = /^\s*[#;]/; |
| 59 | // Trim out lines that reference environment variables that aren't defined |
| 60 | for (const line of npmrcFileLines) { |
| 61 | let lineShouldBeTrimmed = false; |
| 62 | // Ignore comment lines |
| 63 | if (!commentRegExp.test(line)) { |
| 64 | const environmentVariables = line.match(expansionRegExp); |
| 65 | if (environmentVariables) { |
| 66 | for (const token of environmentVariables) { |
| 67 | // Remove the leading "${" and the trailing "}" from the token |
| 68 | const environmentVariableName = token.substring(2, token.length - 1); |
| 69 | // Is the environment variable defined? |
| 70 | if (!process.env[environmentVariableName]) { |
| 71 | // No, so trim this line |
| 72 | lineShouldBeTrimmed = true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | if (lineShouldBeTrimmed) { |
| 79 | // Example output: |
| 80 | // "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}" |
| 81 | resultLines.push('; MISSING ENVIRONMENT VARIABLE: ' + line); |
| 82 | } |
| 83 | else { |
| 84 | resultLines.push(line); |
| 85 | } |
| 86 | } |
| 87 | const combinedNpmrc = resultLines.join('\n'); |
| 88 | fs__WEBPACK_IMPORTED_MODULE_0__.writeFileSync(targetNpmrcPath, combinedNpmrc); |
| 89 | return combinedNpmrc; |
| 90 | } |
| 91 | /** |
| 92 | * syncNpmrc() copies the .npmrc file to the target folder, and also trims unusable lines from the .npmrc file. |
| 93 | * If the source .npmrc file not exist, then syncNpmrc() will delete an .npmrc that is found in the target folder. |
| 94 | * |
| 95 | * IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH Utilities._syncNpmrc() |
| 96 | * |
| 97 | * @returns |
| 98 | * The text of the the synced .npmrc, if one exists. If one does not exist, then undefined is returned. |
| 99 | */ |
| 100 | function syncNpmrc(sourceNpmrcFolder, targetNpmrcFolder, useNpmrcPublish, logger = { |
| 101 | info: console.log, |
| 102 | error: console.error |
| 103 | }) { |
| 104 | const sourceNpmrcPath = path__WEBPACK_IMPORTED_MODULE_1__.join(sourceNpmrcFolder, !useNpmrcPublish ? '.npmrc' : '.npmrc-publish'); |
| 105 | const targetNpmrcPath = path__WEBPACK_IMPORTED_MODULE_1__.join(targetNpmrcFolder, '.npmrc'); |
| 106 | try { |
| 107 | if (fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(sourceNpmrcPath)) { |
| 108 | return _copyAndTrimNpmrcFile(logger, sourceNpmrcPath, targetNpmrcPath); |
| 109 | } |
| 110 | else if (fs__WEBPACK_IMPORTED_MODULE_0__.existsSync(targetNpmrcPath)) { |
| 111 | // If the source .npmrc doesn't exist and there is one in the target, delete the one in the target |
| 112 | logger.info(`Deleting ${targetNpmrcPath}`); // Verbose |
| 113 | fs__WEBPACK_IMPORTED_MODULE_0__.unlinkSync(targetNpmrcPath); |
| 114 | } |
| 115 | } |
| 116 | catch (e) { |
| 117 | throw new Error(`Error syncing .npmrc file: ${e}`); |
| 118 | } |
| 119 | } |
| 120 | //# sourceMappingURL=npmrcUtilities.js.map |
| 121 | |
| 122 | /***/ }), |
| 123 | |
| 124 | /***/ 532081: |
| 125 | /*!********************************!*\ |
| 126 | !*** external "child_process" ***! |
| 127 | \********************************/ |
| 128 | /***/ ((module) => { |
| 129 | |
| 130 | module.exports = require("child_process"); |
| 131 | |
| 132 | /***/ }), |
| 133 | |
| 134 | /***/ 657147: |
| 135 | /*!*********************!*\ |
| 136 | !*** external "fs" ***! |
| 137 | \*********************/ |
| 138 | /***/ ((module) => { |
| 139 | |
| 140 | module.exports = require("fs"); |
| 141 | |
| 142 | /***/ }), |
| 143 | |
| 144 | /***/ 822037: |
| 145 | /*!*********************!*\ |
| 146 | !*** external "os" ***! |
| 147 | \*********************/ |
| 148 | /***/ ((module) => { |
| 149 | |
| 150 | module.exports = require("os"); |
| 151 | |
| 152 | /***/ }), |
| 153 | |
| 154 | /***/ 371017: |
| 155 | /*!***********************!*\ |
| 156 | !*** external "path" ***! |
| 157 | \***********************/ |
| 158 | /***/ ((module) => { |
| 159 | |
| 160 | module.exports = require("path"); |
| 161 | |
| 162 | /***/ }) |
| 163 | |
| 164 | /******/ }); |
| 165 | /************************************************************************/ |
| 166 | /******/ // The module cache |
| 167 | /******/ var __webpack_module_cache__ = {}; |
| 168 | /******/ |
| 169 | /******/ // The require function |
| 170 | /******/ function __webpack_require__(moduleId) { |
| 171 | /******/ // Check if module is in cache |
| 172 | /******/ var cachedModule = __webpack_module_cache__[moduleId]; |
| 173 | /******/ if (cachedModule !== undefined) { |
| 174 | /******/ return cachedModule.exports; |
| 175 | /******/ } |
| 176 | /******/ // Create a new module (and put it into the cache) |
| 177 | /******/ var module = __webpack_module_cache__[moduleId] = { |
| 178 | /******/ // no module.id needed |
| 179 | /******/ // no module.loaded needed |
| 180 | /******/ exports: {} |
| 181 | /******/ }; |
| 182 | /******/ |
| 183 | /******/ // Execute the module function |
| 184 | /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); |
| 185 | /******/ |
| 186 | /******/ // Return the exports of the module |
| 187 | /******/ return module.exports; |
| 188 | /******/ } |
| 189 | /******/ |
| 190 | /************************************************************************/ |
| 191 | /******/ /* webpack/runtime/compat get default export */ |
| 192 | /******/ (() => { |
| 193 | /******/ // getDefaultExport function for compatibility with non-harmony modules |
| 194 | /******/ __webpack_require__.n = (module) => { |
| 195 | /******/ var getter = module && module.__esModule ? |
| 196 | /******/ () => (module['default']) : |
| 197 | /******/ () => (module); |
| 198 | /******/ __webpack_require__.d(getter, { a: getter }); |
| 199 | /******/ return getter; |
| 200 | /******/ }; |
| 201 | /******/ })(); |
| 202 | /******/ |
| 203 | /******/ /* webpack/runtime/define property getters */ |
| 204 | /******/ (() => { |
| 205 | /******/ // define getter functions for harmony exports |
| 206 | /******/ __webpack_require__.d = (exports, definition) => { |
| 207 | /******/ for(var key in definition) { |
| 208 | /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { |
| 209 | /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); |
| 210 | /******/ } |
| 211 | /******/ } |
| 212 | /******/ }; |
| 213 | /******/ })(); |
| 214 | /******/ |
| 215 | /******/ /* webpack/runtime/hasOwnProperty shorthand */ |
| 216 | /******/ (() => { |
| 217 | /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) |
| 218 | /******/ })(); |
| 219 | /******/ |
| 220 | /******/ /* webpack/runtime/make namespace object */ |
| 221 | /******/ (() => { |
| 222 | /******/ // define __esModule on exports |
| 223 | /******/ __webpack_require__.r = (exports) => { |
| 224 | /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { |
| 225 | /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); |
| 226 | /******/ } |
| 227 | /******/ Object.defineProperty(exports, '__esModule', { value: true }); |
| 228 | /******/ }; |
| 229 | /******/ })(); |
| 230 | /******/ |
| 231 | /************************************************************************/ |
| 232 | var __webpack_exports__ = {}; |
| 233 | // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. |
| 234 | (() => { |
| 235 | /*!*******************************************!*\ |
| 236 | !*** ./lib-esnext/scripts/install-run.js ***! |
| 237 | \*******************************************/ |
| 238 | __webpack_require__.r(__webpack_exports__); |
| 239 | /* harmony export */ __webpack_require__.d(__webpack_exports__, { |
| 240 | /* harmony export */ "RUSH_JSON_FILENAME": () => (/* binding */ RUSH_JSON_FILENAME), |
| 241 | /* harmony export */ "findRushJsonFolder": () => (/* binding */ findRushJsonFolder), |
| 242 | /* harmony export */ "getNpmPath": () => (/* binding */ getNpmPath), |
| 243 | /* harmony export */ "installAndRun": () => (/* binding */ installAndRun), |
| 244 | /* harmony export */ "runWithErrorAndStatusCode": () => (/* binding */ runWithErrorAndStatusCode) |
| 245 | /* harmony export */ }); |
| 246 | /* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! child_process */ 532081); |
| 247 | /* harmony import */ var child_process__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(child_process__WEBPACK_IMPORTED_MODULE_0__); |
| 248 | /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! fs */ 657147); |
| 249 | /* harmony import */ var fs__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(fs__WEBPACK_IMPORTED_MODULE_1__); |
| 250 | /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! os */ 822037); |
| 251 | /* harmony import */ var os__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(os__WEBPACK_IMPORTED_MODULE_2__); |
| 252 | /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! path */ 371017); |
| 253 | /* harmony import */ var path__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_3__); |
| 254 | /* harmony import */ var _utilities_npmrcUtilities__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../utilities/npmrcUtilities */ 679877); |
| 255 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 256 | // See the @microsoft/rush package's LICENSE file for license information. |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | const RUSH_JSON_FILENAME = 'rush.json'; |
| 263 | const RUSH_TEMP_FOLDER_ENV_VARIABLE_NAME = 'RUSH_TEMP_FOLDER'; |
| 264 | const INSTALL_RUN_LOCKFILE_PATH_VARIABLE = 'INSTALL_RUN_LOCKFILE_PATH'; |
| 265 | const INSTALLED_FLAG_FILENAME = 'installed.flag'; |
| 266 | const NODE_MODULES_FOLDER_NAME = 'node_modules'; |
| 267 | const PACKAGE_JSON_FILENAME = 'package.json'; |
| 268 | /** |
| 269 | * Parse a package specifier (in the form of name\@version) into name and version parts. |
| 270 | */ |
| 271 | function _parsePackageSpecifier(rawPackageSpecifier) { |
| 272 | rawPackageSpecifier = (rawPackageSpecifier || '').trim(); |
| 273 | const separatorIndex = rawPackageSpecifier.lastIndexOf('@'); |
| 274 | let name; |
| 275 | let version = undefined; |
| 276 | if (separatorIndex === 0) { |
| 277 | // The specifier starts with a scope and doesn't have a version specified |
| 278 | name = rawPackageSpecifier; |
| 279 | } |
| 280 | else if (separatorIndex === -1) { |
| 281 | // The specifier doesn't have a version |
| 282 | name = rawPackageSpecifier; |
| 283 | } |
| 284 | else { |
| 285 | name = rawPackageSpecifier.substring(0, separatorIndex); |
| 286 | version = rawPackageSpecifier.substring(separatorIndex + 1); |
| 287 | } |
| 288 | if (!name) { |
| 289 | throw new Error(`Invalid package specifier: ${rawPackageSpecifier}`); |
| 290 | } |
| 291 | return { name, version }; |
| 292 | } |
| 293 | let _npmPath = undefined; |
| 294 | /** |
| 295 | * Get the absolute path to the npm executable |
| 296 | */ |
| 297 | function getNpmPath() { |
| 298 | if (!_npmPath) { |
| 299 | try { |
| 300 | if (os__WEBPACK_IMPORTED_MODULE_2__.platform() === 'win32') { |
| 301 | // We're on Windows |
| 302 | const whereOutput = child_process__WEBPACK_IMPORTED_MODULE_0__.execSync('where npm', { stdio: [] }).toString(); |
| 303 | const lines = whereOutput.split(os__WEBPACK_IMPORTED_MODULE_2__.EOL).filter((line) => !!line); |
| 304 | // take the last result, we are looking for a .cmd command |
| 305 | // see https://github.com/microsoft/rushstack/issues/759 |
| 306 | _npmPath = lines[lines.length - 1]; |
| 307 | } |
| 308 | else { |
| 309 | // We aren't on Windows - assume we're on *NIX or Darwin |
| 310 | _npmPath = child_process__WEBPACK_IMPORTED_MODULE_0__.execSync('command -v npm', { stdio: [] }).toString(); |
| 311 | } |
| 312 | } |
| 313 | catch (e) { |
| 314 | throw new Error(`Unable to determine the path to the NPM tool: ${e}`); |
| 315 | } |
| 316 | _npmPath = _npmPath.trim(); |
| 317 | if (!fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(_npmPath)) { |
| 318 | throw new Error('The NPM executable does not exist'); |
| 319 | } |
| 320 | } |
| 321 | return _npmPath; |
| 322 | } |
| 323 | function _ensureFolder(folderPath) { |
| 324 | if (!fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(folderPath)) { |
| 325 | const parentDir = path__WEBPACK_IMPORTED_MODULE_3__.dirname(folderPath); |
| 326 | _ensureFolder(parentDir); |
| 327 | fs__WEBPACK_IMPORTED_MODULE_1__.mkdirSync(folderPath); |
| 328 | } |
| 329 | } |
| 330 | /** |
| 331 | * Create missing directories under the specified base directory, and return the resolved directory. |
| 332 | * |
| 333 | * Does not support "." or ".." path segments. |
| 334 | * Assumes the baseFolder exists. |
| 335 | */ |
| 336 | function _ensureAndJoinPath(baseFolder, ...pathSegments) { |
| 337 | let joinedPath = baseFolder; |
| 338 | try { |
| 339 | for (let pathSegment of pathSegments) { |
| 340 | pathSegment = pathSegment.replace(/[\\\/]/g, '+'); |
| 341 | joinedPath = path__WEBPACK_IMPORTED_MODULE_3__.join(joinedPath, pathSegment); |
| 342 | if (!fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(joinedPath)) { |
| 343 | fs__WEBPACK_IMPORTED_MODULE_1__.mkdirSync(joinedPath); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | catch (e) { |
| 348 | throw new Error(`Error building local installation folder (${path__WEBPACK_IMPORTED_MODULE_3__.join(baseFolder, ...pathSegments)}): ${e}`); |
| 349 | } |
| 350 | return joinedPath; |
| 351 | } |
| 352 | function _getRushTempFolder(rushCommonFolder) { |
| 353 | const rushTempFolder = process.env[RUSH_TEMP_FOLDER_ENV_VARIABLE_NAME]; |
| 354 | if (rushTempFolder !== undefined) { |
| 355 | _ensureFolder(rushTempFolder); |
| 356 | return rushTempFolder; |
| 357 | } |
| 358 | else { |
| 359 | return _ensureAndJoinPath(rushCommonFolder, 'temp'); |
| 360 | } |
| 361 | } |
| 362 | /** |
| 363 | * Resolve a package specifier to a static version |
| 364 | */ |
| 365 | function _resolvePackageVersion(logger, rushCommonFolder, { name, version }) { |
| 366 | if (!version) { |
| 367 | version = '*'; // If no version is specified, use the latest version |
| 368 | } |
| 369 | if (version.match(/^[a-zA-Z0-9\-\+\.]+$/)) { |
| 370 | // If the version contains only characters that we recognize to be used in static version specifiers, |
| 371 | // pass the version through |
| 372 | return version; |
| 373 | } |
| 374 | else { |
| 375 | // version resolves to |
| 376 | try { |
| 377 | const rushTempFolder = _getRushTempFolder(rushCommonFolder); |
| 378 | const sourceNpmrcFolder = path__WEBPACK_IMPORTED_MODULE_3__.join(rushCommonFolder, 'config', 'rush'); |
| 379 | (0,_utilities_npmrcUtilities__WEBPACK_IMPORTED_MODULE_4__.syncNpmrc)(sourceNpmrcFolder, rushTempFolder, undefined, logger); |
| 380 | const npmPath = getNpmPath(); |
| 381 | // This returns something that looks like: |
| 382 | // @microsoft/rush@3.0.0 '3.0.0' |
| 383 | // @microsoft/rush@3.0.1 '3.0.1' |
| 384 | // ... |
| 385 | // @microsoft/rush@3.0.20 '3.0.20' |
| 386 | // <blank line> |
| 387 | const npmVersionSpawnResult = child_process__WEBPACK_IMPORTED_MODULE_0__.spawnSync(npmPath, ['view', `${name}@${version}`, 'version', '--no-update-notifier'], { |
| 388 | cwd: rushTempFolder, |
| 389 | stdio: [] |
| 390 | }); |
| 391 | if (npmVersionSpawnResult.status !== 0) { |
| 392 | throw new Error(`"npm view" returned error code ${npmVersionSpawnResult.status}`); |
| 393 | } |
| 394 | const npmViewVersionOutput = npmVersionSpawnResult.stdout.toString(); |
| 395 | const versionLines = npmViewVersionOutput.split('\n').filter((line) => !!line); |
| 396 | const latestVersion = versionLines[versionLines.length - 1]; |
| 397 | if (!latestVersion) { |
| 398 | throw new Error('No versions found for the specified version range.'); |
| 399 | } |
| 400 | const versionMatches = latestVersion.match(/^.+\s\'(.+)\'$/); |
| 401 | if (!versionMatches) { |
| 402 | throw new Error(`Invalid npm output ${latestVersion}`); |
| 403 | } |
| 404 | return versionMatches[1]; |
| 405 | } |
| 406 | catch (e) { |
| 407 | throw new Error(`Unable to resolve version ${version} of package ${name}: ${e}`); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | let _rushJsonFolder; |
| 412 | /** |
| 413 | * Find the absolute path to the folder containing rush.json |
| 414 | */ |
| 415 | function findRushJsonFolder() { |
| 416 | if (!_rushJsonFolder) { |
| 417 | let basePath = __dirname; |
| 418 | let tempPath = __dirname; |
| 419 | do { |
| 420 | const testRushJsonPath = path__WEBPACK_IMPORTED_MODULE_3__.join(basePath, RUSH_JSON_FILENAME); |
| 421 | if (fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(testRushJsonPath)) { |
| 422 | _rushJsonFolder = basePath; |
| 423 | break; |
| 424 | } |
| 425 | else { |
| 426 | basePath = tempPath; |
| 427 | } |
| 428 | } while (basePath !== (tempPath = path__WEBPACK_IMPORTED_MODULE_3__.dirname(basePath))); // Exit the loop when we hit the disk root |
| 429 | if (!_rushJsonFolder) { |
| 430 | throw new Error('Unable to find rush.json.'); |
| 431 | } |
| 432 | } |
| 433 | return _rushJsonFolder; |
| 434 | } |
| 435 | /** |
| 436 | * Detects if the package in the specified directory is installed |
| 437 | */ |
| 438 | function _isPackageAlreadyInstalled(packageInstallFolder) { |
| 439 | try { |
| 440 | const flagFilePath = path__WEBPACK_IMPORTED_MODULE_3__.join(packageInstallFolder, INSTALLED_FLAG_FILENAME); |
| 441 | if (!fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(flagFilePath)) { |
| 442 | return false; |
| 443 | } |
| 444 | const fileContents = fs__WEBPACK_IMPORTED_MODULE_1__.readFileSync(flagFilePath).toString(); |
| 445 | return fileContents.trim() === process.version; |
| 446 | } |
| 447 | catch (e) { |
| 448 | return false; |
| 449 | } |
| 450 | } |
| 451 | /** |
| 452 | * Delete a file. Fail silently if it does not exist. |
| 453 | */ |
| 454 | function _deleteFile(file) { |
| 455 | try { |
| 456 | fs__WEBPACK_IMPORTED_MODULE_1__.unlinkSync(file); |
| 457 | } |
| 458 | catch (err) { |
| 459 | if (err.code !== 'ENOENT' && err.code !== 'ENOTDIR') { |
| 460 | throw err; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | /** |
| 465 | * Removes the following files and directories under the specified folder path: |
| 466 | * - installed.flag |
| 467 | * - |
| 468 | * - node_modules |
| 469 | */ |
| 470 | function _cleanInstallFolder(rushTempFolder, packageInstallFolder, lockFilePath) { |
| 471 | try { |
| 472 | const flagFile = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, INSTALLED_FLAG_FILENAME); |
| 473 | _deleteFile(flagFile); |
| 474 | const packageLockFile = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, 'package-lock.json'); |
| 475 | if (lockFilePath) { |
| 476 | fs__WEBPACK_IMPORTED_MODULE_1__.copyFileSync(lockFilePath, packageLockFile); |
| 477 | } |
| 478 | else { |
| 479 | // Not running `npm ci`, so need to cleanup |
| 480 | _deleteFile(packageLockFile); |
| 481 | const nodeModulesFolder = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME); |
| 482 | if (fs__WEBPACK_IMPORTED_MODULE_1__.existsSync(nodeModulesFolder)) { |
| 483 | const rushRecyclerFolder = _ensureAndJoinPath(rushTempFolder, 'rush-recycler'); |
| 484 | fs__WEBPACK_IMPORTED_MODULE_1__.renameSync(nodeModulesFolder, path__WEBPACK_IMPORTED_MODULE_3__.join(rushRecyclerFolder, `install-run-${Date.now().toString()}`)); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | catch (e) { |
| 489 | throw new Error(`Error cleaning the package install folder (${packageInstallFolder}): ${e}`); |
| 490 | } |
| 491 | } |
| 492 | function _createPackageJson(packageInstallFolder, name, version) { |
| 493 | try { |
| 494 | const packageJsonContents = { |
| 495 | name: 'ci-rush', |
| 496 | version: '0.0.0', |
| 497 | dependencies: { |
| 498 | [name]: version |
| 499 | }, |
| 500 | description: "DON'T WARN", |
| 501 | repository: "DON'T WARN", |
| 502 | license: 'MIT' |
| 503 | }; |
| 504 | const packageJsonPath = path__WEBPACK_IMPORTED_MODULE_3__.join(packageInstallFolder, PACKAGE_JSON_FILENAME); |
| 505 | fs__WEBPACK_IMPORTED_MODULE_1__.writeFileSync(packageJsonPath, JSON.stringify(packageJsonContents, undefined, 2)); |
| 506 | } |
| 507 | catch (e) { |
| 508 | throw new Error(`Unable to create package.json: ${e}`); |
| 509 | } |
| 510 | } |
| 511 | /** |
| 512 | * Run "npm install" in the package install folder. |
| 513 | */ |
| 514 | function _installPackage(logger, packageInstallFolder, name, version, command) { |
| 515 | try { |
| 516 | logger.info(`Installing ${name}...`); |
| 517 | const npmPath = getNpmPath(); |
| 518 | const result = child_process__WEBPACK_IMPORTED_MODULE_0__.spawnSync(npmPath, [command], { |
| 519 | stdio: 'inherit', |
| 520 | cwd: packageInstallFolder, |
| 521 | env: process.env |
| 522 | }); |
| 523 | if (result.status !== 0) { |
| 524 | throw new Error(`"npm ${command}" encountered an error`); |
| 525 | } |
| 526 | logger.info(`Successfully installed ${name}@${version}`); |
| 527 | } |
| 528 | catch (e) { |
| 529 | throw new Error(`Unable to install package: ${e}`); |
| 530 | } |
| 531 | } |
| 532 | /** |
| 533 | * Get the ".bin" path for the package. |
| 534 | */ |
| 535 | function _getBinPath(packageInstallFolder, binName) { |
| 536 | const binFolderPath = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME, '.bin'); |
| 537 | const resolvedBinName = os__WEBPACK_IMPORTED_MODULE_2__.platform() === 'win32' ? `${binName}.cmd` : binName; |
| 538 | return path__WEBPACK_IMPORTED_MODULE_3__.resolve(binFolderPath, resolvedBinName); |
| 539 | } |
| 540 | /** |
| 541 | * Write a flag file to the package's install directory, signifying that the install was successful. |
| 542 | */ |
| 543 | function _writeFlagFile(packageInstallFolder) { |
| 544 | try { |
| 545 | const flagFilePath = path__WEBPACK_IMPORTED_MODULE_3__.join(packageInstallFolder, INSTALLED_FLAG_FILENAME); |
| 546 | fs__WEBPACK_IMPORTED_MODULE_1__.writeFileSync(flagFilePath, process.version); |
| 547 | } |
| 548 | catch (e) { |
| 549 | throw new Error(`Unable to create installed.flag file in ${packageInstallFolder}`); |
| 550 | } |
| 551 | } |
| 552 | function installAndRun(logger, packageName, packageVersion, packageBinName, packageBinArgs, lockFilePath = process.env[INSTALL_RUN_LOCKFILE_PATH_VARIABLE]) { |
| 553 | const rushJsonFolder = findRushJsonFolder(); |
| 554 | const rushCommonFolder = path__WEBPACK_IMPORTED_MODULE_3__.join(rushJsonFolder, 'common'); |
| 555 | const rushTempFolder = _getRushTempFolder(rushCommonFolder); |
| 556 | const packageInstallFolder = _ensureAndJoinPath(rushTempFolder, 'install-run', `${packageName}@${packageVersion}`); |
| 557 | if (!_isPackageAlreadyInstalled(packageInstallFolder)) { |
| 558 | // The package isn't already installed |
| 559 | _cleanInstallFolder(rushTempFolder, packageInstallFolder, lockFilePath); |
| 560 | const sourceNpmrcFolder = path__WEBPACK_IMPORTED_MODULE_3__.join(rushCommonFolder, 'config', 'rush'); |
| 561 | (0,_utilities_npmrcUtilities__WEBPACK_IMPORTED_MODULE_4__.syncNpmrc)(sourceNpmrcFolder, packageInstallFolder, undefined, logger); |
| 562 | _createPackageJson(packageInstallFolder, packageName, packageVersion); |
| 563 | const command = lockFilePath ? 'ci' : 'install'; |
| 564 | _installPackage(logger, packageInstallFolder, packageName, packageVersion, command); |
| 565 | _writeFlagFile(packageInstallFolder); |
| 566 | } |
| 567 | const statusMessage = `Invoking "${packageBinName} ${packageBinArgs.join(' ')}"`; |
| 568 | const statusMessageLine = new Array(statusMessage.length + 1).join('-'); |
| 569 | logger.info('\n' + statusMessage + '\n' + statusMessageLine + '\n'); |
| 570 | const binPath = _getBinPath(packageInstallFolder, packageBinName); |
| 571 | const binFolderPath = path__WEBPACK_IMPORTED_MODULE_3__.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME, '.bin'); |
| 572 | // Windows environment variables are case-insensitive. Instead of using SpawnSyncOptions.env, we need to |
| 573 | // assign via the process.env proxy to ensure that we append to the right PATH key. |
| 574 | const originalEnvPath = process.env.PATH || ''; |
| 575 | let result; |
| 576 | try { |
| 577 | // Node.js on Windows can not spawn a file when the path has a space on it |
| 578 | // unless the path gets wrapped in a cmd friendly way and shell mode is used |
| 579 | const shouldUseShell = binPath.includes(' ') && os__WEBPACK_IMPORTED_MODULE_2__.platform() === 'win32'; |
| 580 | const platformBinPath = shouldUseShell ? `"${binPath}"` : binPath; |
| 581 | process.env.PATH = [binFolderPath, originalEnvPath].join(path__WEBPACK_IMPORTED_MODULE_3__.delimiter); |
| 582 | result = child_process__WEBPACK_IMPORTED_MODULE_0__.spawnSync(platformBinPath, packageBinArgs, { |
| 583 | stdio: 'inherit', |
| 584 | windowsVerbatimArguments: false, |
| 585 | shell: shouldUseShell, |
| 586 | cwd: process.cwd(), |
| 587 | env: process.env |
| 588 | }); |
| 589 | } |
| 590 | finally { |
| 591 | process.env.PATH = originalEnvPath; |
| 592 | } |
| 593 | if (result.status !== null) { |
| 594 | return result.status; |
| 595 | } |
| 596 | else { |
| 597 | throw result.error || new Error('An unknown error occurred.'); |
| 598 | } |
| 599 | } |
| 600 | function runWithErrorAndStatusCode(logger, fn) { |
| 601 | process.exitCode = 1; |
| 602 | try { |
| 603 | const exitCode = fn(); |
| 604 | process.exitCode = exitCode; |
| 605 | } |
| 606 | catch (e) { |
| 607 | logger.error('\n\n' + e.toString() + '\n\n'); |
| 608 | } |
| 609 | } |
| 610 | function _run() { |
| 611 | const [nodePath /* Ex: /bin/node */, scriptPath /* /repo/common/scripts/install-run-rush.js */, rawPackageSpecifier /* qrcode@^1.2.0 */, packageBinName /* qrcode */, ...packageBinArgs /* [-f, myproject/lib] */] = process.argv; |
| 612 | if (!nodePath) { |
| 613 | throw new Error('Unexpected exception: could not detect node path'); |
| 614 | } |
| 615 | if (path__WEBPACK_IMPORTED_MODULE_3__.basename(scriptPath).toLowerCase() !== 'install-run.js') { |
| 616 | // If install-run.js wasn't directly invoked, don't execute the rest of this function. Return control |
| 617 | // to the script that (presumably) imported this file |
| 618 | return; |
| 619 | } |
| 620 | if (process.argv.length < 4) { |
| 621 | console.log('Usage: install-run.js <package>@<version> <command> [args...]'); |
| 622 | console.log('Example: install-run.js qrcode@1.2.2 qrcode https://rushjs.io'); |
| 623 | process.exit(1); |
| 624 | } |
| 625 | const logger = { info: console.log, error: console.error }; |
| 626 | runWithErrorAndStatusCode(logger, () => { |
| 627 | const rushJsonFolder = findRushJsonFolder(); |
| 628 | const rushCommonFolder = _ensureAndJoinPath(rushJsonFolder, 'common'); |
| 629 | const packageSpecifier = _parsePackageSpecifier(rawPackageSpecifier); |
| 630 | const name = packageSpecifier.name; |
| 631 | const version = _resolvePackageVersion(logger, rushCommonFolder, packageSpecifier); |
| 632 | if (packageSpecifier.version !== version) { |
| 633 | console.log(`Resolved to ${name}@${version}`); |
| 634 | } |
| 635 | return installAndRun(logger, name, version, packageBinName, packageBinArgs); |
| 636 | }); |
| 637 | } |
| 638 | _run(); |
| 639 | //# sourceMappingURL=install-run.js.map |
| 640 | })(); |
| 641 | |
| 642 | module.exports = __webpack_exports__; |
| 643 | /******/ })() |
| 644 | ; |
| 645 | //# sourceMappingURL=install-run.js.map |