fix(cli): close process by default after command invokation (#4192)

This commit is contained in:
pooya parsa 2022-04-09 12:02:56 +02:00 committed by GitHub
parent edd0b30896
commit 396472778f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 18 deletions

View File

@ -26,26 +26,18 @@ async function _main () {
}
// Check Node.js version in background
setTimeout(() => { checkEngines() }, 1000)
setTimeout(() => { checkEngines().catch(() => {}) }, 1000)
try {
// @ts-ignore default.default is hotfix for #621
const cmd = await commands[command as Command]() as NuxtCommand
if (args.h || args.help) {
showHelp(cmd.meta)
} else {
await cmd.invoke(args)
}
} catch (err) {
onFatalError(err)
// @ts-ignore default.default is hotfix for #621
const cmd = await commands[command as Command]() as NuxtCommand
if (args.h || args.help) {
showHelp(cmd.meta)
} else {
const result = await cmd.invoke(args)
return result
}
}
function onFatalError (err: unknown) {
consola.error(err)
process.exit(1)
}
// Wrap all console logs with consola for better DX
consola.wrapConsole()
@ -53,5 +45,16 @@ process.on('unhandledRejection', err => consola.error('[unhandledRejection]', er
process.on('uncaughtException', err => consola.error('[uncaughtException]', err))
export function main () {
_main().catch(onFatalError)
_main()
.then((result) => {
if (result === 'error') {
process.exit(1)
} else if (result !== 'wait') {
process.exit(0)
}
})
.catch((error) => {
consola.error(error)
process.exit(1)
})
}

View File

@ -108,5 +108,7 @@ export default defineNuxtCommand({
if (currentNuxt) {
await currentNuxt.hooks.callHook('listen', listener.server, listener)
}
return 'wait'
}
})

View File

@ -30,8 +30,10 @@ export interface NuxtCommandMeta {
[key: string]: any;
}
export type CLIInvokeResult = void | 'error' | 'wait'
export interface NuxtCommand {
invoke(args: Argv): Promise<void> | void
invoke(args: Argv): Promise<CLIInvokeResult> | CLIInvokeResult
meta: NuxtCommandMeta
}

View File

@ -16,6 +16,10 @@ export default defineNuxtCommand({
dev: !!args.dev,
watch: !!args.watch
})
if (args.watch) {
return 'wait'
}
}
})