Skip to content

Commit 5974e2f

Browse files
committed
refactor(cli): unify the 4 subcommand routers via defineSubcommandGroup
Pre-refactor the four router files (cmd-scan, cmd-package, cmd-manifest, cmd-organization) used three subtly different shapes — one with a top-level config object whose flags were never consumed, one that inlined meowWithSubcommands without a config, one that declared hidden: false explicitly while another omitted it. Add `defineSubcommandGroup` that takes a single declarative spec and emits the same CliSubcommand shape every router used to build by hand. Optional knobs preserve pre-refactor surface drift exactly: - `hidden`: only included on the result when explicitly passed (so cmd-scan, which previously had no `hidden` field, stays that way). - `passCommonFlags`: opt in to passing the shared commonFlags map to meowWithSubcommands (cmd-manifest used this implicitly). - `aliases`: forwarded as-is. Migrations: - cmd-scan, cmd-package, cmd-manifest, cmd-organization now declare their spec via defineSubcommandGroup. Public surface is byte-identical per the existing router unit tests (51 pass with no test changes).
1 parent a11596d commit 5974e2f

5 files changed

Lines changed: 215 additions & 164 deletions

File tree

packages/cli/src/commands/manifest/cmd-manifest.mts

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,29 @@ import { cmdManifestGradle } from './cmd-manifest-gradle.mts'
55
import { cmdManifestKotlin } from './cmd-manifest-kotlin.mts'
66
import { cmdManifestScala } from './cmd-manifest-scala.mts'
77
import { cmdManifestSetup } from './cmd-manifest-setup.mts'
8-
import { defineFlags } from '../../meow.mts'
9-
import { commonFlags } from '../../flags.mts'
10-
import { meowWithSubcommands } from '../../utils/cli/with-subcommands.mjs'
8+
import { defineSubcommandGroup } from '../../utils/cli/define-subcommand-group.mts'
119

12-
import type { CliCommandContext } from '../../utils/cli/with-subcommands.mjs'
10+
const description = 'Generate a dependency manifest for certain ecosystems'
1311

