Skip to content

Commit 104b428

Browse files
committed
fix(commands): let a parent's hooks read the subcommand's option groups
For `ns run ios` the before-run-ios hook fires before the subcommand is dispatched and its options are validated, so a service it reached could not read an option group. The subcommand's group values are recorded first.
1 parent 6fe691f commit 104b428

5 files changed

Lines changed: 135 additions & 10 deletions

File tree

‎lib/common/services/commands-service.ts‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,43 @@ export class CommandsService
267267
? undefined
268268
: (command && command.optionGroups) || [],
269269
);
270+
} else if (!this.isExecutingInProcess) {
271+
this.recordSubcommandOptions(commandName, commandArguments);
270272
}
271273

272274
return this.canExecuteResolvedCommand(commandName, commandArguments);
273275
}
274276

277+
/**
278+
* A parent's hooks run before the subcommand is dispatched and its options
279+
* are validated, and may reach a service that reads an option group. The
280+
* parser is put back: what the subcommand declares is not in force yet.
281+
*/
282+
private recordSubcommandOptions(
283+
parentName: string,
284+
commandArguments: string[],
285+
): void {
286+
const subcommand = this.$injector.buildHierarchicalCommand(
287+
parentName,
288+
commandArguments,
289+
);
290+
const command =
291+
subcommand && this.$injector.resolveCommand(subcommand.commandName);
292+
if (!command || command.isHierarchicalCommand) {
293+
return;
294+
}
295+
296+
const restoreOptions = this.saveOptions();
297+
try {
298+
this.$options.recordCommandLine(
299+
command.dashedOptions,
300+
command.optionGroups || [],
301+
);
302+
} finally {
303+
restoreOptions();
304+
}
305+
}
306+
275307
public async tryExecuteCommand(
276308
commandName: string,
277309
commandArguments: string[],
@@ -544,19 +576,24 @@ export class CommandsService
544576
};
545577
}
546578

