Skip to content

Commit dacb6c0

Browse files
authored
feat(subcommand): add subcommand support (#644)
1 parent 00a0a71 commit dacb6c0

File tree

3 files changed

+47
-81
lines changed

3 files changed

+47
-81
lines changed

src/commands/build-module.ts

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/commands/index.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@ import type { CommandDef } from 'citty'
33
const _rDefault = (r: any) => (r.default || r) as Promise<CommandDef>
44

55
export const commands = {
6-
'add': () => import('./add').then(_rDefault),
7-
'analyze': () => import('./analyze').then(_rDefault),
8-
'build-module': () => import('./build-module').then(_rDefault),
9-
'build': () => import('./build').then(_rDefault),
10-
'cleanup': () => import('./cleanup').then(_rDefault),
11-
'_dev': () => import('./dev-child').then(_rDefault),
12-
'dev': () => import('./dev').then(_rDefault),
13-
'devtools': () => import('./devtools').then(_rDefault),
14-
'generate': () => import('./generate').then(_rDefault),
15-
'info': () => import('./info').then(_rDefault),
16-
'init': () => import('./init').then(_rDefault),
17-
'module': () => import('./module').then(_rDefault),
18-
'prepare': () => import('./prepare').then(_rDefault),
19-
'preview': () => import('./preview').then(_rDefault),
20-
'start': () => import('./preview').then(_rDefault),
21-
'test': () => import('./test').then(_rDefault),
22-
'typecheck': () => import('./typecheck').then(_rDefault),
23-
'upgrade': () => import('./upgrade').then(_rDefault),
6+
add: () => import('./add').then(_rDefault),
7+
analyze: () => import('./analyze').then(_rDefault),
8+
build: () => import('./build').then(_rDefault),
9+
cleanup: () => import('./cleanup').then(_rDefault),
10+
_dev: () => import('./dev-child').then(_rDefault),
11+
dev: () => import('./dev').then(_rDefault),
12+
devtools: () => import('./devtools').then(_rDefault),
13+
generate: () => import('./generate').then(_rDefault),
14+
info: () => import('./info').then(_rDefault),
15+
init: () => import('./init').then(_rDefault),
16+
module: () => import('./module').then(_rDefault),
17+
prepare: () => import('./prepare').then(_rDefault),
18+
preview: () => import('./preview').then(_rDefault),
19+
start: () => import('./preview').then(_rDefault),
20+
test: () => import('./test').then(_rDefault),
21+
typecheck: () => import('./typecheck').then(_rDefault),
22+
upgrade: () => import('./upgrade').then(_rDefault),
2423
} as const

src/main.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { resolve } from 'node:path'
2+
import process from 'node:process'
3+
14
import { defineCommand } from 'citty'
25
import { provider } from 'std-env'
6+
37
import nuxiPkg from '../package.json' assert { type: 'json' }
48
import { commands } from './commands'
9+
import { cwdArgs } from './commands/_shared'
510
import { setupGlobalConsole } from './utils/console'
611
import { checkEngines } from './utils/engines'
712
import { logger } from './utils/logger'
@@ -12,6 +17,12 @@ export const main = defineCommand({
1217
version: nuxiPkg.version,
1318
description: nuxiPkg.description,
1419
},
20+
args: {
21+
...cwdArgs,
22+
command: {
23+
type: 'positional',
24+
},
25+
},
1526
subCommands: commands,
1627
async setup(ctx) {
1728
const command = ctx.args._[0]
@@ -30,5 +41,24 @@ export const main = defineCommand({
3041
if (command === 'init') {
3142
await backgroundTasks
3243
}
44+
45+
// allow running arbitrary commands if there's a locally registered binary with `nuxt-` prefix
46+
if (ctx.args.command && !(ctx.args.command in commands)) {
47+
const cwd = resolve(ctx.args.cwd)
48+
try {
49+
const { x } = await import('tinyexec')
50+
// `tinyexec` will resolve command from local binaries
51+
await x(`nuxt-${ctx.args.command}`, ctx.args._.slice(1), {
52+
nodeOptions: { stdio: 'inherit', cwd },
53+
throwOnError: true,
54+
})
55+
}
56+
catch (err) {
57+
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
58+
return
59+
}
60+
}
61+
process.exit()
62+
}
3363
},
3464
})

0 commit comments

Comments
 (0)