Skip to content

Commit 0a5d9ef

Browse files
committed
Allow the "rebuild" command to have different behavior than the "build" command.
1 parent acd14cd commit 0a5d9ef

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ export class RushCommandLineParser extends CommandLineParser {
200200
}
201201

202202
if (!this.tryGetAction(RushConstants.rebuildCommandName)) {
203-
this._addCommandLineConfigAction(commandLineConfiguration, CommandLineConfiguration.defaultRebuildCommandJson);
203+
this._addCommandLineConfigAction(
204+
commandLineConfiguration,
205+
CommandLineConfiguration.defaultRebuildCommandJson,
206+
RushConstants.buildCommandName
207+
);
204208
}
205209
}
206210

@@ -217,7 +221,8 @@ export class RushCommandLineParser extends CommandLineParser {
217221

218222
private _addCommandLineConfigAction(
219223
commandLineConfiguration: CommandLineConfiguration | undefined,
220-
command: CommandJson
224+
command: CommandJson,
225+
commandToRun?: string
221226
): void {
222227
if (this.tryGetAction(command.name)) {
223228
throw new Error(`${RushConstants.commandLineFilename} defines a command "${command.name}"`
@@ -231,9 +236,9 @@ export class RushCommandLineParser extends CommandLineParser {
231236
this.addAction(new BulkScriptAction({
232237
actionName: command.name,
233238

234-
// The rush rebuild and rush build command invoke the same NPM script because they share the same
235-
// package-deps-hash state.
236-
commandToRun: command.name === RushConstants.rebuildCommandName ? 'build' : undefined,
239+
// By default, the "rebuild" action runs the "build" script. However, if the command-line.json file
240+
// overrides "rebuild," the "rebuild" script should be run.
241+
commandToRun: commandToRun,
237242

238243
summary: command.summary,
239244
documentation: command.description || command.summary,

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,28 @@
44
import * as os from 'os';
55
import * as colors from 'colors';
66

7-
import {
8-
Event
9-
} from '../../index';
10-
117
import {
128
CommandLineFlagParameter,
139
CommandLineStringParameter,
1410
CommandLineStringListParameter,
1511
CommandLineParameterKind
1612
} from '@rushstack/ts-command-line';
13+
import {
14+
FileSystem,
15+
PackageJsonLookup,
16+
IPackageJson
17+
} from '@rushstack/node-core-library';
1718

19+
import { Event } from '../../index';
1820
import { SetupChecks } from '../../logic/SetupChecks';
1921
import { TaskSelector } from '../../logic/TaskSelector';
2022
import { Stopwatch } from '../../utilities/Stopwatch';
2123
import { AlreadyReportedError } from '../../utilities/AlreadyReportedError';
2224
import { BaseScriptAction, IBaseScriptActionOptions } from './BaseScriptAction';
23-
import {
24-
FileSystem,
25-
PackageJsonLookup,
26-
IPackageJson
27-
} from '@rushstack/node-core-library';
2825
import { TaskRunner } from '../../logic/taskRunner/TaskRunner';
2926
import { TaskCollection } from '../../logic/taskRunner/TaskCollection';
3027
import { Utilities } from '../../utilities/Utilities';
28+
import { RushConstants } from '../../logic/RushConstants';
3129

3230
/**
3331
* Constructor parameters for BulkScriptAction.
@@ -255,7 +253,7 @@ export class BulkScriptAction extends BaseScriptAction {
255253
}
256254

257255
private _doBeforeTask(): void {
258-
if (this.actionName !== 'build' && this.actionName !== 'rebuild') {
256+
if (this.actionName !== RushConstants.buildCommandName && this.actionName !== RushConstants.rebuildCommandName) {
259257
// Only collects information for built-in tasks like build or rebuild.
260258
return;
261259
}
@@ -266,7 +264,7 @@ export class BulkScriptAction extends BaseScriptAction {
266264
}
267265

268266
private _doAfterTask(stopwatch: Stopwatch, success: boolean): void {
269-
if (this.actionName !== 'build' && this.actionName !== 'rebuild') {
267+
if (this.actionName !== RushConstants.buildCommandName && this.actionName !== RushConstants.rebuildCommandName) {
270268
// Only collects information for built-in tasks like build or rebuild.
271269
return;
272270
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('RushCommandLineParser', () => {
199199
});
200200

201201
describe(`'rebuild' action`, () => {
202-
it(`executes the package's 'build' script`, () => {
202+
it(`executes the package's 'rebuild' script`, () => {
203203
const repoName: string = 'overrideRebuildAndRunRebuildActionRepo';
204204
const instance: IParserTestInstance = getCommandLineParserInstance(repoName, 'rebuild');
205205

@@ -211,7 +211,7 @@ describe('RushCommandLineParser', () => {
211211
expect(packageCount).toEqual(2);
212212

213213
// Use regex for task name in case spaces were prepended or appended to spawned command
214-
const expectedBuildTaskRegexp: RegExp = /fake_build_task_but_works_with_mock/;
214+
const expectedBuildTaskRegexp: RegExp = /fake_REbuild_task_but_works_with_mock/;
215215

216216
// eslint-disable-next-line @typescript-eslint/no-explicit-any
217217
const firstSpawn: any[] = instance.spawnMock.mock.calls[0];

0 commit comments

Comments
 (0)