// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. /* eslint-disable no-console */ import * as path from 'node:path'; import * as fs from 'node:fs'; import type { ILogger } from '../utilities/npmrcUtilities'; import { createInstallRunRushBootstrap, type IInstallRunRushBootstrap } from './InstallRunRushBootstrap'; import { BOOTSTRAP_PROTOCOL_MAJOR, encodeBootstrapEnvelope } from './generated/BootstrapProtocol'; const { installAndRun, findRushJsonFolder, RUSH_JSON_FILENAME }: typeof import('./install-run') = __non_webpack_require__('./install-run'); declare const RUSH_LIB_VERSION_FOR_BOOTSTRAP: string; const PACKAGE_NAME: string = '@microsoft/rush'; const RUSH_PREVIEW_VERSION: string = 'RUSH_PREVIEW_VERSION'; const RUSH_QUIET_MODE: string = 'RUSH_QUIET_MODE'; const INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE: 'INSTALL_RUN_RUSH_LOCKFILE_PATH' = 'INSTALL_RUN_RUSH_LOCKFILE_PATH'; function _validateBundledBootstrapProtocol(): void { if (BOOTSTRAP_PROTOCOL_MAJOR < 1 || typeof encodeBootstrapEnvelope !== 'function') { throw new Error('The bundled Rush reporter bootstrap protocol is invalid.'); } } function _getRushVersion(): { readonly version: string; readonly sourceMessage?: string } { const rushPreviewVersion: string | undefined = process.env[RUSH_PREVIEW_VERSION]; if (rushPreviewVersion !== undefined) { return { version: rushPreviewVersion, sourceMessage: `Using Rush version from environment variable ${RUSH_PREVIEW_VERSION}=${rushPreviewVersion}` }; } const rushJsonFolder: string = findRushJsonFolder(); const rushJsonPath: string = path.join(rushJsonFolder, RUSH_JSON_FILENAME); try { const rushJsonContents: string = fs.readFileSync(rushJsonPath, 'utf-8'); // Use a regular expression to parse out the rushVersion value because rush.json supports comments, // but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script. const rushJsonMatches: string[] = rushJsonContents.match( /\"rushVersion\"\s*\:\s*\"([0-9a-zA-Z.+\-]+)\"/ )!; return { version: rushJsonMatches[1] }; } catch (e) { throw new Error( `Unable to determine the required version of Rush from ${RUSH_JSON_FILENAME} (${rushJsonFolder}). ` + `The 'rushVersion' field is either not assigned in ${RUSH_JSON_FILENAME} or was specified ` + 'using an unexpected syntax.' ); } } function _getBin(scriptName: string): 'rush' | 'rush-pnpm' | 'rushx' { switch (scriptName.toLowerCase()) { case 'install-run-rush-pnpm.js': return 'rush-pnpm'; case 'install-run-rushx.js': return 'rushx'; default: return 'rush'; } } function _run(): void { const [ nodePath /* Ex: /bin/node */, scriptPath /* /repo/common/scripts/install-run-rush.js */, ...packageBinArgs /* [build, --to, myproject] */ ]: string[] = process.argv; // Detect if this script was directly invoked, or if the install-run-rushx script was invokved to select the // appropriate binary inside the rush package to run const scriptName: string = path.basename(scriptPath); const bin: 'rush' | 'rush-pnpm' | 'rushx' = _getBin(scriptName); if (!nodePath || !scriptPath) { throw new Error('Unexpected exception: could not detect node path or script path'); } let commandFound: boolean = false; const quietModeEnvValue: string | undefined = process.env[RUSH_QUIET_MODE]; let quiet: boolean = quietModeEnvValue === '1' || quietModeEnvValue === 'true'; for (const arg of packageBinArgs) { if (arg === '--') { break; } else if (arg === '-q' || arg === '--quiet') { // The -q/--quiet flag is supported by both `rush` and `rushx`, and will suppress // any normal informational/diagnostic information printed during startup. // // To maintain the same user experience, the install-run* scripts pass along this // flag but also use it to suppress any diagnostic information normally printed // to stdout. quiet = true; } else if (!arg.startsWith('-') || arg === '-h' || arg === '--help') { // We either found something that looks like a command (i.e. - doesn't start with a "-"), // or we found the -h/--help flag, which can be run without a command commandFound = true; } } if (!commandFound) { console.log(`Usage: ${scriptName} [args...]`); if (scriptName === 'install-run-rush-pnpm.js') { console.log(`Example: ${scriptName} pnpm-command`); } else if (scriptName === 'install-run-rush.js') { console.log(`Example: ${scriptName} build --to myproject`); } else { console.log(`Example: ${scriptName} custom-command`); } process.exit(1); } let bootstrap: IInstallRunRushBootstrap | undefined; process.exitCode = 1; try { _validateBundledBootstrapProtocol(); const rushJsonFolder: string = findRushJsonFolder(); const rushVersion: { readonly version: string; readonly sourceMessage?: string } = _getRushVersion(); bootstrap = createInstallRunRushBootstrap({ argv: packageBinArgs, env: process.env, rushJsonFolder, rushVersion: rushVersion.version, bootstrapVersion: RUSH_LIB_VERSION_FOR_BOOTSTRAP, commandName: bin, quiet }); const logger: ILogger = bootstrap.logger; if (rushVersion.sourceMessage) { logger.info(rushVersion.sourceMessage); } logger.info(`The ${RUSH_JSON_FILENAME} configuration requests Rush version ${rushVersion.version}`); const lockFilePath: string | undefined = process.env[INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE]; if (lockFilePath) { logger.info( `Found ${INSTALL_RUN_RUSH_LOCKFILE_PATH_VARIABLE}="${lockFilePath}", installing with lockfile.`, 'local-sensitive' ); } process.exitCode = installAndRun( logger, PACKAGE_NAME, rushVersion.version, bin, packageBinArgs, lockFilePath, { onExternalOutput: bootstrap.externalOutputHandler, onExternalOutputOverflow: bootstrap.externalOutputOverflowHandler, externalOutputCaptureMaxBytes: bootstrap.externalOutputCaptureMaxBytes, externalOutputLiveStreams: bootstrap.externalOutputLiveStreams, prepareToRun: bootstrap.prepareToRun } ); } catch (error) { const logger: ILogger = bootstrap?.logger ?? (quiet ? { info: () => {}, error: (text: string) => console.error(text) } : { info: (text: string) => console.log(text), error: (text: string) => console.error(text) }); logger.error(`\n\n${String(error)}\n`); } } _run();