mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxi): expose runCommand
(#3749)
This commit is contained in:
parent
ed908fe027
commit
621ce975b4
@ -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())
|
||||
|
@ -7,6 +7,7 @@ export default defineBuildConfig({
|
||||
cjsBridge: true
|
||||
},
|
||||
entries: [
|
||||
'src/cli',
|
||||
'src/index'
|
||||
],
|
||||
externals: [
|
||||
|
57
packages/nuxi/src/cli.ts
Executable file
57
packages/nuxi/src/cli.ts
Executable 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)
|
||||
}
|
@ -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
12
packages/nuxi/src/run.ts
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user