microsoft/typespec
Publicmirrored from https://github.com/microsoft/typespecAvailable
common/scripts/install-run-rush.js
116lines · modecode
| 1 | "use strict"; |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 3 | // See the @microsoft/rush package's LICENSE file for license information. |
| 4 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 5 | if (k2 === undefined) k2 = k; |
| 6 | var desc = Object.getOwnPropertyDescriptor(m, k); |
| 7 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 8 | desc = { enumerable: true, get: function() { return m[k]; } }; |
| 9 | } |
| 10 | Object.defineProperty(o, k2, desc); |
| 11 | }) : (function(o, m, k, k2) { |
| 12 | if (k2 === undefined) k2 = k; |
| 13 | o[k2] = m[k]; |
| 14 | })); |
| 15 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 16 | Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 17 | }) : function(o, v) { |
| 18 | o["default"] = v; |
| 19 | }); |
| 20 | var __importStar = (this && this.__importStar) || function (mod) { |
| 21 | if (mod && mod.__esModule) return mod; |
| 22 | var result = {}; |
| 23 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 24 | __setModuleDefault(result, mod); |
| 25 | return result; |
| 26 | }; |
| 27 | Object.defineProperty(exports, "__esModule", { value: true }); |
| 28 | // THIS FILE WAS GENERATED BY A TOOL. ANY MANUAL MODIFICATIONS WILL GET OVERWRITTEN WHENEVER RUSH IS UPGRADED. |
| 29 | // |
| 30 | // This script is intended for usage in an automated build environment where the Rush command may not have |
| 31 | // been preinstalled, or may have an unpredictable version. This script will automatically install the version of Rush |
| 32 | // specified in the rush.json configuration file (if not already installed), and then pass a command-line to it. |
| 33 | // An example usage would be: |
| 34 | // |
| 35 | // node common/scripts/install-run-rush.js install |
| 36 | // |
| 37 | // For more information, see: https://rushjs.io/pages/maintainer/setup_new_repo/ |
| 38 | const path = __importStar(require("path")); |
| 39 | const fs = __importStar(require("fs")); |
| 40 | const install_run_1 = require("./install-run"); |
| 41 | const PACKAGE_NAME = '@microsoft/rush'; |
| 42 | const RUSH_PREVIEW_VERSION = 'RUSH_PREVIEW_VERSION'; |
| 43 | const INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE = 'INSTALL_RUN_RUSH_LOCKFILE_PATH'; |
| 44 | function _getRushVersion(logger) { |
| 45 | const rushPreviewVersion = process.env[RUSH_PREVIEW_VERSION]; |
| 46 | if (rushPreviewVersion !== undefined) { |
| 47 | logger.info(`Using Rush version from environment variable ${RUSH_PREVIEW_VERSION}=${rushPreviewVersion}`); |
| 48 | return rushPreviewVersion; |
| 49 | } |
| 50 | const rushJsonFolder = (0, install_run_1.findRushJsonFolder)(); |
| 51 | const rushJsonPath = path.join(rushJsonFolder, install_run_1.RUSH_JSON_FILENAME); |
| 52 | try { |
| 53 | const rushJsonContents = fs.readFileSync(rushJsonPath, 'utf-8'); |
| 54 | // Use a regular expression to parse out the rushVersion value because rush.json supports comments, |
| 55 | // but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script. |
| 56 | const rushJsonMatches = rushJsonContents.match(/\"rushVersion\"\s*\:\s*\"([0-9a-zA-Z.+\-]+)\"/); |
| 57 | return rushJsonMatches[1]; |
| 58 | } |
| 59 | catch (e) { |
| 60 | throw new Error(`Unable to determine the required version of Rush from rush.json (${rushJsonFolder}). ` + |
| 61 | "The 'rushVersion' field is either not assigned in rush.json or was specified " + |
| 62 | 'using an unexpected syntax.'); |
| 63 | } |
| 64 | } |
| 65 | function _run() { |
| 66 | const [nodePath /* Ex: /bin/node */, scriptPath /* /repo/common/scripts/install-run-rush.js */, ...packageBinArgs /* [build, --to, myproject] */] = process.argv; |
| 67 | // Detect if this script was directly invoked, or if the install-run-rushx script was invokved to select the |
| 68 | // appropriate binary inside the rush package to run |
| 69 | const scriptName = path.basename(scriptPath); |
| 70 | const bin = scriptName.toLowerCase() === 'install-run-rushx.js' ? 'rushx' : 'rush'; |
| 71 | if (!nodePath || !scriptPath) { |
| 72 | throw new Error('Unexpected exception: could not detect node path or script path'); |
| 73 | } |
| 74 | let commandFound = false; |
| 75 | let logger = { info: console.log, error: console.error }; |
| 76 | for (const arg of packageBinArgs) { |
| 77 | if (arg === '-q' || arg === '--quiet') { |
| 78 | // The -q/--quiet flag is supported by both `rush` and `rushx`, and will suppress |
| 79 | // any normal informational/diagnostic information printed during startup. |
| 80 | // |
| 81 | // To maintain the same user experience, the install-run* scripts pass along this |
| 82 | // flag but also use it to suppress any diagnostic information normally printed |
| 83 | // to stdout. |
| 84 | logger = { |
| 85 | info: () => { }, |
| 86 | error: console.error |
| 87 | }; |
| 88 | } |
| 89 | else if (!arg.startsWith('-') || arg === '-h' || arg === '--help') { |
| 90 | // We either found something that looks like a command (i.e. - doesn't start with a "-"), |
| 91 | // or we found the -h/--help flag, which can be run without a command |
| 92 | commandFound = true; |
| 93 | } |
| 94 | } |
| 95 | if (!commandFound) { |
| 96 | console.log(`Usage: ${scriptName} <command> [args...]`); |
| 97 | if (scriptName === 'install-run-rush.js') { |
| 98 | console.log(`Example: ${scriptName} build --to myproject`); |
| 99 | } |
| 100 | else { |
| 101 | console.log(`Example: ${scriptName} custom-command`); |
| 102 | } |
| 103 | process.exit(1); |
| 104 | } |
| 105 | (0, install_run_1.runWithErrorAndStatusCode)(logger, () => { |
| 106 | const version = _getRushVersion(logger); |
| 107 | logger.info(`The rush.json configuration requests Rush version ${version}`); |
| 108 | const lockFilePath = process.env[INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE]; |
| 109 | if (lockFilePath) { |
| 110 | logger.info(`Found ${INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE}="${lockFilePath}", installing with lockfile.`); |
| 111 | } |
| 112 | return (0, install_run_1.installAndRun)(logger, PACKAGE_NAME, version, bin, packageBinArgs, lockFilePath); |
| 113 | }); |
| 114 | } |
| 115 | _run(); |
| 116 | //# sourceMappingURL=install-run-rush.js.map |