Skip to content

Commit 5668eda

Browse files
committed
refactor(cli): deduplicate lazy subcommand registration
1 parent 345cfde commit 5668eda

1 file changed

Lines changed: 61 additions & 183 deletions

File tree

‎src/cli/program/register.subclis-core.ts‎

Lines changed: 61 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -80,51 +80,33 @@ async function registerSubCliWithPluginCommands(
8080
}
8181
}
8282

83+
function defineImportedSubCliGroups(
84+
definitions: ReadonlyArray<
85+
readonly [readonly string[], () => Promise<Record<string, unknown>>, string]
86+
>,
87+
) {
88+
return defineImportedProgramCommandGroupSpecs(
89+
definitions.map(([commandNames, loadModule, exportName]) => ({
90+
commandNames,
91+
loadModule,
92+
exportName,
93+
})),
94+
);
95+
}
96+
8397
// Note for humans and agents:
8498
// If you update the list of commands, also check whether they have subcommands
8599
// and set the flag accordingly.
86100
const entrySpecs: readonly CommandGroupDescriptorSpec<SubCliRegistrar>[] = [
87-
...defineImportedProgramCommandGroupSpecs([
88-
{
89-
commandNames: ["acp"],
90-
loadModule: () => import("../acp-cli.js"),
91-
exportName: "registerAcpCli",
92-
},
93-
{
94-
commandNames: ["gateway"],
95-
loadModule: () => import("../gateway-cli.js"),
96-
exportName: "registerGatewayCli",
97-
},
98-
{
99-
commandNames: ["daemon"],
100-
loadModule: () => import("../daemon-cli.js"),
101-
exportName: "registerDaemonCli",
102-
},
103-
{
104-
commandNames: ["logs"],
105-
loadModule: () => import("../logs-cli.js"),
106-
exportName: "registerLogsCli",
107-
},
108-
{
109-
commandNames: ["system"],
110-
loadModule: () => import("../system-cli.js"),
111-
exportName: "registerSystemCli",
112-
},
113-
{
114-
commandNames: ["models"],
115-
loadModule: () => import("../models-cli.js"),
116-
exportName: "registerModelsCli",
117-
},
118-
{
119-
commandNames: ["promos"],
120-
loadModule: () => import("../promos-cli.js"),
121-
exportName: "registerPromosCli",
122-
},
123-
{
124-
commandNames: ["telemetry"],
125-
loadModule: () => import("../telemetry-cli.js"),
126-
exportName: "registerTelemetryCli",
127-
},
101+
...defineImportedSubCliGroups([
102+
[["acp"], () => import("../acp-cli.js"), "registerAcpCli"],
103+
[["gateway"], () => import("../gateway-cli.js"), "registerGatewayCli"],
104+
[["daemon"], () => import("../daemon-cli.js"), "registerDaemonCli"],
105+
[["logs"], () => import("../logs-cli.js"), "registerLogsCli"],
106+
[["system"], () => import("../system-cli.js"), "registerSystemCli"],
107+
[["models"], () => import("../models-cli.js"), "registerModelsCli"],
108+
[["promos"], () => import("../promos-cli.js"), "registerPromosCli"],
109+
[["telemetry"], () => import("../telemetry-cli.js"), "registerTelemetryCli"],
128110
]),
129111
{
130112
commandNames: ["infer", "capability"],
@@ -133,19 +115,15 @@ const entrySpecs: readonly CommandGroupDescriptorSpec<SubCliRegistrar>[] = [
133115
await mod.registerCapabilityCli(program, argv);
134116
},
135117
},
136-
...defineImportedProgramCommandGroupSpecs([
137-
{
138-
// exec-approvals is a commander alias on the approvals command; the lazy
139-
// router only routes names listed here, so the alias must be owned too.
140-
commandNames: ["approvals", "exec-approvals"],
141-
loadModule: () => import("../exec-approvals-cli.js"),
142-
exportName: "registerExecApprovalsCli",
143-
},
144-
{
145-
commandNames: ["exec-policy"],
146-
loadModule: () => import("../exec-policy-cli.js"),
147-
exportName: "registerExecPolicyCli",
148-
},
118+
...defineImportedSubCliGroups([
119+
// exec-approvals is a commander alias on the approvals command; the lazy
120+
// router only routes names listed here, so the alias must be owned too.
121+
[
122+
["approvals", "exec-approvals"],
123+
() => import("../exec-approvals-cli.js"),
124+
"registerExecApprovalsCli",
125+
],
126+
[["exec-policy"], () => import("../exec-policy-cli.js"), "registerExecPolicyCli"],
149127
]),
150128
{
151129
commandNames: ["nodes"],
@@ -154,109 +132,29 @@ const entrySpecs: readonly CommandGroupDescriptorSpec<SubCliRegistrar>[] = [
154132
await mod.registerNodesCli(program, argv);
155133
},
156134
},
157-
...defineImportedProgramCommandGroupSpecs([
158-
{
159-
commandNames: ["devices"],
160-
loadModule: () => import("../devices-cli.js"),
161-
exportName: "registerDevicesCli",
162-
},
163-
{
164-
commandNames: ["users"],
165-
loadModule: () => import("../users-cli.js"),
166-
exportName: "registerUsersCli",
167-
},
168-
{
169-
commandNames: ["node"],
170-
loadModule: () => import("../node-cli.js"),
171-
exportName: "registerNodeCli",
172-
},
173-
{
174-
commandNames: ["connect"],
175-
loadModule: () => import("../connect-cli.js"),
176-
exportName: "registerConnectCli",
177-
},
178-
{
179-
commandNames: ["worker"],
180-
loadModule: () => import("../worker-cli.js"),
181-
exportName: "registerWorkerCli",
182-
},
183-
{
184-
commandNames: ["sandbox"],
185-
loadModule: () => import("../sandbox-cli.js"),
186-
exportName: "registerSandboxCli",
187-
},
188-
{
189-
commandNames: ["fleet"],
190-
loadModule: () => import("../fleet-cli.js"),
191-
exportName: "registerFleetCli",
192-
},
193-
{
194-
commandNames: ["worktrees"],
195-
loadModule: () => import("../worktrees-cli.js"),
196-
exportName: "registerWorktreesCli",
197-
},
198-
{
199-
commandNames: ["attach"],
200-
loadModule: () => import("../attach-cli.js"),
201-
exportName: "registerAttachCli",
202-
},
203-
{
204-
commandNames: ["tui", "terminal", "chat"],
205-
loadModule: () => import("../tui-cli.js"),
206-
exportName: "registerTuiCli",
207-
},
208-
{
209-
commandNames: ["resume"],
210-
loadModule: () => import("../resume-cli.js"),
211-
exportName: "registerResumeCli",
212-
},
213-
{
214-
// automations is a commander alias on the cron command; the lazy
215-
// router only routes names listed here, so the alias must be owned too.
216-
commandNames: ["cron", "automations"],
217-
loadModule: () => import("../cron-cli.js"),
218-
exportName: "registerCronCli",
219-
},
220-
{
221-
commandNames: ["dns"],
222-
loadModule: () => import("../dns-cli.js"),
223-
exportName: "registerDnsCli",
224-
},
225-
{
226-
commandNames: ["docs"],
227-
loadModule: () => import("../docs-cli.js"),
228-
exportName: "registerDocsCli",
229-
},
230-
{
231-
commandNames: ["qa"],
232-
loadModule: loadPrivateQaCliModule,
233-
exportName: "registerQaLabCli",
234-
},
235-
{
236-
commandNames: ["proxy"],
237-
loadModule: () => import("../proxy-cli.js"),
238-
exportName: "registerProxyCli",
239-
},
240-
{
241-
commandNames: ["hooks"],
242-
loadModule: () => import("../hooks-cli.js"),
243-
exportName: "registerHooksCli",
244-
},
245-
{
246-
commandNames: ["webhooks"],
247-
loadModule: () => import("../webhooks-cli.js"),
248-
exportName: "registerWebhooksCli",
249-
},
250-
{
251-
commandNames: ["qr"],
252-
loadModule: () => import("../qr-cli.js"),
253-
exportName: "registerQrCli",
254-
},
255-
{
256-
commandNames: ["clawbot"],
257-
loadModule: () => import("../clawbot-cli.js"),
258-
exportName: "registerClawbotCli",
259-
},
135+
...defineImportedSubCliGroups([
136+
[["devices"], () => import("../devices-cli.js"), "registerDevicesCli"],
137+
[["users"], () => import("../users-cli.js"), "registerUsersCli"],
138+
[["node"], () => import("../node-cli.js"), "registerNodeCli"],
139+
[["connect"], () => import("../connect-cli.js"), "registerConnectCli"],
140+
[["worker"], () => import("../worker-cli.js"), "registerWorkerCli"],
141+
[["sandbox"], () => import("../sandbox-cli.js"), "registerSandboxCli"],
142+
[["fleet"], () => import("../fleet-cli.js"), "registerFleetCli"],
143+
[["worktrees"], () => import("../worktrees-cli.js"), "registerWorktreesCli"],
144+
[["attach"], () => import("../attach-cli.js"), "registerAttachCli"],
145+
[["tui", "terminal", "chat"], () => import("../tui-cli.js"), "registerTuiCli"],
146+
[["resume"], () => import("../resume-cli.js"), "registerResumeCli"],
147+
// automations is a commander alias on the cron command; the lazy
148+
// router only routes names listed here, so the alias must be owned too.
149+
[["cron", "automations"], () => import("../cron-cli.js"), "registerCronCli"],
150+
[["dns"], () => import("../dns-cli.js"), "registerDnsCli"],
151+
[["docs"], () => import("../docs-cli.js"), "registerDocsCli"],
152+
[["qa"], loadPrivateQaCliModule, "registerQaLabCli"],
153+
[["proxy"], () => import("../proxy-cli.js"), "registerProxyCli"],
154+
[["hooks"], () => import("../hooks-cli.js"), "registerHooksCli"],
155+
[["webhooks"], () => import("../webhooks-cli.js"), "registerWebhooksCli"],
156+
[["qr"], () => import("../qr-cli.js"), "registerQrCli"],
157+
[["clawbot"], () => import("../clawbot-cli.js"), "registerClawbotCli"],
260158
]),
261159
{
262160
commandNames: ["pairing"],
@@ -295,32 +193,12 @@ const entrySpecs: readonly CommandGroupDescriptorSpec<SubCliRegistrar>[] = [
295193
});
296194
},
297195
},
298-
...defineImportedProgramCommandGroupSpecs([
299-
{
300-
commandNames: ["directory"],
301-
loadModule: () => import("../directory-cli.js"),
302-
exportName: "registerDirectoryCli",
303-
},
304-
{
305-
commandNames: ["security"],
306-
loadModule: () => import("../security-cli.js"),
307-
exportName: "registerSecurityCli",
308-
},
309-
{
310-
commandNames: ["secrets"],
311-
loadModule: () => import("../secrets-cli.js"),
312-
exportName: "registerSecretsCli",
313-
},
314-
{
315-
commandNames: ["skills"],
316-
loadModule: () => import("../skills-cli.js"),
317-
exportName: "registerSkillsCli",
318-
},
319-
{
320-
commandNames: ["update"],
321-
loadModule: () => import("../update-cli.js"),
322-
exportName: "registerUpdateCli",
323-
},
196+
...defineImportedSubCliGroups([
197+
[["directory"], () => import("../directory-cli.js"), "registerDirectoryCli"],
198+
[["security"], () => import("../security-cli.js"), "registerSecurityCli"],
199+
[["secrets"], () => import("../secrets-cli.js"), "registerSecretsCli"],
200+
[["skills"], () => import("../skills-cli.js"), "registerSkillsCli"],
201+
[["update"], () => import("../update-cli.js"), "registerUpdateCli"],
324202
]),
325203
];
326204

0 commit comments

Comments
 (0)