Skip to content

Commit 2701cc2

Browse files
authored
Merge pull request #4293 from william2958/will/expose-command-line
[rush] Expose Rush Command Line Parser via API
2 parents b48674b + 59be5ce commit 2701cc2

File tree

7 files changed

+1577
-0
lines changed

7 files changed

+1577
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rush",
5+
"comment": "Introduce a `RushCommandLine` API that exposes an object representing the skeleton of the Rush command-line.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush"
10+
}

common/reviews/api/rush-lib.api.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { AsyncSeriesHook } from 'tapable';
1212
import { AsyncSeriesWaterfallHook } from 'tapable';
1313
import type { CollatedWriter } from '@rushstack/stream-collator';
1414
import type { CommandLineParameter } from '@rushstack/ts-command-line';
15+
import { CommandLineParameterKind } from '@rushstack/ts-command-line';
1516
import { HookMap } from 'tapable';
1617
import { IPackageJson } from '@rushstack/node-core-library';
1718
import { ITerminal } from '@rushstack/terminal';
@@ -703,6 +704,30 @@ export interface IRushCommand {
703704
readonly actionName: string;
704705
}
705706

707+
// @beta
708+
export interface IRushCommandLineAction {
709+
// (undocumented)
710+
actionName: string;
711+
// (undocumented)
712+
parameters: IRushCommandLineParameter[];
713+
}
714+
715+
// @beta
716+
export interface IRushCommandLineParameter {
717+
readonly description: string;
718+
readonly environmentVariable?: string;
719+
readonly kind: keyof typeof CommandLineParameterKind;
720+
readonly longName: string;
721+
readonly required?: boolean;
722+
readonly shortName?: string;
723+
}
724+
725+
// @beta
726+
export interface IRushCommandLineSpec {
727+
// (undocumented)
728+
actions: IRushCommandLineAction[];
729+
}
730+
706731
// @beta (undocumented)
707732
export interface IRushPlugin {
708733
// (undocumented)
@@ -1057,6 +1082,12 @@ export class Rush {
10571082
static get version(): string;
10581083
}
10591084

1085+
// @beta
1086+
export class RushCommandLine {
1087+
// (undocumented)
1088+
static getCliSpec(rushJsonFolder: string): IRushCommandLineSpec;
1089+
}
1090+
10601091
// @public
10611092
export class RushConfiguration {
10621093
readonly allowMostlyStandardPackageNames: boolean;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { CommandLineParameterKind } from '@rushstack/ts-command-line';
5+
6+
import { RushCommandLineParser } from '../cli/RushCommandLineParser';
7+
8+
/**
9+
* Information about the available parameters associated with a Rush action
10+
*
11+
* @beta
12+
*/
13+
export interface IRushCommandLineParameter {
14+
/**
15+
* The corresponding string representation of CliParameterKind
16+
*/
17+
readonly kind: keyof typeof CommandLineParameterKind;
18+
19+
/**
20+
* The long name of the flag including double dashes, e.g. "--do-something"
21+
*/
22+
readonly longName: string;
23+
24+
/**
25+
* An optional short name for the flag including the dash, e.g. "-d"
26+
*/
27+
readonly shortName?: string;
28+
29+
/**
30+
* Documentation for the parameter that will be shown when invoking the tool with "--help"
31+
*/
32+
readonly description: string;
33+
34+
/**
35+
* If true, then an error occurs if the parameter was not included on the command-line.
36+
*/
37+
readonly required?: boolean;
38+
39+
/**
40+
* If provided, this parameter can also be provided by an environment variable with the specified name.
41+
*/
42+
readonly environmentVariable?: string;
43+
}
44+
45+
/**
46+
* The full spec of an available Rush command line action
47+
*
48+
* @beta
49+
*/
50+
export interface IRushCommandLineAction {
51+
actionName: string;
52+
parameters: IRushCommandLineParameter[];
53+
}
54+
55+
/**
56+
* The full spec of a Rush CLI
57+
*
58+
* @beta
59+
*/
60+
export interface IRushCommandLineSpec {
61+
actions: IRushCommandLineAction[];
62+
}
63+
64+
const _commandLineSpecByWorkspaceFolder: Map<string, IRushCommandLineSpec> = new Map();
65+
66+
/**
67+
* Information about the available CLI commands
68+
*
69+
* @beta
70+
*/
71+
export class RushCommandLine {
72+
public static getCliSpec(rushJsonFolder: string): IRushCommandLineSpec {
73+
let result: IRushCommandLineSpec | undefined = _commandLineSpecByWorkspaceFolder.get(rushJsonFolder);
74+
75+
if (!result) {
76+
const commandLineParser: RushCommandLineParser = new RushCommandLineParser({ cwd: rushJsonFolder });
77+
78+
// extract the set of command line elements from the command line parser
79+
const actions: IRushCommandLineAction[] = [];
80+
for (const { actionName, parameters: rawParameters } of commandLineParser.actions) {
81+
const parameters: IRushCommandLineParameter[] = [];
82+
for (const {
83+
kind: rawKind,
84+
longName,
85+
shortName,
86+
description,
87+
required,
88+
environmentVariable
89+
} of rawParameters) {
90+
parameters.push({
91+
kind: CommandLineParameterKind[rawKind] as keyof typeof CommandLineParameterKind,
92+
longName,
93+
shortName,
94+
description,
95+
required,
96+
environmentVariable
97+
});
98+
}
99+
100+
actions.push({
101+
actionName,
102+
parameters
103+
});
104+
}
105+
106+
result = { actions };
107+
_commandLineSpecByWorkspaceFolder.set(rushJsonFolder, result);
108+
}
109+
110+
return result;
111+
}
112+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import path from 'node:path';
5+
6+
import { RushCommandLine } from '../RushCommandLine';
7+
8+
describe(RushCommandLine.name, () => {
9+
it(`Returns a spec`, async () => {
10+
const spec = RushCommandLine.getCliSpec(path.resolve(__dirname, '../../cli/test/repo/'));
11+
expect(spec).toMatchSnapshot();
12+
});
13+
});

0 commit comments

Comments
 (0)