Skip to content

Commit

Permalink
Set up skeleton for "rush-pnpm" command binary
Browse files Browse the repository at this point in the history
  • Loading branch information
octogonz authored and chengcyber committed Jun 7, 2022
1 parent 8dcb67b commit 88cb4cc
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
41 changes: 41 additions & 0 deletions apps/rush-lib/src/cli/RushPnpmCommandLine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import colors from 'colors/safe';

import { RushConfiguration } from '../api/RushConfiguration';
import { NodeJsCompatibility } from '../logic/NodeJsCompatibility';

export interface ILaunchRushPnpmInternalOptions {
isManaged: boolean;
alreadyReportedNodeTooNewError?: boolean;
}

export class RushPnpmCommandLine {
public static launch(launcherVersion: string, options: ILaunchRushPnpmInternalOptions): void {
// Node.js can sometimes accidentally terminate with a zero exit code (e.g. for an uncaught
// promise exception), so we start with the assumption that the exit code is 1
// and set it to 0 only on success.
process.exitCode = 1;

try {
// Are we in a Rush repo?
let rushConfiguration: RushConfiguration | undefined = undefined;
if (RushConfiguration.tryFindRushJsonLocation()) {
rushConfiguration = RushConfiguration.loadFromDefaultLocation({ showVerbose: true });
}

NodeJsCompatibility.warnAboutCompatibilityIssues({
isRushLib: true,
alreadyReportedNodeTooNewError: !!options.alreadyReportedNodeTooNewError,
rushConfiguration
});

console.log('Success');
process.exitCode = 0;

} catch (error) {
console.log(colors.red('Error: ' + error.message));
}
}
}
6 changes: 6 additions & 0 deletions apps/rush-lib/src/start-pnpm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { Rush } from './api/Rush';

Rush.launchRushPnpm(Rush.version, { isManaged: false });
2 changes: 2 additions & 0 deletions apps/rush/bin/rush-pnpm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../lib/start.js')
1 change: 1 addition & 0 deletions apps/rush/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"bin": {
"rush": "./bin/rush",
"rush-pnpm": "./bin/rush-pnpm",
"rushx": "./bin/rushx"
},
"license": "MIT",
Expand Down
31 changes: 24 additions & 7 deletions apps/rush/src/RushCommandSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import colors from 'colors/safe';
import * as path from 'path';
import * as rushLib from '@microsoft/rush-lib';

type CommandName = 'rush' | 'rushx' | undefined;
type CommandName = 'rush' | 'rush-pnpm' | 'rushx' | undefined;

