Skip to content

Commit be103a2

Browse files
committed
refactor(buddy): replace 43 duplicate ':*' handlers with onUnknownSubcommand
Every command file used to inline the same 5-line buddy.on(':*') boilerplate with slight variations (some used console.error, some log.error, exit codes drifted between 1 and ExitCode.FatalError). This sweep replaces all 43 call sites with onUnknownSubcommand(buddy, prefix), which: - Emits a consistent 'Unknown <prefix> subcommand: …' message - Includes a 'Run `buddy <prefix> --help`' hint - Exits with code 64 (EX_USAGE per <sysexits.h>) so CI/scripts can distinguish 'user typed an unknown subcommand' from real failures without parsing stderr The helper itself was added in the prior commit but had no consumers.
1 parent 658da7b commit be103a2

44 files changed

Lines changed: 91 additions & 174 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

storage/framework/core/buddy/src/commands/_helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ import process from 'node:process'
2929
*/
3030
export function onUnknownSubcommand(buddy: CLI, prefix: string): void {
3131
buddy.on(`${prefix}:*`, () => {
32-
const args = (buddy as { args?: string[] }).args ?? []
32+
// @stacksjs/clapp exposes `args` at runtime but the public CLI type doesn't
33+
// surface it. Widening through `unknown` is the canonical "I know
34+
// better than the type system here" escape hatch and only runs in
35+
// the error-path so the cost is irrelevant.
36+
const args = (buddy as unknown as { args?: string[] }).args ?? []
3337
process.stderr.write(
3438
`Unknown ${prefix} subcommand: ${args.join(' ')}\n`
3539
+ `Run \`buddy ${prefix} --help\` to see available subcommands.\n`,

storage/framework/core/buddy/src/commands/about.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CLI } from '@stacksjs/types'
22
import process from 'node:process'
33
import { bold, dim, green, intro, log } from '@stacksjs/cli'
44
import { storage } from '@stacksjs/storage'
5+
import { onUnknownSubcommand } from './_helpers'
56

67
export function about(buddy: CLI): void {
78
buddy
@@ -59,8 +60,5 @@ export function about(buddy: CLI): void {
5960
}
6061
})
6162

62-
buddy.on('about:*', () => {
63-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
64-
process.exit(1)
65-
})
63+
onUnknownSubcommand(buddy, "about")
6664
}

storage/framework/core/buddy/src/commands/add.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process'
33
import { runAdd } from '@stacksjs/actions'
44
import { log } from '@stacksjs/logging'
55
import { ExitCode } from '@stacksjs/types'
6+
import { onUnknownSubcommand } from './_helpers'
67

78
export function add(buddy: CLI): void {
89
const descriptions = {
@@ -60,10 +61,7 @@ export function add(buddy: CLI): void {
6061
await runAdd(options)
6162
})
6263

63-
buddy.on('add:*', () => {
64-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
65-
process.exit(1)
66-
})
64+
onUnknownSubcommand(buddy, "add")
6765
}
6866

6967
function hasNoOptions(options: AddOptions) {

storage/framework/core/buddy/src/commands/auth.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { runAction } from '@stacksjs/actions'
44
import { intro, log, outro } from '@stacksjs/cli'
55
import { Action } from '@stacksjs/enums'
66
import { ExitCode } from '@stacksjs/types'
7+
import { onUnknownSubcommand } from './_helpers'
78

89
export function auth(buddy: CLI): void {
910
const descriptions = {
@@ -130,8 +131,5 @@ export function auth(buddy: CLI): void {
130131
process.exit(ExitCode.Success)
131132
})
132133

133-
buddy.on('auth:*', () => {
134-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
135-
process.exit(1)
136-
})
134+
onUnknownSubcommand(buddy, "auth")
137135
}

storage/framework/core/buddy/src/commands/build.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import process from 'node:process'
33
import { intro, log, outro } from '@stacksjs/cli'
44
import { Action } from '@stacksjs/enums'
55
import { ExitCode } from '@stacksjs/types'
6+
import { onUnknownSubcommand } from './_helpers'
67

78
// Lazy-load @stacksjs/actions — importing it at module level forces every
89
// `buddy <anything>` invocation to resolve the actions barrel before
@@ -242,10 +243,7 @@ export function build(buddy: CLI): void {
242243
await outro('Stacks built successfully', { startTime, useSeconds: true })
243244
})
244245

245-
buddy.on('build:*', () => {
246-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
247-
process.exit(1)
248-
})
246+
onUnknownSubcommand(buddy, "build")
249247
}
250248

