|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See the @microsoft/rush package's LICENSE file for license information. |
| 3 | + |
| 4 | +// THIS FILE WAS GENERATED BY A TOOL. ANY MANUAL MODIFICATIONS WILL GET OVERWRITTEN WHENEVER RUSH IS UPGRADED. |
| 5 | +// |
| 6 | +// This script is intended for usage in an automated build environment where the Rush command may not have |
| 7 | +// been preinstalled, or may have an unpredictable version. This script will automatically install the version of Rush |
| 8 | +// specified in the rush.json configuration file (if not already installed), and then pass a command-line to it. |
| 9 | +// An example usage would be: |
| 10 | +// |
| 11 | +// node common/scripts/install-run-rushx.js install |
| 12 | +// |
| 13 | +// For more information, see: https://rushjs.io/pages/maintainer/setup_new_repo/ |
| 14 | + |
| 15 | +import * as path from 'path'; |
| 16 | +import * as fs from 'fs'; |
| 17 | + |
| 18 | +import { |
| 19 | + installAndRun, |
| 20 | + findRushJsonFolder, |
| 21 | + RUSH_JSON_FILENAME, |
| 22 | + runWithErrorAndStatusCode |
| 23 | +} from './install-run'; |
| 24 | + |
| 25 | +const PACKAGE_NAME: string = '@microsoft/rush'; |
| 26 | + |
| 27 | +function getRushVersion(): string { |
| 28 | + const rushJsonFolder: string = findRushJsonFolder(); |
| 29 | + const rushJsonPath: string = path.join(rushJsonFolder, RUSH_JSON_FILENAME); |
| 30 | + try { |
| 31 | + const rushJsonContents: string = fs.readFileSync(rushJsonPath, 'utf-8'); |
| 32 | + // Use a regular expression to parse out the rushVersion value because rush.json supports comments, |
| 33 | + // but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script. |
| 34 | + const rushJsonMatches: string[] = rushJsonContents.match(/\"rushVersion\"\s*\:\s*\"([0-9a-zA-Z.+\-]+)\"/)!; |
| 35 | + return rushJsonMatches[1]; |
| 36 | + } catch (e) { |
| 37 | + throw new Error( |
| 38 | + `Unable to determine the required version of Rush from rush.json (${rushJsonFolder}). ` + |
| 39 | + 'The \'rushVersion\' field is either not assigned in rush.json or was specified ' + |
| 40 | + 'using an unexpected syntax.' |
| 41 | + ); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function run(): void { |
| 46 | + const [ |
| 47 | + nodePath, /* Ex: /bin/node */ |
| 48 | + scriptPath, /* /repo/common/scripts/install-run-rushx.js */ |
| 49 | + ...packageBinArgs /* [build, --verbose] */ |
| 50 | + ]: string[] = process.argv; |
| 51 | + |
| 52 | + const scriptName = path.basename(scriptPath); |
| 53 | + const bin = scriptName.toLowerCase() === 'install-run-rushx.js' ? 'rushx' : 'rush'; |
| 54 | + if (!nodePath || !scriptPath) { |
| 55 | + throw new Error('Unexpected exception: could not detect node path or script path'); |
| 56 | + } |
| 57 | + |
| 58 | + if (process.argv.length < 3) { |
| 59 | + console.log(`Usage: ${scriptName} <command> [args...]`); |
| 60 | + console.log(`Example: ${scriptName} build`); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + runWithErrorAndStatusCode(() => { |
| 65 | + const version: string = getRushVersion(); |
| 66 | + console.log(`The rush.json configuration requests Rush version ${version}`); |
| 67 | + |
| 68 | + return installAndRun(PACKAGE_NAME, version, bin, packageBinArgs); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +run(); |
0 commit comments