Nuxt/packages/nuxi/src/index.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-04-09 15:52:45 +00:00
import mri from 'mri'
2021-04-15 19:17:44 +00:00
import { red, cyan } from 'colorette'
import consola from 'consola'
import { checkEngines } from './utils/engines'
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'
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
// Check Node.js version in background
setTimeout(() => { checkEngines() }, 1000)
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 {
// @ts-ignore default.default is hotfix for #621
const cmd = await commands[command as Command]() 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) {
consola.error(err)
2021-04-09 15:52:45 +00:00
process.exit(1)
2021-01-20 14:43:43 +00:00
}
// Wrap all console logs with consola for better DX
consola.wrapConsole()
process.on('unhandledRejection', err => consola.error('[unhandledRejection]', err))
process.on('uncaughtException', err => consola.error('[uncaughtException]', err))
2021-04-15 19:17:44 +00:00
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
}