Nuxt/packages/nuxi/src/index.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-04-09 15:52:45 +00:00
import 'v8-compile-cache'
import mri from 'mri'
2021-04-15 19:17:44 +00:00
import { red, cyan } from 'colorette'
import { commands, Command, NuxtCommand } from './commands'
2021-04-09 15:52:45 +00:00
import { showHelp } from './utils/help'
2021-04-15 19:17:44 +00:00
import { showBanner } from './utils/banner'
import { error } from './utils/log'
2021-01-20 14:43:43 +00:00
async function _main () {
2021-04-09 15:52:45 +00:00
const _argv = process.argv.slice(2)
2021-07-02 12:45:15 +00:00
const args = mri(_argv, {
boolean: [
'no-clear'
]
})
2021-04-09 15:52:45 +00:00
// @ts-ignore
let command = args._.shift() || 'usage'
2021-03-28 21:01:51 +00:00
2021-07-02 12:45:15 +00:00
showBanner(command === 'dev' && args.clear !== false)
2021-03-28 21:01:51 +00:00
2021-04-09 15:52:45 +00:00
if (!(command in commands)) {
console.log('\n' + red('Invalid command ' + command))
command = 'usage'
}
2021-03-28 21:01:51 +00:00
2021-04-09 15:52:45 +00:00
if (command === 'usage') {
console.log(`\nUsage: ${cyan(`npx nuxi ${Object.keys(commands).join('|')} [args]`)}\n`)
2021-04-09 15:52:45 +00:00
process.exit(1)
}
2021-01-20 15:44:52 +00:00
2021-04-09 15:52:45 +00:00
try {
const cmd = await commands[command as Command]().then(c => c.default || c) as NuxtCommand
2021-04-09 15:52:45 +00:00
if (args.h || args.help) {
showHelp(cmd.meta)
} else {
await cmd.invoke(args)
}
} catch (err) {
onFatalError(err)
2021-01-20 15:44:52 +00:00
}
2021-04-09 15:52:45 +00:00
}
2021-01-20 15:44:52 +00:00
function onFatalError (err: unknown) {
2021-04-15 19:17:44 +00:00
error(err)
2021-04-09 15:52:45 +00:00
process.exit(1)
2021-01-20 14:43:43 +00:00
}
2021-04-15 19:17:44 +00:00
process.on('unhandledRejection', err => error('[unhandledRejection]', err))
process.on('uncaughtException', err => error('[uncaughtException]', err))
2021-01-20 14:43:43 +00:00
export function main () {
2021-04-09 15:52:45 +00:00
_main().catch(onFatalError)
2021-01-20 14:43:43 +00:00
}