Skip to content

Commit c70d3c3

Browse files
committed
Set up GlobalScriptAction.ts
1 parent 62ed714 commit c70d3c3

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

apps/rush-lib/src/cli/RushCommandLineParser.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { ScanAction } from './actions/ScanAction';
2525
import { VersionAction } from './actions/VersionAction';
2626

2727
import { BulkScriptAction } from './scriptActions/BulkScriptAction';
28+
import { GlobalScriptAction } from './scriptActions/GlobalScriptAction';
2829

2930
import { Telemetry } from '../logic/Telemetry';
3031
import { AlreadyReportedError } from '../utilities/AlreadyReportedError';
@@ -180,7 +181,16 @@ export class RushCommandLineParser extends CommandLineParser {
180181
}));
181182
break;
182183
case 'global':
183-
// todo
184+
this.addAction(new GlobalScriptAction({
185+
actionName: command.name,
186+
summary: command.summary,
187+
documentation: command.description || command.summary,
188+
189+
parser: this,
190+
commandLineConfiguration: commandLineConfiguration,
191+
192+
scriptPath: command.scriptPath
193+
}));
184194
break;
185195
default:
186196
throw new Error(`${RushConstants.commandLineFilename} defines a command "${command!.name}"`

apps/rush-lib/src/cli/scriptActions/BaseScriptAction.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ export interface IBaseScriptActionOptions extends IBaseRushActionOptions {
1616

1717
/**
1818
* Base class for command-line actions that are implemented using user-defined scripts.
19-
* These actions and their parameters can be defined via common/config/command-line.json.
19+
*
20+
* @remarks
21+
* Compared to the normal built-in actions, these actions are special because (1) they
22+
* can be discovered dynamically via from common/config/command-line.json, and (2)
23+
* user-defined command-line parameters can be passed through to the script.
24+
*
25+
* The two subclasses are BulkScriptAction and GlobalScriptAction.
2026
*/
2127
export abstract class BaseScriptAction extends BaseRushAction {
2228
protected readonly _commandLineConfiguration: CommandLineConfiguration;

apps/rush-lib/src/cli/scriptActions/BulkScriptAction.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,23 @@ import { Stopwatch } from '../../utilities/Stopwatch';
2222
import { AlreadyReportedError } from '../../utilities/AlreadyReportedError';
2323
import { BaseScriptAction, IBaseScriptActionOptions } from './BaseScriptAction';
2424

25+
/**
26+
* Constructor parameters for BulkScriptAction.
27+
*/
2528
export interface IBulkScriptActionOptions extends IBaseScriptActionOptions {
2629
enableParallelism: boolean;
2730
ignoreMissingScript: boolean;
2831
}
2932

33+
/**
34+
* This class implements bulk commands which are run individually for each project in the repo,
35+
* possibly in parallel. The action executes a script found in the project's package.json file.
36+
*
37+
* @remarks
38+
* Bulk commands can be defined via common/config/command-line.json. Rush's predefined "build"
39+
* and "rebuild" commands are also modeled as bulk commands, because they essentially just
40+
* execute scripts from package.json in the same as any custom command.
41+
*/
3042
export class BulkScriptAction extends BaseScriptAction {
3143
private _enableParallelism: boolean;
3244
private _ignoreMissingScript: boolean;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 * as fsx from 'fs-extra';
5+
import * as os from 'os';
6+
7+
import { BaseScriptAction, IBaseScriptActionOptions } from './BaseScriptAction';
8+
9+
/**
10+
* Constructor parameters for GlobalScriptAction.
11+
*/
12+
export interface IGlobalScriptActionOptions extends IBaseScriptActionOptions {
13+
scriptPath: string;
14+
}
15+
16+
/**
17+
* This class implements custom commands that are run once globally for the entire repo
18+
* (versus bulk commands, which run separately for each project). The action executes
19+
* a user-defined script file.
20+
*
21+
* @remarks
22+
* Bulk commands can be defined via common/config/command-line.json. Rush's predefined "build"
23+
* and "rebuild" commands are also modeled as bulk commands, because they essentially just
24+
* invoke scripts from package.json in the same way as a custom command.
25+
*/
26+
export class GlobalScriptAction extends BaseScriptAction {
27+
private _scriptPath: string;
28+
29+
constructor(
30+
options: IGlobalScriptActionOptions
31+
) {
32+
super(options);
33+
this._scriptPath = options.scriptPath;
34+
}
35+
36+
public run(): Promise<void> {
37+
if (!fsx.existsSync(this.rushConfiguration.rushLinkJsonFilename)) {
38+
throw new Error(`File not found: ${this.rushConfiguration.rushLinkJsonFilename}` +
39+
`${os.EOL}Did you run "rush link"?`);
40+
}
41+
42+
// Collect all custom parameter values
43+
const customParameterValues: string[] = [];
44+
45+
for (const customParameter of this.customParameters) {
46+
customParameter.appendToArgList(customParameterValues);
47+
}
48+
49+
return Promise.resolve();
50+
}
51+
52+
protected onDefineParameters(): void {
53+
this.defineScriptParameters();
54+
}
55+
}

apps/rush-lib/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Positional arguments:
3939
rebuild Clean and rebuild the entire set of projects
4040
import-strings
4141
Imports translated strings into each project.
42+
deploy Deploys the build
4243
4344
Optional arguments:
4445
-h, --help Show this help message and exit.
@@ -135,6 +136,20 @@ Optional arguments:
135136
"
136137
`;
137138
139+
exports[`CommandLineHelp prints the help for "rush deploy" 1`] = `
140+
"usage: rush deploy [-h] [--locale {en-us,fr-fr,es-es,zh-cn}]
141+
142+
Uploads all the built assets to the CDN
143+
144+
Optional arguments:
145+
-h, --help Show this help message and exit.
146+
--locale {en-us,fr-fr,es-es,zh-cn}
147+
Selects a single instead of the default locale
148+
(en-us) for non-ship builds or all locales for ship
149+
builds.
150+
"
151+
`;
152+
138153
exports[`CommandLineHelp prints the help for "rush import-strings" 1`] = `
139154
"usage: rush import-strings [-h] [-p COUNT] [-t PROJECT1]
140155
[--to-version-policy VERSION_POLICY_NAME]

apps/rush-lib/src/cli/test/repo/common/config/rush/command-line.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"longName": "--locale",
2222
"parameterKind": "choice",
2323
"description": "Selects a single instead of the default locale (en-us) for non-ship builds or all locales for ship builds.",
24-
"associatedCommands": [ "import-strings" ],
24+
"associatedCommands": [ "import-strings", "deploy" ],
2525
"alternatives": [
2626
{
2727
"name": "en-us",

0 commit comments

Comments
 (0)