Skip to content

Commit f4c5756

Browse files
committed
Add optional flag to custom command and fix other minor issues in custom command.
1 parent 6a10172 commit f4c5756

6 files changed

Lines changed: 71 additions & 42 deletions

File tree

‎apps/rush-lib/src/cli/actions/CustomCommandFactory.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ export class CustomCommandFactory {
4646
throw new Error(`Cannot define two custom actions with the same name: "${command.name}"`);
4747
}
4848
customActions.set(command.name, new CustomRushAction(parser, {
49-
actionVerb: command.name,
50-
summary: command.summary,
51-
documentation: command.documentation || command.summary
52-
},
53-
command.parallelized));
49+
actionVerb: command.name,
50+
summary: command.summary,
51+
documentation: command.documentation || command.summary
52+
},
53+
command.parallelized,
54+
command.optional
55+
));
5456
});
5557

5658
// Associate each custom option to a command

‎apps/rush-lib/src/cli/actions/CustomRushAction.ts‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ export class CustomRushAction extends BaseRushAction {
4343

4444
constructor(private _parser: RushCommandLineParser,
4545
options: ICommandLineActionOptions,
46-
private _parallelized: boolean = false) {
47-
46+
private _parallelized: boolean = false,
47+
private _optional: boolean = false
48+
) {
4849
super(options);
4950
}
5051

@@ -67,7 +68,7 @@ export class CustomRushAction extends BaseRushAction {
6768
throw new Error(`File not found: ${this.rushConfiguration.rushLinkJsonFilename}` +
6869
`${os.EOL}Did you run "rush link"?`);
6970
}
70-
this.eventHooksManager.handle(Event.preRushBuild);
71+
this._doBeforeTask();
7172

7273
const stopwatch: Stopwatch = Stopwatch.start();
7374

@@ -103,24 +104,21 @@ export class CustomRushAction extends BaseRushAction {
103104
isQuietMode,
104105
parallelism,
105106
isIncrementalBuildAllowed: this.options.actionVerb === 'build',
106-
changedProjectsOnly
107+
changedProjectsOnly,
108+
optional: this._optional
107109
}
108110
);
109111

110112
return tasks.execute().then(
111113
() => {
112114
stopwatch.stop();
113115
console.log(colors.green(`rush ${this.options.actionVerb} (${stopwatch.toString()})`));
114-
this._collectTelemetry(stopwatch, true);
115-
this._parser.flushTelemetry();
116-
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
116+
this._doAfterTask(stopwatch, true);
117117
},
118118
() => {
119119
stopwatch.stop();
120120
console.log(colors.red(`rush ${this.options.actionVerb} - Errors! (${stopwatch.toString()})`));
121-
this._collectTelemetry(stopwatch, false);
122-
this._parser.flushTelemetry();
123-
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
121+
this._doAfterTask(stopwatch, false);
124122
this._parser.exitWithError();
125123
});
126124
}
@@ -191,6 +189,25 @@ export class CustomRushAction extends BaseRushAction {
191189
|| this._parallelized;
192190
}
193191

