Skip to content

Commit

Permalink
Merge pull request microsoft#4293 from william2958/will/expose-comman…
Browse files Browse the repository at this point in the history
…d-line

[rush] Expose Rush Command Line Parser via API
  • Loading branch information
iclanton authored Feb 29, 2024
2 parents b48674b + 59be5ce commit 2701cc2
Show file tree
Hide file tree
Showing 7 changed files with 1,577 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Introduce a `RushCommandLine` API that exposes an object representing the skeleton of the Rush command-line.",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
31 changes: 31 additions & 0 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AsyncSeriesHook } from 'tapable';
import { AsyncSeriesWaterfallHook } from 'tapable';
import type { CollatedWriter } from '@rushstack/stream-collator';
import type { CommandLineParameter } from '@rushstack/ts-command-line';
import { CommandLineParameterKind } from '@rushstack/ts-command-line';
import { HookMap } from 'tapable';
import { IPackageJson } from '@rushstack/node-core-library';
import { ITerminal } from '@rushstack/terminal';
Expand Down Expand Up @@ -703,6 +704,30 @@ export interface IRushCommand {
readonly actionName: string;
}

// @beta
export interface IRushCommandLineAction {
// (undocumented)
actionName: string;
// (undocumented)
parameters: IRushCommandLineParameter[];
}

// @beta
export interface IRushCommandLineParameter {
readonly description: string;
readonly environmentVariable?: string;
readonly kind: keyof typeof CommandLineParameterKind;
readonly longName: string;
readonly required?: boolean;
readonly shortName?: string;
}

// @beta
export interface IRushCommandLineSpec {
// (undocumented)
actions: IRushCommandLineAction[];
}

// @beta (undocumented)
export interface IRushPlugin {
// (undocumented)
Expand Down Expand Up @@ -1057,6 +1082,12 @@ export class Rush {
static get version(): string;
}

// @beta
export class RushCommandLine {
// (undocumented)
static getCliSpec(rushJsonFolder: string): IRushCommandLineSpec;
}

// @public
export class RushConfiguration {
readonly allowMostlyStandardPackageNames: boolean;
Expand Down
112 changes: 112 additions & 0 deletions libraries/rush-lib/src/api/RushCommandLine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { CommandLineParameterKind } from '@rushstack/ts-command-line';

import { RushCommandLineParser } from '../cli/RushCommandLineParser';

/**
* Information about the available parameters associated with a Rush action
*
* @beta
*/
export interface IRushCommandLineParameter {
/**
* The corresponding string representation of CliParameterKind
*/
readonly kind: keyof typeof CommandLineParameterKind;

/**
* The long name of the flag including double dashes, e.g. "--do-something"
*/
readonly longName: string;

/**
* An optional short name for the flag including the dash, e.g. "-d"
*/
readonly shortName?: string;

/**
* Documentation for the parameter that will be shown when invoking the tool with "--help"
*/
readonly description: string;

/**
* If true, then an error occurs if the parameter was not included on the command-line.
*/
readonly required?: boolean;

/**
* If provided, this parameter can also be provided by an environment variable with the specified name.
*/
readonly environmentVariable?: string;
}

/**
* The full spec of an available Rush command line action
*
* @beta
*/
export interface IRushCommandLineAction {
actionName: string;
parameters: IRushCommandLineParameter[];
}

/**
* The full spec of a Rush CLI
*
* @beta
*/
export interface IRushCommandLineSpec {
actions: IRushCommandLineAction[];
}

const _commandLineSpecByWorkspaceFolder: Map<string, IRushCommandLineSpec> = new Map();

/**
* Information about the available CLI commands
*
* @beta
*/
export class RushCommandLine {
public static getCliSpec(rushJsonFolder: string): IRushCommandLineSpec {
let result: IRushCommandLineSpec | undefined = _commandLineSpecByWorkspaceFolder.get(rushJsonFolder);

if (!result) {
const commandLineParser: RushCommandLineParser = new RushCommandLineParser({ cwd: rushJsonFolder });

// extract the set of command line elements from the command line parser
const actions: IRushCommandLineAction[] = [];
for (const { actionName, parameters: rawParameters } of commandLineParser.actions) {
const parameters: IRushCommandLineParameter[] = [];
for (const {
kind: rawKind,
longName,
shortName,
description,
required,
environmentVariable
} of rawParameters) {
parameters.push({
kind: CommandLineParameterKind[rawKind] as keyof typeof CommandLineParameterKind,
longName,
shortName,
description,
required,
environmentVariable
});
}

actions.push({
actionName,
parameters
});
}

result = { actions };
_commandLineSpecByWorkspaceFolder.set(rushJsonFolder, result);
}

return result;
}
}
13 changes: 13 additions & 0 deletions libraries/rush-lib/src/api/test/RushCommandLine.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import path from 'node:path';

import { RushCommandLine } from '../RushCommandLine';

describe(RushCommandLine.name, () => {
it(`Returns a spec`, async () => {
const spec = RushCommandLine.getCliSpec(path.resolve(__dirname, '../../cli/test/repo/'));
expect(spec).toMatchSnapshot();
});
});
Loading

0 comments on commit 2701cc2

Please sign in to comment.