Skip to content

Commit

Permalink
feat(subcommand): add subcommand support (#644)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jan 6, 2025
1 parent 00a0a71 commit dacb6c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 81 deletions.
63 changes: 0 additions & 63 deletions src/commands/build-module.ts

This file was deleted.

35 changes: 17 additions & 18 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import type { CommandDef } from 'citty'
const _rDefault = (r: any) => (r.default || r) as Promise<CommandDef>

export const commands = {
'add': () => import('./add').then(_rDefault),
'analyze': () => import('./analyze').then(_rDefault),
'build-module': () => import('./build-module').then(_rDefault),
'build': () => import('./build').then(_rDefault),
'cleanup': () => import('./cleanup').then(_rDefault),
'_dev': () => import('./dev-child').then(_rDefault),
'dev': () => import('./dev').then(_rDefault),
'devtools': () => import('./devtools').then(_rDefault),
'generate': () => import('./generate').then(_rDefault),
'info': () => import('./info').then(_rDefault),
'init': () => import('./init').then(_rDefault),
'module': () => import('./module').then(_rDefault),
'prepare': () => import('./prepare').then(_rDefault),
'preview': () => import('./preview').then(_rDefault),
'start': () => import('./preview').then(_rDefault),
'test': () => import('./test').then(_rDefault),
'typecheck': () => import('./typecheck').then(_rDefault),
'upgrade': () => import('./upgrade').then(_rDefault),
add: () => import('./add').then(_rDefault),
analyze: () => import('./analyze').then(_rDefault),
build: () => import('./build').then(_rDefault),
cleanup: () => import('./cleanup').then(_rDefault),
_dev: () => import('./dev-child').then(_rDefault),
dev: () => import('./dev').then(_rDefault),
devtools: () => import('./devtools').then(_rDefault),
generate: () => import('./generate').then(_rDefault),
info: () => import('./info').then(_rDefault),
init: () => import('./init').then(_rDefault),
module: () => import('./module').then(_rDefault),
prepare: () => import('./prepare').then(_rDefault),
preview: () => import('./preview').then(_rDefault),
start: () => import('./preview').then(_rDefault),
test: () => import('./test').then(_rDefault),
typecheck: () => import('./typecheck').then(_rDefault),
upgrade: () => import('./upgrade').then(_rDefault),
} as const
30 changes: 30 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { resolve } from 'node:path'
import process from 'node:process'

import { defineCommand } from 'citty'
import { provider } from 'std-env'

import nuxiPkg from '../package.json' assert { type: 'json' }
import { commands } from './commands'
import { cwdArgs } from './commands/_shared'
import { setupGlobalConsole } from './utils/console'
import { checkEngines } from './utils/engines'
import { logger } from './utils/logger'
Expand All @@ -12,6 +17,12 @@ export const main = defineCommand({
version: nuxiPkg.version,
description: nuxiPkg.description,
},
args: {
...cwdArgs,
command: {
type: 'positional',
},
},
subCommands: commands,
async setup(ctx) {
const command = ctx.args._[0]
Expand All @@ -30,5 +41,24 @@ export const main = defineCommand({
if (command === 'init') {
await backgroundTasks
}

// allow running arbitrary commands if there's a locally registered binary with `nuxt-` prefix
if (ctx.args.command && !(ctx.args.command in commands)) {
const cwd = resolve(ctx.args.cwd)
try {
const { x } = await import('tinyexec')
// `tinyexec` will resolve command from local binaries
await x(`nuxt-${ctx.args.command}`, ctx.args._.slice(1), {
nodeOptions: { stdio: 'inherit', cwd },
throwOnError: true,
})
}
catch (err) {
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
return
}
}
process.exit()
}
},
})

0 comments on commit dacb6c0

Please sign in to comment.