Nuxt/packages/cli/src/index.ts

50 lines
1.1 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'
2021-04-09 15:52:45 +00:00
import { commands } from './commands'
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)
const args = mri(_argv)
// @ts-ignore
let command = args._.shift() || 'usage'
2021-03-28 21:01:51 +00:00
2021-04-15 19:17:44 +00:00
showBanner(command === 'dev')
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(`nu ${Object.keys(commands).join('|')} [args]`)}\n`)
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]()
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
2021-04-09 15:52:45 +00:00
function onFatalError (err) {
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
}