2020-10-13 10:20:04 +00:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { resolve } from 'path'
|
2018-12-20 11:15:48 +00:00
|
|
|
import execa from 'execa'
|
2020-10-13 10:20:04 +00:00
|
|
|
import consola from 'consola'
|
2019-07-04 12:58:07 +00:00
|
|
|
import { name as pkgName } from '../package.json'
|
2018-10-29 22:16:16 +00:00
|
|
|
import NuxtCommand from './command'
|
2018-10-25 07:43:42 +00:00
|
|
|
import setup from './setup'
|
2018-12-20 11:15:48 +00:00
|
|
|
import getCommand from './commands'
|
2020-08-05 20:23:25 +00:00
|
|
|
import { isNuxtDir } from './utils/dir'
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2019-08-24 14:15:08 +00:00
|
|
|
export default async function run (_argv, hooks = {}) {
|
2019-07-10 09:57:31 +00:00
|
|
|
// Check for not installing both nuxt and nuxt-edge
|
2020-10-13 10:20:04 +00:00
|
|
|
const dupPkg = pkgName === '@nuxt/cli-edge' ? 'cli' : 'cli-edge'
|
|
|
|
const dupPkgJSON = resolve(__dirname, '../..' /* dist/../.. */, dupPkg, 'package.json')
|
|
|
|
if (existsSync(dupPkgJSON) && require(dupPkgJSON).name !== '@nuxt/' + dupPkg) {
|
|
|
|
consola.warn('Both `nuxt` and `nuxt-edge` dependencies are installed! Please choose one and remove the other one from dependencies.')
|
2019-07-10 09:57:31 +00:00
|
|
|
}
|
2019-07-04 12:58:07 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
// Read from process.argv
|
|
|
|
const argv = _argv ? Array.from(_argv) : process.argv.slice(2)
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
// Check for internal command
|
|
|
|
let cmd = await getCommand(argv[0])
|
|
|
|
|
|
|
|
// Matching `nuxt` or `nuxt [dir]` or `nuxt -*` for `nuxt dev` shortcut
|
2020-08-05 20:23:25 +00:00
|
|
|
if (!cmd && (!argv[0] || argv[0][0] === '-' || isNuxtDir(argv[0]))) {
|
2018-12-20 11:15:48 +00:00
|
|
|
argv.unshift('dev')
|
|
|
|
cmd = await getCommand('dev')
|
2018-10-25 07:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 14:15:08 +00:00
|
|
|
// Check for dev
|
|
|
|
const dev = argv[0] === 'dev'
|
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
// Setup env
|
2019-08-24 14:15:08 +00:00
|
|
|
setup({ dev })
|
2018-12-20 11:15:48 +00:00
|
|
|
|
|
|
|
// Try internal command
|
|
|
|
if (cmd) {
|
2019-08-24 14:15:08 +00:00
|
|
|
return NuxtCommand.run(cmd, argv.slice(1), hooks)
|
2018-12-20 11:15:48 +00:00
|
|
|
}
|
2018-10-25 07:43:42 +00:00
|
|
|
|
2018-12-20 11:15:48 +00:00
|
|
|
// Try external command
|
|
|
|
try {
|
|
|
|
await execa(`nuxt-${argv[0]}`, argv.slice(1), {
|
|
|
|
stdout: process.stdout,
|
|
|
|
stderr: process.stderr,
|
|
|
|
stdin: process.stdin
|
2018-10-25 07:43:42 +00:00
|
|
|
})
|
2018-12-20 11:15:48 +00:00
|
|
|
} catch (error) {
|
2019-06-25 15:48:53 +00:00
|
|
|
if (error.exitCode === 2) {
|
2018-12-20 11:15:48 +00:00
|
|
|
throw String(`Command not found: nuxt-${argv[0]}`)
|
|
|
|
}
|
2019-02-08 16:25:11 +00:00
|
|
|
throw String(`Failed to run command \`nuxt-${argv[0]}\`:\n${error}`)
|
2018-12-20 11:15:48 +00:00
|
|
|
}
|
2018-10-25 07:43:42 +00:00
|
|
|
}
|