Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(subcommand): add subcommand support #644

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@
import { resolve } from 'node:path'
import process from 'node:process'

import { defineCommand } from 'citty'
import { provider } from 'std-env'
import { x } from 'tinyexec'
danielroe marked this conversation as resolved.
Show resolved Hide resolved

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 +18,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 +42,23 @@ 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 {
// `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()
}
},
})
Loading