/**
* Both "rush" and "rushx" share the same src/start.ts entry point. This makes it
Expand All @@ -16,9 +16,10 @@ type CommandName = 'rush' | 'rushx' | undefined;
*/
export class RushCommandSelector {
public static failIfNotInvokedAsRush(version: string): void {
if (RushCommandSelector._getCommandName() === 'rushx') {
const commandName: CommandName = RushCommandSelector._getCommandName();
if (commandName !== 'rush' && commandName !== undefined) {
RushCommandSelector._failWithError(
`This repository is using Rush version ${version} which does not support the "rushx" command`
`This repository is using Rush version ${version} which does not support the ${commandName} command`
);
}
}
Expand All @@ -36,7 +37,20 @@ export class RushCommandSelector {
RushCommandSelector._failWithError(`Unable to find the "Rush" entry point in @microsoft/rush-lib`);
}

if (RushCommandSelector._getCommandName() === 'rushx') {
const commandName: CommandName = RushCommandSelector._getCommandName();

if (commandName === 'rush-pnpm') {
if (!Rush.launchRushPnpm) {
RushCommandSelector._failWithError(
`This repository is using Rush version ${Rush.version}` +
` which does not support the "rush-pnpm" command`
);
}
Rush.launchRushPnpm(launcherVersion, {
isManaged: options.isManaged,
alreadyReportedNodeTooNewError: options.alreadyReportedNodeTooNewError
});
} else if (commandName === 'rushx') {
if (!Rush.launchRushX) {
RushCommandSelector._failWithError(
`This repository is using Rush version ${Rush.version}` +
Expand All @@ -60,12 +74,15 @@ export class RushCommandSelector {
// argv[0]: "C:\\Program Files\\nodejs\\node.exe"
// argv[1]: "C:\\Program Files\\nodejs\\node_modules\\@microsoft\\rush\\bin\\rushx"
const basename: string = path.basename(process.argv[1]).toUpperCase();
if (basename === 'RUSHX') {
return 'rushx';
}
if (basename === 'RUSH') {
return 'rush';
}
if (basename === 'RUSH-PNPM') {
return 'rush-pnpm';
}
if (basename === 'RUSHX') {
return 'rushx';
}
}
return undefined;
}
Expand Down
4 changes: 2 additions & 2 deletions apps/rush/src/RushVersionSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export class RushVersionSelector {

if (semver.lt(version, '3.0.20')) {
// In old versions, requiring the entry point invoked the command-line parser immediately,
// so fail if "rushx" was used
// so fail if "rushx" or "rush-pnpm" was used
RushCommandSelector.failIfNotInvokedAsRush(version);
require(path.join(expectedRushPath, 'node_modules', '@microsoft', 'rush', 'lib', 'rush'));
} else if (semver.lt(version, '4.0.0')) {
// In old versions, requiring the entry point invoked the command-line parser immediately,
// so fail if "rushx" was used
// so fail if "rushx" or "rush-pnpm" was used
RushCommandSelector.failIfNotInvokedAsRush(version);
require(path.join(expectedRushPath, 'node_modules', '@microsoft', 'rush', 'lib', 'start'));
} else {
Expand Down
1 change: 1 addition & 0 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ export class RepoStateFile {
// @public
export class Rush {
static launch(launcherVersion: string, arg: ILaunchOptions): void;
static launchRushPnpm(launcherVersion: string, options: ILaunchOptions): void;
static launchRushX(launcherVersion: string, options: ILaunchOptions): void;
static get version(): string;
}
Expand Down
15 changes: 11 additions & 4 deletions libraries/rush-lib/src/api/Rush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RushXCommandLine } from '../cli/RushXCommandLine';
import { CommandLineMigrationAdvisor } from '../cli/CommandLineMigrationAdvisor';
import { EnvironmentVariableNames } from './EnvironmentConfiguration';
import { IBuiltInPluginConfiguration } from '../pluginFramework/PluginLoader/BuiltInPluginLoader';
import { RushPnpmCommandLine } from '../cli/RushPnpmCommandLine';

/**
* Options to pass to the rush "launch" functions.
Expand Down Expand Up @@ -50,8 +51,6 @@ export class Rush {
* Third-party tools should not use this API. Instead, they should execute the "rush" binary
* and start a new Node.js process.
*
* @param launcherVersion - The version of the `@microsoft/rush` wrapper used to call invoke the CLI.
*
* @remarks
* Earlier versions of the rush frontend used a different API contract. In the old contract,
* the second argument was the `isManaged` value of the {@link ILaunchOptions} object.
Expand Down Expand Up @@ -83,8 +82,6 @@ export class Rush {
* This API is used by the `@microsoft/rush` front end to launch the "rushx" command-line.
* Third-party tools should not use this API. Instead, they should execute the "rushx" binary
* and start a new Node.js process.
*
* @param launcherVersion - The version of the `@microsoft/rush` wrapper used to call invoke the CLI.
*/
public static launchRushX(launcherVersion: string, options: ILaunchOptions): void {
options = Rush._normalizeLaunchOptions(options);
Expand All @@ -93,6 +90,16 @@ export class Rush {
RushXCommandLine._launchRushXInternal(launcherVersion, { ...options });
}

/**
* This API is used by the `@microsoft/rush` front end to launch the "rush-pnpm" command-line.
* Third-party tools should not use this API. Instead, they should execute the "rush-pnpm" binary
* and start a new Node.js process.
*/
public static launchRushPnpm(launcherVersion: string, options: ILaunchOptions): void {
Rush._assignRushInvokedFolder();
RushPnpmCommandLine.launch(launcherVersion, { ...options });
}

/**
* The currently executing version of the "rush-lib" library.
* This is the same as the Rush tool version for that release.
Expand Down

0 comments on commit 88cb4cc

Please sign in to comment.