14-
// meowWithSubcommands renders its own help text from the subcommand list,
15-
// so this command-level config doesn't need a help builder.
16-
const config = {
17-
commandName: 'manifest',
18-
description: 'Generate a dependency manifest for certain ecosystems',
12+
export const cmdManifest = defineSubcommandGroup({
13+
name: 'manifest',
14+
description,
1915
hidden: false,
20-
flags: defineFlags({
21-
...commonFlags,
22-
}),
23-
}
24-
25-
export const cmdManifest = {
26-
description: config.description,
27-
hidden: config.hidden,
28-
run,
29-
}
30-
31-
async function run(
32-
argv: string[] | readonly string[],
33-
importMeta: ImportMeta,
34-
{ parentName }: CliCommandContext,
35-
): Promise<void> {
36-
await meowWithSubcommands(
37-
{
38-
argv,
39-
name: `${parentName} ${config.commandName}`,
40-
importMeta,
41-
subcommands: {
42-
auto: cmdManifestAuto,
43-
cdxgen: cmdManifestCdxgen,
44-
conda: cmdManifestConda,
45-
gradle: cmdManifestGradle,
46-
kotlin: cmdManifestKotlin,
47-
scala: cmdManifestScala,
48-
setup: cmdManifestSetup,
49-
},
50-
},
51-
{
52-
aliases: {
53-
yolo: {
54-
description: config.description,
55-
hidden: true,
56-
argv: ['auto'],
57-
},
58-
},
59-
description: config.description,
60-
flags: config.flags,
16+
passCommonFlags: true,
17+
subcommands: {
18+
auto: cmdManifestAuto,
19+
cdxgen: cmdManifestCdxgen,
20+
conda: cmdManifestConda,
21+
gradle: cmdManifestGradle,
22+
kotlin: cmdManifestKotlin,
23+
scala: cmdManifestScala,
24+
setup: cmdManifestSetup,
25+
},
26+
aliases: {
27+
yolo: {
28+
description,
29+
hidden: true,
30+
argv: ['auto'],
6131
},
62-
)
63-
}
32+
},
33+
})

packages/cli/src/commands/organization/cmd-organization.mts

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,33 @@ import { cmdOrganizationPolicyLicense } from './cmd-organization-policy-license.
44
import { cmdOrganizationPolicySecurity } from './cmd-organization-policy-security.mts'
55
import { cmdOrganizationPolicy } from './cmd-organization-policy.mts'
66
import { cmdOrganizationQuota } from './cmd-organization-quota.mts'
7-
import { meowWithSubcommands } from '../../utils/cli/with-subcommands.mjs'
7+
import { defineSubcommandGroup } from '../../utils/cli/define-subcommand-group.mts'
88

9-
import type { CliSubcommand } from '../../utils/cli/with-subcommands.mjs'
10-
11-
const description = 'Manage Socket organization account details'
12-
13-
export const cmdOrganization: CliSubcommand = {
14-
description,
9+
export const cmdOrganization = defineSubcommandGroup({
10+
name: 'organization',
11+
description: 'Manage Socket organization account details',
1512
hidden: false,
16-
async run(argv, importMeta, { parentName }) {
17-
await meowWithSubcommands(
18-
{
19-
argv,
20-
name: `${parentName} organization`,
21-
importMeta,
22-
subcommands: {
23-
dependencies: cmdOrganizationDependencies,
24-
list: cmdOrganizationList,
25-
quota: cmdOrganizationQuota,
26-
policy: cmdOrganizationPolicy,
27-
},
28-
},
29-
{
30-
aliases: {
31-
deps: {
32-
description: cmdOrganizationDependencies.description,
33-
hidden: true,
34-
argv: ['dependencies'],
35-
},
36-
license: {
37-
description: cmdOrganizationPolicyLicense.description,
38-
hidden: true,
39-
argv: ['policy', 'license'],
40-
},
41-
security: {
42-
description: cmdOrganizationPolicySecurity.description,
43-
hidden: true,
44-
argv: ['policy', 'security'],
45-
},
46-
},
47-
description,
48-
},
49-
)
13+
subcommands: {
14+
dependencies: cmdOrganizationDependencies,
15+
list: cmdOrganizationList,
16+
quota: cmdOrganizationQuota,
17+
policy: cmdOrganizationPolicy,
18+
},
19+
aliases: {
20+
deps: {
21+
description: cmdOrganizationDependencies.description,
22+
hidden: true,
23+
argv: ['dependencies'],
24+
},
25+
license: {
26+
description: cmdOrganizationPolicyLicense.description,
27+
hidden: true,
28+
argv: ['policy', 'license'],
29+
},
30+
security: {
31+
description: cmdOrganizationPolicySecurity.description,
32+
hidden: true,
33+
argv: ['policy', 'security'],
34+
},
5035
},
51-
}
36+
})
Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
import { cmdPackageScore } from './cmd-package-score.mts'
22
import { cmdPackageShallow } from './cmd-package-shallow.mts'
3-
import { meowWithSubcommands } from '../../utils/cli/with-subcommands.mjs'
4-
5-
import type { CliSubcommand } from '../../utils/cli/with-subcommands.mjs'
3+
import { defineSubcommandGroup } from '../../utils/cli/define-subcommand-group.mts'
64

75
const description = 'Look up published package details'
86

9-
export const cmdPackage: CliSubcommand = {
7+
export const cmdPackage = defineSubcommandGroup({
8+
name: 'package',
109
description,
1110
hidden: false,
12-
async run(argv, importMeta, { parentName }) {
13-
await meowWithSubcommands(
14-
{
15-
argv,
16-
name: `${parentName} package`,
17-
importMeta,
18-
subcommands: {
19-
score: cmdPackageScore,
20-
shallow: cmdPackageShallow,
21-
},
22-
},
23-
{
24-
aliases: {
25-
deep: {
26-
description,
27-
hidden: true,
28-
argv: ['score'],
29-
},
30-
},
31-
description,
32-
},
33-
)
11+
subcommands: {
12+
score: cmdPackageScore,
13+
shallow: cmdPackageShallow,
14+
},
15+
aliases: {
16+
deep: {
17+
description,
18+
hidden: true,
19+
argv: ['score'],
20+
},
3421
},
35-
}
22+
})

packages/cli/src/commands/scan/cmd-scan.mts

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,33 @@ import { cmdScanReach } from './cmd-scan-reach.mts'
88
import { cmdScanReport } from './cmd-scan-report.mts'
99
import { cmdScanSetup } from './cmd-scan-setup.mts'
1010
import { cmdScanView } from './cmd-scan-view.mts'
11-
import { meowWithSubcommands } from '../../utils/cli/with-subcommands.mjs'
11+
import { defineSubcommandGroup } from '../../utils/cli/define-subcommand-group.mts'
1212

13-
import type { CliSubcommand } from '../../utils/cli/with-subcommands.mjs'
14-
15-
const description = 'Manage Socket scans'
16-
17-
export const cmdScan: CliSubcommand = {
18-
description,
19-
async run(argv, importMeta, { parentName }) {
20-
await meowWithSubcommands(
21-
{
22-
argv,
23-
name: `${parentName} scan`,
24-
importMeta,
25-
subcommands: {
26-
create: cmdScanCreate,
27-
del: cmdScanDel,
28-
diff: cmdScanDiff,
29-
github: cmdScanGithub,
30-
list: cmdScanList,
31-
metadata: cmdScanMetadata,
32-
reach: cmdScanReach,
33-
report: cmdScanReport,
34-
setup: cmdScanSetup,
35-
view: cmdScanView,
36-
},
37-
},
38-
{
39-
aliases: {
40-
meta: {
41-
description: cmdScanMetadata.description,
42-
hidden: true,
43-
argv: ['metadata'],
44-
},
45-
reachability: {
46-
description: cmdScanReach.description,
47-
hidden: true,
48-
argv: ['reach'],
49-
},
50-
},
51-
description,
52-
},
53-
)
13+
export const cmdScan = defineSubcommandGroup({
14+
name: 'scan',
15+
description: 'Manage Socket scans',
16+
subcommands: {
17+
create: cmdScanCreate,
18+
del: cmdScanDel,
19+
diff: cmdScanDiff,
20+
github: cmdScanGithub,
21+
list: cmdScanList,
22+
metadata: cmdScanMetadata,
23+
reach: cmdScanReach,
24+
report: cmdScanReport,
25+
setup: cmdScanSetup,
26+
view: cmdScanView,
27+
},
28+
aliases: {
29+
meta: {
30+
description: cmdScanMetadata.description,
31+
hidden: true,
32+
argv: ['metadata'],
33+
},
34+
reachability: {
35+
description: cmdScanReach.description,
36+
hidden: true,
37+
argv: ['reach'],
38+
},
5439
},
55-
}
40+
})

0 commit comments

Comments
 (0)