Nuxt/packages/cli/src/index.ts

46 lines
997 B
TypeScript
Raw Normal View History

2021-04-09 15:52:45 +00:00
import 'v8-compile-cache'
import mri from 'mri'
import { red, cyan, green } from 'colorette'
import { version } from '../package.json'
import { commands } from './commands'
import { showHelp } from './utils/help'
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-09 15:52:45 +00:00
console.log(green(`Nuxt CLI v${version}`))
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) {
console.error(err)
process.exit(1)
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
}