192+
private _doBeforeTask(): void {
193+
if (this.options.actionVerb !== 'build' && this.options.actionVerb !== 'rebuild') {
194+
// Only collects information for built-in tasks like build or rebuild.
195+
return;
196+
}
197+
198+
this.eventHooksManager.handle(Event.preRushBuild);
199+
}
200+
201+
private _doAfterTask(stopwatch: Stopwatch, success: boolean): void {
202+
if (this.options.actionVerb !== 'build' && this.options.actionVerb !== 'rebuild') {
203+
// Only collects information for built-in tasks like build or rebuild.
204+
return;
205+
}
206+
this._collectTelemetry(stopwatch, success);
207+
this._parser.flushTelemetry();
208+
this.eventHooksManager.handle(Event.postRushBuild, this._parser.isDebug);
209+
}
210+
194211
private _collectTelemetry(stopwatch: Stopwatch, success: boolean): void {
195212
const extraData: { [key: string]: string } = {
196213
command_to: (!!this._toFlag.value).toString(),

‎apps/rush-lib/src/cli/logic/TaskSelector.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { default as RushConfigurationProject } from '../../data/RushConfiguratio
66
import { JsonFile } from '@microsoft/node-core-library';
77

88
import TaskRunner from '../taskRunner/TaskRunner';
9-
import ProjectBuildTask from '../taskRunner/ProjectBuildTask';
9+
import ProjectTask from '../taskRunner/ProjectTask';
1010

1111
export interface ITaskSelectorConstructor {
1212
rushConfiguration: RushConfiguration;
@@ -18,13 +18,14 @@ export interface ITaskSelectorConstructor {
1818
parallelism: string;
1919
isIncrementalBuildAllowed: boolean;
2020
changedProjectsOnly: boolean;
21+
optional: boolean;
2122
}
2223

2324
/**
2425
* This class is responsible for:
2526
* - based on to/from flags, solving the dependency graph and figuring out which projects need to be run
26-
* - creating a ProjectBuildTask for each project that needs to be built
27-
* - registering the necessary ProjectBuildTasks with the TaskRunner, which actually orchestrates execution
27+
* - creating a ProjectTask for each project that needs to be built
28+
* - registering the necessary ProjectTasks with the TaskRunner, which actually orchestrates execution
2829
*
2930
* This class is currently only used by CustomRushAction
3031
*/
@@ -162,12 +163,14 @@ export class TaskSelector {
162163
private _registerTask(project: RushConfigurationProject | undefined): void {
163164
if (project) {
164165

165-
const projectTask: ProjectBuildTask = new ProjectBuildTask(
166+
const projectTask: ProjectTask = new ProjectTask(
166167
project,
167168
this._options.rushConfiguration,
168169
this._options.commandToRun,
169170
this._options.customFlags,
170-
this._options.isIncrementalBuildAllowed);
171+
this._options.isIncrementalBuildAllowed,
172+
this._options.optional
173+
);
171174

172175
if (!this._taskRunner.hasTask(projectTask.name)) {
173176
this._taskRunner.addTask(projectTask);

apps/rush-lib/src/cli/taskRunner/ProjectBuildTask.ts renamed to apps/rush-lib/src/cli/taskRunner/ProjectTask.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface IPackageDependencies extends IPackageDeps {
2828
/**
2929
* A TaskRunner task which cleans and builds a project
3030
*/
31-
export default class ProjectBuildTask implements ITaskDefinition {
31+
export default class ProjectTask implements ITaskDefinition {
3232
public get name(): string {
3333
return this._rushProject.packageName;
3434
}
@@ -40,28 +40,29 @@ export default class ProjectBuildTask implements ITaskDefinition {
4040
private _rushConfiguration: RushConfiguration,
4141
private _commandToRun: string,
4242
private _customFlags: string[],
43-
public isIncrementalBuildAllowed: boolean
43+
public isIncrementalBuildAllowed: boolean,
44+
private _optional: boolean
4445
) {}
4546

4647
public execute(writer: ITaskWriter): Promise<TaskStatus> {
4748
return new Promise<TaskStatus>((resolve: (status: TaskStatus) => void, reject: (errors: TaskError[]) => void) => {
4849
try {
49-
const build: string = this._getScriptToRun();
50-
const deps: IPackageDependencies | undefined = this._getPackageDependencies(build, writer);
51-
this._executeTask(build, writer, deps, resolve, reject);
50+
const taskCommand: string = this._getScriptToRun();
51+
const deps: IPackageDependencies | undefined = this._getPackageDependencies(taskCommand, writer);
52+
this._executeTask(taskCommand, writer, deps, resolve, reject);
5253
} catch (error) {
5354
reject([new TaskError('executing', error.toString())]);
5455
}
5556
});
5657
}
5758

58-
private _getPackageDependencies(buildCommand: string, writer: ITaskWriter): IPackageDependencies | undefined {
59+
private _getPackageDependencies(taskCommand: string, writer: ITaskWriter): IPackageDependencies | undefined {
5960
let deps: IPackageDependencies | undefined = undefined;
6061
PackageChangeAnalyzer.rushConfig = this._rushConfiguration;
6162
try {
6263
deps = {
6364
files: PackageChangeAnalyzer.instance.getPackageDepsHash(this._rushProject.packageName)!.files,
64-
arguments: buildCommand
65+
arguments: taskCommand
6566
};
6667
} catch (error) {
6768
writer.writeLine('Unable to calculate incremental build state. ' +
@@ -71,7 +72,7 @@ export default class ProjectBuildTask implements ITaskDefinition {
7172
}
7273

7374
private _executeTask(
74-
buildCommand: string,
75+
taskCommand: string,
7576
writer: ITaskWriter,
7677
currentPackageDeps: IPackageDependencies | undefined,
7778
resolve: (status: TaskStatus) => void,
@@ -107,29 +108,29 @@ export default class ProjectBuildTask implements ITaskDefinition {
107108
fsx.unlinkSync(currentDepsPath);
108109
}
109110

110-
if (!buildCommand) {
111+
if (!taskCommand) {
111112
// tslint:disable-next-line:max-line-length
112-
writer.writeLine(`The 'build' or 'test' command was registered in the package.json but is blank, so no action will be taken.`);
113-
resolve(TaskStatus.Success);
113+
writer.writeLine(`The task command ${this._commandToRun} was registered in the package.json but is blank, so no action will be taken.`);
114+
resolve(TaskStatus.Skipped);
114115
return;
115116
}
116117

117-
// Run the build step
118-
writer.writeLine(buildCommand);
119-
const buildTask: child_process.ChildProcess =
120-
Utilities.executeShellCommandAsync(buildCommand, projectFolder, process.env, true);
118+
// Run the task
119+
writer.writeLine(taskCommand);
120+
const task: child_process.ChildProcess =
121+
Utilities.executeShellCommandAsync(taskCommand, projectFolder, process.env, true);
121122

122123
// Hook into events, in order to get live streaming of build log
123-
buildTask.stdout.on('data', (data: string) => {
124+
task.stdout.on('data', (data: string) => {
124125
writer.write(data);
125126
});
126127

127-
buildTask.stderr.on('data', (data: string) => {
128+
task.stderr.on('data', (data: string) => {
128129
writer.writeError(data);
129130
this._hasWarningOrError = true;
130131
});
131132

132-
buildTask.on('close', (code: number) => {
133+
task.on('close', (code: number) => {
133134
// Write the logs to disk
134135
this._writeLogsToDisk(writer);
135136

@@ -172,14 +173,14 @@ export default class ProjectBuildTask implements ITaskDefinition {
172173
} else {
173174
script = this._getScriptCommand(this._commandToRun);
174175

175-
if (script === undefined) {
176+
if (script === undefined && !this._optional) {
176177
// tslint:disable-next-line:max-line-length
177178
throw new Error(`The project [${this._rushProject.packageName}] does not define a '${this._commandToRun}' command in the 'scripts' section of its package.json`);
178179
}
179180
}
180181

181-
if (script === '') {
182-
return script;
182+
if (!script) {
183+
return '';
183184
}
184185

185186
return `${script} ${this._customFlags.join(' ')}`;

‎apps/rush-lib/src/data/CommandLineConfiguration.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ICustomCommand {
1616
summary: string;
1717
documentation: string | undefined;
1818
parallelized: boolean;
19+
optional: boolean;
1920
}
2021

2122
export interface ICustomEnumValue {

‎apps/rush-lib/src/schemas/command-line.schema.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"title": "Custom Command",
2121
"type": "object",
2222
"additionalProperties": false,
23-
"required": [ "name", "summary", "documentation", "parallelized" ],
23+
"required": [ "name", "summary", "documentation", "parallelized", "optional" ],
2424
"properties": {
2525
"name": {
2626
"title": "Custom Command Name",
@@ -41,6 +41,11 @@
4141
"title": "Is Parallelized",
4242
"description": "Describes whether or not this command can be run in parallel (with multiple instances running on several projects simultaneously). Defaults to 'true'.",
4343
"type": "boolean"
44+
},
45+
"optional": {
46+
"title": "Is optional and only applies to projects with corresponding npm script",
47+
"description": "Indicates whether the command runs on every project or just projects that have the corresponding npm script.",
48+
"type": "boolean"
4449
}
4550
}
4651
}

0 commit comments

Comments
 (0)