feat(nuxi): expose runCommand (#3749)

This commit is contained in:
pooya parsa 2022-03-17 19:49:59 +01:00 committed by GitHub
parent ed908fe027
commit 621ce975b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 58 deletions

View File

@ -1,3 +1,3 @@
#!/usr/bin/env node
process._startTime = Date.now()
import('../dist/index.mjs').then(r => (r.default || r).main())
import('../dist/cli.mjs').then(r => (r.default || r).main())

View File

@ -7,6 +7,7 @@ export default defineBuildConfig({
cjsBridge: true
},
entries: [
'src/cli',
'src/index'
],
externals: [

57
packages/nuxi/src/cli.ts Executable file
View File

@ -0,0 +1,57 @@
import mri from 'mri'
import { red } from 'colorette'
import consola from 'consola'
import { checkEngines } from './utils/engines'
import { commands, Command, NuxtCommand } from './commands'
import { showHelp } from './utils/help'
import { showBanner } from './utils/banner'
async function _main () {
const _argv = process.argv.slice(2)
const args = mri(_argv, {
boolean: [
'no-clear'
]
})
// @ts-ignore
const command = args._.shift() || 'usage'
showBanner(command === 'dev' && args.clear !== false && !args.help)
if (!(command in commands)) {
console.log('\n' + red('Invalid command ' + command))
await commands.usage().then(r => r.invoke())
process.exit(1)
}
// Check Node.js version in background
setTimeout(() => { checkEngines() }, 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)
}
}
function onFatalError (err: unknown) {
consola.error(err)
process.exit(1)
}
// 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))
export function main () {
_main().catch(onFatalError)
}

View File

@ -1,57 +1 @@
import mri from 'mri'
import { red } from 'colorette'
import consola from 'consola'
import { checkEngines } from './utils/engines'
import { commands, Command, NuxtCommand } from './commands'
import { showHelp } from './utils/help'
import { showBanner } from './utils/banner'
async function _main () {
const _argv = process.argv.slice(2)
const args = mri(_argv, {
boolean: [
'no-clear'
]
})
// @ts-ignore
const command = args._.shift() || 'usage'
showBanner(command === 'dev' && args.clear !== false && !args.help)
if (!(command in commands)) {
console.log('\n' + red('Invalid command ' + command))
await commands.usage().then(r => r.invoke())
process.exit(1)
}
// Check Node.js version in background
setTimeout(() => { checkEngines() }, 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)
}
}
function onFatalError (err: unknown) {
consola.error(err)
process.exit(1)
}
// 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))
export function main () {
_main().catch(onFatalError)
}
export * from './run'

12
packages/nuxi/src/run.ts Normal file
View File

@ -0,0 +1,12 @@
import mri from 'mri'
import { commands, Command, NuxtCommand } from './commands'
export async function runCommand (command: string, argv = process.argv.slice(2)) {
const args = mri(argv)
args.clear = false // used by dev
const cmd = await commands[command as Command]() as NuxtCommand
if (!cmd) {
throw new Error(`Invalid command ${command}`)
}
await cmd.invoke(args)
}