251249
function hasNoOptions(options: BuildOptions) {

storage/framework/core/buddy/src/commands/changelog.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { runAction } from '@stacksjs/actions'
44
import { intro, log, outro } from '@stacksjs/cli'
55
import { Action } from '@stacksjs/enums'
66
import { ExitCode } from '@stacksjs/types'
7+
import { onUnknownSubcommand } from './_helpers'
78

89
export function changelog(buddy: CLI): void {
910
const descriptions = {
@@ -45,9 +46,5 @@ export function changelog(buddy: CLI): void {
4546
})
4647
})
4748

48-
buddy.on('changelog:*', () => {
49-
console.log('Invalid command: %s', buddy.args.join(' '))
50-
console.log('See --help for a list of available commands.')
51-
process.exit(1)
52-
})
49+
onUnknownSubcommand(buddy, 'changelog')
5350
}

storage/framework/core/buddy/src/commands/clean.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { runAction } from '@stacksjs/actions'
44
import { intro, log, outro } from '@stacksjs/cli'
55
import { Action } from '@stacksjs/enums'
66
import { ExitCode } from '@stacksjs/types'
7+
import { onUnknownSubcommand } from './_helpers'
78

89
export function clean(buddy: CLI): void {
910
const descriptions = {
@@ -53,8 +54,5 @@ export function clean(buddy: CLI): void {
5354
process.exit(ExitCode.Success)
5455
})
5556

56-
buddy.on('clean:*', () => {
57-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
58-
process.exit(1)
59-
})
57+
onUnknownSubcommand(buddy, "clean")
6058
}

storage/framework/core/buddy/src/commands/cloud.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '@stacksjs/cloud'
1818
import { path as p } from '@stacksjs/path'
1919
import { ExitCode } from '@stacksjs/types'
20+
import { onUnknownSubcommand } from './_helpers'
2021

2122
/**
2223
* Create a temporary IAM role to allow CloudFormation to delete a stuck stack
@@ -742,8 +743,5 @@ export function cloud(buddy: CLI): void {
742743
process.exit(ExitCode.Success)
743744
})
744745

745-
buddy.on('cloud:*', () => {
746-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
747-
process.exit(1)
748-
})
746+
onUnknownSubcommand(buddy, "cloud")
749747
}

storage/framework/core/buddy/src/commands/commit.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { CLI, FreshOptions } from '@stacksjs/types'
22
import process from 'node:process'
33
import { runCommit } from '@stacksjs/actions'
44
import { log } from '@stacksjs/logging'
5+
import { onUnknownSubcommand } from './_helpers'
56

67
export function commit(buddy: CLI): void {
78
const descriptions = {
@@ -19,8 +20,5 @@ export function commit(buddy: CLI): void {
1920
await runCommit(options)
2021
})
2122

22-
buddy.on('commit:*', () => {
23-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
24-
process.exit(1)
25-
})
23+
onUnknownSubcommand(buddy, "commit")
2624
}

storage/framework/core/buddy/src/commands/completion.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CLI } from '@stacksjs/types'
22
import process from 'node:process'
33
import { log } from '@stacksjs/cli'
4+
import { onUnknownSubcommand } from './_helpers'
45

56
export function completion(buddy: CLI): void {
67
const descriptions = {
@@ -41,10 +42,7 @@ export function completion(buddy: CLI): void {
4142
}
4243
})
4344

44-
buddy.on('completion:*', () => {
45-
console.error('Invalid command: %s\nSee --help for a list of available commands.', buddy.args.join(' '))
46-
process.exit(1)
47-
})
45+
onUnknownSubcommand(buddy, "completion")
4846
}
4947

5048
function generateBashCompletion(commands: string[]): string {

0 commit comments

Comments
 (0)