Nuxt/packages/cli/src/run.js
Alexander Lichter 69dfd848d7 refactor: some small stuff (#4979)
* refactor: flatten ifs

* refactor: unnecessary curly brackets

* refactor: unnecessary else

* refactor: promise.all instead of for-await

* refactor: apply changes suggested by @clarkdo

* chore: fix typo

* refactor: early return

* refactor: add removal TODOs

* refactor: more descriptive variable name

* refactor: prefer template string

* refactor: one-line

* refactor: early returns

* refactor: early return

* refactor: parallel promises

* refactor: early return and no else

* refactor: spread operator

* refactor: spread operator and early return

* fix: remove error and throw string instead

* fix: always return true

* refactor: clear multiline ternary

* refactor: err stack assignment

* refactor: early return and async/await

* refactor: one line

* refactor: early return

* refactor: early return

* refactor: promise.all

* refactor: args spread
2019-02-08 17:25:11 +01:00

42 lines
1.1 KiB
JavaScript

import fs from 'fs'
import execa from 'execa'
import NuxtCommand from './command'
import setup from './setup'
import getCommand from './commands'
export default async function run(_argv) {
// Read from process.argv
const argv = _argv ? Array.from(_argv) : process.argv.slice(2)
// Check for internal command
let cmd = await getCommand(argv[0])
// Matching `nuxt` or `nuxt [dir]` or `nuxt -*` for `nuxt dev` shortcut
if (!cmd && (!argv[0] || argv[0][0] === '-' || fs.existsSync(argv[0]))) {
argv.unshift('dev')
cmd = await getCommand('dev')
}
// Setup env
setup({ dev: argv[0] === 'dev' })
// Try internal command
if (cmd) {
return NuxtCommand.run(cmd, argv.slice(1))
}
// Try external command
try {
await execa(`nuxt-${argv[0]}`, argv.slice(1), {
stdout: process.stdout,
stderr: process.stderr,
stdin: process.stdin
})
} catch (error) {
if (error.code === 'ENOENT') {
throw String(`Command not found: nuxt-${argv[0]}`)
}
throw String(`Failed to run command \`nuxt-${argv[0]}\`:\n${error}`)
}
}