Skip to content

Commit 3549b4c

Browse files
committed
refactor(commands): drop what never shipped and the leftovers the review found
`executeCommandInProcess` and `canExecuteCommandInProcess` were renamed on this branch before any release, so they go rather than stay deprecated; the same for the `getInjector()` alias of `getRootInjector()`. The dispatcher's hierarchical branch in option priming was unreachable once parent names were routed to their leaf, the adapter shares `define-command`'s `isPlainObject` instead of carrying a second one, the options service is typed in the adapter, and `classCommandDefinition` is no longer exported.
1 parent b41cadd commit 3549b4c

10 files changed

Lines changed: 35 additions & 89 deletions

File tree

‎lib/common/define-command.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,14 @@ const invalid = (definition: any, problem: string): never => {
303303
);
304304
};
305305

306-
const isPlainObject = (value: any): boolean =>
307-
!!value && typeof value === "object" && !Array.isArray(value);
306+
export function isPlainObject(value: unknown): boolean {
307+
if (value === null || typeof value !== "object") {
308+
return false;
309+
}
310+
311+
const prototype = Object.getPrototypeOf(value);
312+
return prototype === Object.prototype || prototype === null;
313+
}
308314

309315
const validateName = (definition: any): void => {
310316
const name = definition.name;
@@ -809,9 +815,7 @@ const buildClassDefinition = (ctor: any): DefinedCommand<any, any, any> => {
809815
* was read from. The cache entry is an own property so a class extending
810816
* another command class never serves its parent's definition.
811817
*/
812-
export function classCommandDefinition(
813-
ctor: any,
814-
): DefinedCommand<any, any, any> {
818+
function classCommandDefinition(ctor: any): DefinedCommand<any, any, any> {
815819
if (!isCommandClass(ctor)) {
816820
throw new Error(
817821
`${describeDefinition(ctor)} is not a command class: it did not come ` +

‎lib/common/definitions/commands-service.d.ts‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ interface ICommandsService {
3333
commandArguments?: string[],
3434
options?: import("../contracts/commands-service").CommandDispatchOptions,
3535
): Promise<boolean>;
36-
/** @deprecated Use `runCommand`. */
37-
executeCommandInProcess(
38-
commandName: string,
39-
commandArguments?: string[],
40-
): Promise<void>;
41-
/** @deprecated Use `canExecuteCommand`. */
42-
canExecuteCommandInProcess(
43-
commandName: string,
44-
commandArguments?: string[],
45-
): Promise<boolean>;
4636
}
4737

4838
/**

‎lib/common/services/command-definition-adapter.ts‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Injector } from "../di/injector";
66
import { IDictionary, IDashedOption, IErrors } from "../declarations";
77
import { ICommand } from "../definitions/commands";
88
import { COMMAND_CONTEXT } from "../contracts/command-context";
9+
import { IOptions } from "../../declarations";
910
import {
1011
COMMAND_PRECONDITIONS,
1112
CommandPrecondition,
@@ -39,17 +40,9 @@ import {
3940
RegisterableCommand,
4041
defineCommand,
4142
toCommandDefinition,
43+
isPlainObject,
4244
} from "../define-command";
4345

44-
function isPlainObject(value: unknown): boolean {
45-
if (value === null || typeof value !== "object") {
46-
return false;
47-
}
48-
49-
const prototype = Object.getPrototypeOf(value);
50-
return prototype === Object.prototype || prototype === null;
51-
}
52-
5346
const OPTION_TYPES: IDictionary<OptionType> = {
5447
boolean: OptionType.Boolean,
5548
string: OptionType.String,
@@ -121,7 +114,7 @@ const warnOnCliOptionCollisions = (
121114
targetInjector: Injector,
122115
definition: CommandDefinition<any, any, any>,
123116
schema: CommandOptionsSchema,
124-
optionsService: any,
117+
optionsService: IOptions | undefined,
125118
): void => {
126119
const cliOptions = optionsService && optionsService.options;
127120
if (!cliOptions) {
@@ -217,7 +210,7 @@ export function createCommandFromDefinition<
217210

218211
// Only a definition that declares options may depend on the options service
219212
// being registered - a bare command must work without one.
220-
const optionsService: any = optionNames.length
213+
const optionsService: IOptions | undefined = optionNames.length
221214
? targetInjector.get("options")
222215
: null;
223216

@@ -284,7 +277,7 @@ export function createCommandFromDefinition<
284277
const buildContext = (args: string[]): CommandContext<TSchema> => {
285278
const options: any = {};
286279
for (const optionName of optionNames) {
287-
options[optionName] = optionsService[optionName];
280+
options[optionName] = (<any>optionsService)[optionName];
288281
}
289282

290283
return {

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -453,22 +453,6 @@ export class CommandsService
453453
return helpers.stringReplaceAll(name, "|", " ");
454454
}
455455

456-
/** @deprecated Use {@link runCommand}. */
457-
public executeCommandInProcess(
458-
commandName: string,
459-
commandArguments: string[] = [],
460-
): Promise<void> {
461-
return this.runCommand(commandName, commandArguments);
462-
}
463-
464-
/** @deprecated Use {@link canExecuteCommand}. */
465-
public canExecuteCommandInProcess(
466-
commandName: string,
467-
commandArguments: string[] = [],
468-
): Promise<boolean> {
469-
return this.canExecuteCommand(commandName, commandArguments);
470-
}
471-
472456
/**
473457
* A name is looked up in the registry; a definition or class is run as the
474458
* caller holds it, registered or not, so what runs is what was referenced.
@@ -561,10 +545,6 @@ export class CommandsService
561545
* done. An in-process dispatch has to put the parser back where it found it.
562546
*/
563547
private primeOptions(command: ICommand): () => void {
564-
if (command.isHierarchicalCommand) {
565-
return () => undefined;
566-
}
567-
568548
const declaredOptions = { ...this.$options.options };
569549
const parsedArgv = this.$options.argv;
570550

‎lib/common/yok.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,6 @@ export function getRootInjector(): IInjector {
750750
return injector;
751751
}
752752

753-
/** @deprecated Use getRootInjector(). */
754-
export function getInjector(): IInjector {
755-
return getRootInjector();
756-
}
757-
758753
/**
759754
* @deprecated Global-singleton wiring for the legacy facade; new code receives
760755
* the container via inject(Injector) instead of a process-wide global.

‎test/commands-service.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ describe("commands-service", () => {
191191
});
192192
});
193193

194-
describe("executeCommandInProcess", () => {
194+
describe("runCommand", () => {
195195
it("primes the command's declared options before it runs", async () => {
196196
const { injector, record } = createDispatchInjector(null);
197197
const command = definedCommand(record, { allowUnknownOptions: true });
198198
injector.resolveCommand = () => command;
199199
const service = injector.resolve(CommandsService);
200200

201-
await service.executeCommandInProcess("open|ios");
201+
await service.runCommand("open|ios");
202202

203203
assert.deepEqual(record.primedWith, [
204204
{ dashedOptions: command.dashedOptions, allowUnknown: true },
@@ -212,7 +212,7 @@ describe("commands-service", () => {
212212
injector.resolveCommand = () => definedCommand(record);
213213
const service = injector.resolve(CommandsService);
214214

215-
await service.executeCommandInProcess("open|ios");
215+
await service.runCommand("open|ios");
216216

217217
assert.deepEqual(options.options, { watch: cliOption });
218218
assert.strictEqual(options.argv, initialArgv);
@@ -229,7 +229,7 @@ describe("commands-service", () => {
229229
});
230230
const service = injector.resolve(CommandsService);
231231

232-
await assert.isRejected(service.executeCommandInProcess("open|ios"));
232+
await assert.isRejected(service.runCommand("open|ios"));
233233

234234
assert.deepEqual(options.options, { watch: cliOption });
235235
assert.strictEqual(options.argv, initialArgv);
@@ -244,7 +244,7 @@ describe("commands-service", () => {
244244
const service = injector.resolve(CommandsService);
245245

246246
await assert.isRejected(
247-
service.executeCommandInProcess("open|ios"),
247+
service.runCommand("open|ios"),
248248
"Command 'open|ios' cannot be executed.",
249249
);
250250

@@ -258,7 +258,7 @@ describe("commands-service", () => {
258258
const service = injector.resolve(CommandsService);
259259

260260
await assert.isRejected(
261-
service.executeCommandInProcess("open|ios", ["extra"]),
261+
service.runCommand("open|ios", ["extra"]),
262262
"This command doesn't accept parameters.",
263263
);
264264

@@ -275,7 +275,7 @@ describe("commands-service", () => {
275275
});
276276
const service = injector.resolve(CommandsService);
277277

278-
await service.executeCommandInProcess("install", ["lodash"]);
278+
await service.runCommand("install", ["lodash"]);
279279

280280
assert.deepEqual(record.executed, [["lodash"]]);
281281
assert.deepEqual(record.postCommandActions, [["lodash"]]);
@@ -294,7 +294,7 @@ describe("commands-service", () => {
294294

295295
let raised: Error = null;
296296
try {
297-
await service.executeCommandInProcess("open|ios");
297+
await service.runCommand("open|ios");
298298
} catch (err) {
299299
raised = err;
300300
}
@@ -309,7 +309,7 @@ describe("commands-service", () => {
309309
const service = injector.resolve(CommandsService);
310310

311311
await assert.isRejected(
312-
service.executeCommandInProcess("nope"),
312+
service.runCommand("nope"),
313313
"Unknown command 'nope'.",
314314
);
315315

@@ -322,8 +322,8 @@ describe("commands-service", () => {
322322
injector.resolveCommand = () => definedCommand(record);
323323
const service = injector.resolve(CommandsService);
324324

325-
await service.executeCommandInProcess("open|ios");
326-
await service.executeCommandInProcess("open|ios");
325+
await service.runCommand("open|ios");
326+
await service.runCommand("open|ios");
327327

328328
assert.deepEqual(record.executed, [[], []]);
329329
assert.equal(record.primedWith.length, 2);
@@ -334,7 +334,7 @@ describe("commands-service", () => {
334334
injector.resolveCommand = () => definedCommand(record);
335335
const service = injector.resolve(CommandsService);
336336

337-
await service.executeCommandInProcess("open|ios");
337+
await service.runCommand("open|ios");
338338

339339
assert.deepEqual(record.hooks, [
340340
"before:open-ios",

‎test/compat/injector-facade-surface.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert } from "chai";
2-
import { Yok, getInjector } from "../../lib/common/yok";
2+
import { Yok, getRootInjector } from "../../lib/common/yok";
33
import { Injector, inject, runInInjectionContext } from "../../lib/common/di";
44
import {
55
CommandRegistry,
@@ -57,17 +57,17 @@ describe("injector facade surface", () => {
5757
assert.strictEqual(sub.resolve("injector"), sub);
5858
});
5959

60-
it("keeps getInjector() synchronized with a direct global.$injector assignment", () => {
61-
const previous = getInjector();
60+
it("keeps getRootInjector() synchronized with a direct global.$injector assignment", () => {
61+
const previous = getRootInjector();
6262
const fresh = new Yok();
6363

6464
(<any>global).$injector = fresh;
6565
try {
66-
assert.strictEqual(getInjector(), fresh);
66+
assert.strictEqual(getRootInjector(), fresh);
6767
} finally {
6868
(<any>global).$injector = previous;
6969
}
70-
assert.strictEqual(getInjector(), previous);
70+
assert.strictEqual(getRootInjector(), previous);
7171
});
7272

7373
it("assigns the process-wide global.$injector", () => {

‎test/compat/legacy-hooks.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { assert } from "chai";
22
import * as fs from "fs";
33
import * as os from "os";
44
import * as path from "path";
5-
import { Yok, getInjector, setGlobalInjector } from "../../lib/common/yok";
5+
import { Yok, getRootInjector, setGlobalInjector } from "../../lib/common/yok";
66
import { HooksService } from "../../lib/common/services/hooks-service";
77
import { hook } from "../../lib/common/helpers";
88
import { IInjector } from "../../lib/common/definitions/yok";
@@ -301,7 +301,7 @@ describe("legacy hook contract", () => {
301301
}
302302
}
303303

304-
const previousInjector = getInjector();
304+
const previousInjector = getRootInjector();
305305
setGlobalInjector(testInjector);
306306
try {
307307
const result = await new Subject().doWork();
@@ -328,7 +328,7 @@ describe("legacy hook contract", () => {
328328
}
329329
}
330330

331-
const previousInjector = getInjector();
331+
const previousInjector = getRootInjector();
332332
setGlobalInjector(<any>{
333333
resolve: () => {
334334
throw new Error("the process-wide injector must be the last resort");

‎test/define-command.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,9 +691,7 @@ describe("defineCommand", () => {
691691

692692
const commandsService: ICommandsService =
693693
testInjector.resolve("commandsService");
694-
await commandsService.executeCommandInProcess(
695-
"dctest-shortcuts-in-process",
696-
);
694+
await commandsService.runCommand("dctest-shortcuts-in-process");
697695

698696
assert.isTrue(ran);
699697
assert.deepEqual(keyShortcutService.attached, []);

‎test/stubs.ts‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,20 +1342,6 @@ export class CommandsService implements ICommandsService {
13421342
return Promise.resolve(true);
13431343
}
13441344

1345-
public executeCommandInProcess(
1346-
commandName: string,
1347-
commandArguments?: string[],
1348-
): Promise<void> {
1349-
return this.runCommand(commandName, commandArguments);
1350-
}
1351-
1352-
public canExecuteCommandInProcess(
1353-
commandName: string,
1354-
commandArguments?: string[],
1355-
): Promise<boolean> {
1356-
return this.canExecuteCommand(commandName, commandArguments);
1357-
}
1358-
13591345
public completeCommand(): Promise<boolean> {
13601346
return Promise.resolve(true);
13611347
}

0 commit comments

Comments
 (0)