579+
/** Returns a function that puts the parser back to its state now. */
580+
private saveOptions(): () => void {
581+
const declaredOptions = { ...this.$options.options };
582+
const parsedArgv = this.$options.argv;
583+
return (): void => {
584+
this.$options.options = declaredOptions;
585+
this.$options.argv = parsedArgv;
586+
};
587+
}
588+
547589
/**
548590
* Merging a command's options into the parser rewrites the values the host
549591
* process is still running on: a declared default replaces the CLI-wide one
550592
* and the host keeps reading the replacement long after the command is
551593
* done. An in-process dispatch has to put the parser back where it found it.
552594
*/
553595
private primeOptions(command: ICommand): () => void {
554-
const declaredOptions = { ...this.$options.options };
555-
const parsedArgv = this.$options.argv;
556-
const restore = (): void => {
557-
this.$options.options = declaredOptions;
558-
this.$options.argv = parsedArgv;
559-
};
596+
const restore = this.saveOptions();
560597

561598
// validateOptions merges the command's declarations into the live table
562599
// before it can throw, so a failed priming has to be undone here.

‎lib/declarations.d.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,14 @@ interface IOptions
623623
allowUnknownOptions?: boolean,
624624
commandLineGroups?: readonly object[],
625625
): void;
626+
/**
627+
* Parses the options of the command the user typed and fixes the values its
628+
* option groups read, without validating anything.
629+
*/
630+
recordCommandLine(
631+
commandSpecificDashedOptions: IDictionary<IDashedOption>,
632+
commandLineGroups: readonly object[],
633+
): void;
626634
options: IDictionary<IDashedOption>;
627635
shorthands: string[];
628636
/**

‎lib/options.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,23 @@ export class Options implements OptionValueReader {
328328
}
329329
}
330330

331-
if (commandLineGroups && !this.commandLineArgv) {
331+
if (commandLineGroups) {
332+
this.fixCommandLine(commandLineGroups);
333+
}
334+
}
335+
336+
public recordCommandLine(
337+
commandSpecificDashedOptions: IDictionary<IDashedOption>,
338+
commandLineGroups: readonly object[],
339+
): void {
340+
this.setupOptions(commandSpecificDashedOptions);
341+
this.fixCommandLine(commandLineGroups);
342+
}
343+
344+
private fixCommandLine(groups: readonly object[]): void {
345+
if (!this.commandLineArgv) {
332346
this.commandLineArgv = _.cloneDeep(this.argv);
333-
this.commandLineGroups = new Set(commandLineGroups);
347+
this.commandLineGroups = new Set(groups);
334348
}
335349
}
336350

‎test/define-command.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ describe("defineCommand", () => {
698698
testInjector.register("optionsTracker", {});
699699
testInjector.register("options", {
700700
validateOptions: (): void => undefined,
701+
recordCommandLine: (): void => undefined,
701702
});
702703
testInjector.register("commandsService", CommandsService);
703704
const keyShortcutService = keyShortcutServiceStub();
@@ -1352,6 +1353,7 @@ describe("defineCommand", () => {
13521353
validateOptions: (dashedOptions: any) => {
13531354
validatedOptions = dashedOptions;
13541355
},
1356+
recordCommandLine: (): void => undefined,
13551357
});
13561358
testInjector.register("commandsService", CommandsService);
13571359
return testInjector;
@@ -1514,6 +1516,7 @@ describe("defineCommand", () => {
15141516
testInjector.register("optionsTracker", {});
15151517
testInjector.register("options", {
15161518
validateOptions: (): void => undefined,
1519+
recordCommandLine: (): void => undefined,
15171520
});
15181521
testInjector.register("commandsService", CommandsService);
15191522
return testInjector;

‎test/services/commands-service-dispatch.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { inject, runInInjectionContext } from "../../lib/common/di";
66
import { COMMAND_OWNER } from "../../lib/common/contracts/command-registry";
77
import { CommandsService } from "../../lib/common/services/commands-service";
88
import { HooksService } from "../../lib/common/services/hooks-service";
9-
import { defineCommand, stringOption } from "../../lib/common/define-command";
9+
import {
10+
defineCommand,
11+
defineOptions,
12+
stringOption,
13+
} from "../../lib/common/define-command";
14+
import { COMMAND_LINE_EXPECTED } from "../../lib/common/contracts/command-line-expected";
1015
import { registerCommand } from "../../lib/common/services/command-definition-adapter";
1116
import { Options } from "../../lib/options";
1217
import { LoggerStub } from "../stubs";
@@ -24,7 +29,11 @@ interface IDispatchHarness {
2429
}
2530

2631
const createHarness = (
27-
opts: { disableCommandHooks?: boolean; realOptions?: boolean } = {},
32+
opts: {
33+
disableCommandHooks?: boolean;
34+
realOptions?: boolean;
35+
commandLine?: boolean;
36+
} = {},
2837
): IDispatchHarness => {
2938
const testInjector = new Yok();
3039
const harness = <IDispatchHarness>(<unknown>{
@@ -73,6 +82,10 @@ const createHarness = (
7382
trackOptions: async (): Promise<void> => undefined,
7483
});
7584
if (opts.realOptions) {
85+
testInjector.register({
86+
provide: COMMAND_LINE_EXPECTED,
87+
useValue: !!opts.commandLine,
88+
});
7689
testInjector.register("hostInfo", {});
7790
testInjector.register("settingsService", {
7891
setSettings: (): any => undefined,
@@ -82,6 +95,7 @@ const createHarness = (
8295
} else {
8396
testInjector.register("options", {
8497
validateOptions: (): void => undefined,
98+
recordCommandLine: (): void => undefined,
8599
});
86100
}
87101
testInjector.register("commandsService", CommandsService);
@@ -272,6 +286,55 @@ describe("CommandsService in-process dispatch", () => {
272286
assert.deepEqual(inProcess.hooks, commandLine.hooks);
273287
});
274288

289+
it("gives option groups the subcommand's values before the parent's hooks", async () => {
290+
const argv = process.argv;
291+
process.argv = [
292+
"node",
293+
"ns",
294+
"dctest-groups",
295+
"child",
296+
"--level",
297+
"typed",
298+
];
299+
try {
300+
const harness = createHarness({
301+
realOptions: true,
302+
commandLine: true,
303+
});
304+
const Level = defineOptions({
305+
level: stringOption({ default: "group" }),
306+
});
307+
const Unlisted = defineOptions({
308+
level: stringOption({ default: "unlisted" }),
309+
});
310+
const level = harness.injector.get(Level);
311+
const unlisted = harness.injector.get(Unlisted);
312+
const seen: string[] = [];
313+
harness.injector.resolve("hooksService").executeBeforeHooks = async (
314+
name: string,
315+
): Promise<any[]> => {
316+
seen.push(`${name}: ${level.level} ${unlisted.level}`);
317+
return [];
318+
};
319+
register(harness.injector, {
320+
name: "dctest-groups|child",
321+
options: [Level],
322+
run: (): void => undefined,
323+
});
324+
325+
await harness.commandsService.tryExecuteCommand("dctest-groups", [
326+
"child",
327+
]);
328+
329+
assert.deepEqual(seen, [
330+
"dctest-groups-child: typed unlisted",
331+
"dctest-groups|child: typed unlisted",
332+
]);
333+
} finally {
334+
process.argv = argv;
335+
}
336+
});
337+
275338
it("rejects with the subcommand's failure instead of exiting", async () => {
276339
const harness = createHarness();
277340
register(harness.injector, {

0 commit comments

Comments
 (0)