Skip to content

Commit 955efb2

Browse files
committed
refactor(commands): move the structured commands to the class form
The commands with real internal structure - state shared between canExecute and run, values derived once per invocation, several private steps - are classes now, with one inject() field per dependency and the handlers as methods. Long handlers are split into private methods along the seams that were already there. EmbedCommand asks prepare whether it could run instead of importing its canExecute, which is what canExecuteCommand exists for.
1 parent 60d2885 commit 955efb2

21 files changed

Lines changed: 1471 additions & 1664 deletions

lib/bootstrap.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,8 @@ injector.require(
176176
);
177177
injector.require("platformCommandParameter", "./platform-command-param");
178178
registerBuiltInCommand<
179-
typeof import("./commands/create-project").createProjectCommandDefinition
180-
>(
181-
"create",
182-
() => require("./commands/create-project").createProjectCommandDefinition,
183-
);
179+
typeof import("./commands/create-project").CreateProjectCommand
180+
>("create", () => require("./commands/create-project").CreateProjectCommand);
184181
registerBuiltInCommand<
185182
typeof import("./commands/clean").cleanCommandDefinition
186183
>("clean", () => require("./commands/clean").cleanCommandDefinition);
@@ -206,22 +203,19 @@ registerBuiltInCommand<
206203
() => require("./commands/list-platforms").listPlatformsCommandDefinition,
207204
);
208205
registerBuiltInCommand<
209-
typeof import("./commands/add-platform").addPlatformCommandDefinition
210-
>(
211-
"platform|add",
212-
() => require("./commands/add-platform").addPlatformCommandDefinition,
213-
);
206+
typeof import("./commands/add-platform").AddPlatformCommand
207+
>("platform|add", () => require("./commands/add-platform").AddPlatformCommand);
214208
registerBuiltInCommand<
215209
typeof import("./commands/remove-platform").removePlatformCommandDefinition
216210
>(
217211
"platform|remove",
218212
() => require("./commands/remove-platform").removePlatformCommandDefinition,
219213
);
220214
registerBuiltInCommand<
221-
typeof import("./commands/update-platform").updatePlatformCommandDefinition
215+
typeof import("./commands/update-platform").UpdatePlatformCommand
222216
>(
223217
"platform|update",
224-
() => require("./commands/update-platform").updatePlatformCommandDefinition,
218+
() => require("./commands/update-platform").UpdatePlatformCommand,
225219
);
226220
registerBuiltInCommand<typeof import("./commands/run").runCommandDefinition>(
227221
"run|*all",
@@ -259,13 +253,15 @@ registerBuiltInCommand<typeof import("./commands/open").visionOpenCommand>(
259253
"open|vision",
260254
() => require("./commands/open").visionOpenCommand,
261255
);
262-
registerBuiltInCommand<
263-
typeof import("./commands/typings").typingsCommandDefinition
264-
>("typings", () => require("./commands/typings").typingsCommandDefinition);
256+
registerBuiltInCommand<typeof import("./commands/typings").TypingsCommand>(
257+
"typings",
258+
() => require("./commands/typings").TypingsCommand,
259+
);
265260

266-
registerBuiltInCommand<
267-
typeof import("./commands/preview").previewCommandDefinition
268-
>("preview", () => require("./commands/preview").previewCommandDefinition);
261+
registerBuiltInCommand<typeof import("./commands/preview").PreviewCommand>(
262+
"preview",
263+
() => require("./commands/preview").PreviewCommand,
264+
);
269265

270266
registerBuiltInCommand<typeof import("./commands/debug").iosDebugCommand>(
271267
"debug|ios",
@@ -312,8 +308,8 @@ registerBuiltInCommand<
312308
>("deploy", () => require("./commands/deploy").deployCommandDefinition);
313309

314310
registerBuiltInCommand<
315-
typeof import("./commands/embedding/embed").embedCommandDefinition
316-
>("embed", () => require("./commands/embedding/embed").embedCommandDefinition);
311+
typeof import("./commands/embedding/embed").EmbedCommand
312+
>("embed", () => require("./commands/embedding/embed").EmbedCommand);
317313

318314
injector.require("testExecutionService", "./services/test-execution-service");
319315
injector.require(
@@ -342,9 +338,10 @@ registerBuiltInCommand<
342338
"test|visionos",
343339
() => require("./commands/test").testVisionOSCommandDefinition,
344340
);
345-
registerBuiltInCommand<
346-
typeof import("./commands/test-init").testInitCommandDefinition
347-
>("test|init", () => require("./commands/test-init").testInitCommandDefinition);
341+
registerBuiltInCommand<typeof import("./commands/test-init").TestInitCommand>(
342+
"test|init",
343+
() => require("./commands/test-init").TestInitCommand,
344+
);
348345
registerBuiltInCommand<
349346
typeof import("./commands/generate-help").generateHelpCommandDefinition
350347
>(
@@ -353,23 +350,20 @@ registerBuiltInCommand<
353350
);
354351

355352
registerBuiltInCommand<
356-
typeof import("./commands/appstore-list").listiOSAppsCommandDefinition
353+
typeof import("./commands/appstore-list").ListiOSAppsCommand
357354
>(
358355
"appstore|*list",
359-
() => require("./commands/appstore-list").listiOSAppsCommandDefinition,
356+
() => require("./commands/appstore-list").ListiOSAppsCommand,
360357
);
361358
registerBuiltInCommand<
362-
typeof import("./commands/appstore-upload").publishIOSCommandDefinition
359+
typeof import("./commands/appstore-upload").PublishIOSCommand
363360
>(
364361
"appstore|upload",
365-
() => require("./commands/appstore-upload").publishIOSCommandDefinition,
362+
() => require("./commands/appstore-upload").PublishIOSCommand,
366363
);
367364
registerBuiltInCommand<
368-
typeof import("./commands/appstore-upload").publishIOSCommandDefinition
369-
>(
370-
"publish|ios",
371-
() => require("./commands/appstore-upload").publishIOSCommandDefinition,
372-
);
365+
typeof import("./commands/appstore-upload").PublishIOSCommand
366+
>("publish|ios", () => require("./commands/appstore-upload").PublishIOSCommand);
373367
registerBuiltInCommand<
374368
typeof import("./commands/apple-login").appleLoginCommandDefinition
375369
>(
@@ -459,17 +453,16 @@ registerBuiltInCommand<
459453
require("./commands/plugin/update-plugin").updatePluginCommandDefinition,
460454
);
461455
registerBuiltInCommand<
462-
typeof import("./commands/plugin/build-plugin").buildPluginCommandDefinition
456+
typeof import("./commands/plugin/build-plugin").BuildPluginCommand
463457
>(
464458
"plugin|build",
465-
() => require("./commands/plugin/build-plugin").buildPluginCommandDefinition,
459+
() => require("./commands/plugin/build-plugin").BuildPluginCommand,
466460
);
467461
registerBuiltInCommand<
468-
typeof import("./commands/plugin/create-plugin").createPluginCommandDefinition
462+
typeof import("./commands/plugin/create-plugin").CreatePluginCommand
469463
>(
470464
"plugin|create",
471-
() =>
472-
require("./commands/plugin/create-plugin").createPluginCommandDefinition,
465+
() => require("./commands/plugin/create-plugin").CreatePluginCommand,
473466
);
474467

475468
registerBuiltInCommand<
@@ -575,10 +568,10 @@ injector.require(
575568
injector.require("messages", "./common/messages/messages");
576569

577570
registerBuiltInCommand<
578-
typeof import("./commands/post-install").postInstallCliCommandDefinition
571+
typeof import("./commands/post-install").PostInstallCliCommand
579572
>(
580573
"post-install-cli",
581-
() => require("./commands/post-install").postInstallCliCommandDefinition,
574+
() => require("./commands/post-install").PostInstallCliCommand,
582575
);
583576
registerBuiltInCommand<
584577
typeof import("./commands/migrate").migrateCommandDefinition

lib/commands/add-platform.ts

Lines changed: 56 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import { canExecuteCommandBase } from "./command-base";
12
import {
2-
canExecuteCommandBase,
3-
injectPlatformCommandServices,
4-
} from "./command-base";
5-
import { IPlatformCommandHelper } from "../declarations";
3+
IPlatformCommandHelper,
4+
IPlatformValidationService,
5+
} from "../declarations";
6+
import { IProjectData } from "../definitions/project";
67
import { IErrors } from "../common/declarations";
78
import {
8-
CommandContext,
9+
Command,
910
CommandOptionsSchema,
10-
defineCommand,
1111
stringOption,
1212
} from "../common/define-command";
1313
import { inject } from "../common/di";
@@ -16,82 +16,63 @@ const addPlatformCommandOptions = {
1616
frameworkPath: stringOption(),
1717
} satisfies CommandOptionsSchema;
1818

19-
export type AddPlatformCommandContext = CommandContext<
20-
typeof addPlatformCommandOptions
21-
>;
19+
export class AddPlatformCommand extends Command({
20+
name: "platform|add",
21+
description:
22+
"Configures the current project to target the selected platform.",
23+
options: addPlatformCommandOptions,
24+
arguments: "any",
25+
}) {
26+
private $errors = inject<IErrors>("errors");
27+
private $platformCommandHelper = inject<IPlatformCommandHelper>(
28+
"platformCommandHelper",
29+
);
30+
private $platformValidationService = inject<IPlatformValidationService>(
31+
"platformValidationService",
32+
);
33+
private $projectData = inject<IProjectData>("projectData");
2234

23-
export function setupAddPlatformCommand() {
24-
const services = {
25-
...injectPlatformCommandServices(),
26-
$errors: inject<IErrors>("errors"),
27-
$platformCommandHelper: inject<IPlatformCommandHelper>(
28-
"platformCommandHelper",
29-
),
30-
};
31-
services.$projectData.initializeProjectData();
35+
constructor() {
36+
super();
37+
this.$projectData.initializeProjectData();
38+
}
3239

33-
return services;
34-
}
40+
public async canExecute(): Promise<boolean> {
41+
const args = this.args;
42+
if (!args || args.length === 0) {
43+
this.$errors.failWithHelp(
44+
"No platform specified. Please specify a platform to add.",
45+
);
46+
}
3547

36-
export type IAddPlatformCommandServices = ReturnType<
37-
typeof setupAddPlatformCommand
38-
>;
48+
let canExecute = true;
49+
for (const arg of args) {
50+
this.$platformValidationService.validatePlatform(arg, this.$projectData);
3951

40-
export async function canExecuteAddPlatformCommand(
41-
context: AddPlatformCommandContext,
42-
services: IAddPlatformCommandServices,
43-
): Promise<boolean> {
44-
const args = context.args;
45-
if (!args || args.length === 0) {
46-
services.$errors.failWithHelp(
47-
"No platform specified. Please specify a platform to add.",
48-
);
49-
}
52+
if (
53+
!this.$platformValidationService.isPlatformSupportedForOS(
54+
arg,
55+
this.$projectData,
56+
)
57+
) {
58+
this.$errors.fail(
59+
`Applications for platform ${arg} cannot be built on this OS`,
60+
);
61+
}
5062

51-
let canExecute = true;
52-
for (const arg of args) {
53-
services.$platformValidationService.validatePlatform(
54-
arg,
55-
services.$projectData,
56-
);
57-
58-
if (
59-
!services.$platformValidationService.isPlatformSupportedForOS(
60-
arg,
61-
services.$projectData,
62-
)
63-
) {
64-
services.$errors.fail(
65-
`Applications for platform ${arg} cannot be built on this OS`,
66-
);
63+
// The assignment overwrites the previous platform's verdict, so only the
64+
// last one decides.
65+
canExecute = await canExecuteCommandBase(this.context, arg);
6766
}
6867

69-
// The assignment overwrites the previous platform's verdict, so only the
70-
// last one decides. Kept as it was.
71-
canExecute = await canExecuteCommandBase(services, arg);
68+
return canExecute;
7269
}
7370

74-
return canExecute;
75-
}
76-
77-
export async function runAddPlatformCommand(
78-
context: AddPlatformCommandContext,
79-
services: IAddPlatformCommandServices,
80-
): Promise<void> {
81-
await services.$platformCommandHelper.addPlatforms(
82-
context.args,
83-
services.$projectData,
84-
context.options.frameworkPath,
85-
);
71+
public async run(): Promise<void> {
72+
await this.$platformCommandHelper.addPlatforms(
73+
this.args,
74+
this.$projectData,
75+
this.options.frameworkPath,
76+
);
77+
}
8678
}
87-
88-
export const addPlatformCommandDefinition = defineCommand({
89-
name: "platform|add",
90-
description:
91-
"Configures the current project to target the selected platform.",
92-
options: addPlatformCommandOptions,
93-
arguments: "any",
94-
setup: setupAddPlatformCommand,
95-
canExecute: canExecuteAddPlatformCommand,
96-
run: runAddPlatformCommand,
97-
});

0 commit comments

Comments
 (0)