forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request microsoft#4293 from william2958/will/expose-comman…
…d-line [rush] Expose Rush Command Line Parser via API
- Loading branch information
Showing
7 changed files
with
1,577 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@microsoft/rush/will-expose-command-line_2023-08-24-22-36.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.