2018-10-17 21:28:25 +00:00
|
|
|
import parseArgs from 'minimist'
|
|
|
|
import consola from 'consola'
|
2018-10-18 15:57:35 +00:00
|
|
|
import { loadNuxtConfig, runAsyncScript } from '../common/utils'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
|
|
|
export default async function dev() {
|
|
|
|
const { Nuxt } = await import('@nuxt/core')
|
|
|
|
const { Builder } = await import('@nuxt/builder')
|
|
|
|
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
|
|
|
alias: {
|
|
|
|
h: 'help',
|
|
|
|
H: 'hostname',
|
|
|
|
p: 'port',
|
|
|
|
c: 'config-file',
|
|
|
|
s: 'spa',
|
|
|
|
u: 'universal',
|
|
|
|
v: 'version'
|
|
|
|
},
|
|
|
|
boolean: ['h', 's', 'u', 'v'],
|
|
|
|
string: ['H', 'c'],
|
|
|
|
default: {
|
|
|
|
c: 'nuxt.config.js'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (argv.version) {
|
|
|
|
process.stderr.write('TODO' + '\n')
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.hostname === '') {
|
|
|
|
consola.fatal('Provided hostname argument has no value')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.help) {
|
|
|
|
process.stderr.write(`
|
|
|
|
Description
|
|
|
|
Starts the application in development mode (hot-code reloading, error
|
|
|
|
reporting, etc)
|
|
|
|
Usage
|
|
|
|
$ nuxt dev <dir> -p <port number> -H <hostname>
|
|
|
|
Options
|
|
|
|
--port, -p A port number on which to start the application
|
|
|
|
--hostname, -H Hostname on which to start the application
|
|
|
|
--spa Launch in SPA mode
|
|
|
|
--universal Launch in Universal mode (default)
|
|
|
|
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
|
|
|
--help, -h Displays this message
|
|
|
|
`)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2018-10-18 15:57:35 +00:00
|
|
|
const config = async () => {
|
2018-10-17 21:28:25 +00:00
|
|
|
// Force development mode for add hot reloading and watching changes
|
2018-10-18 15:57:35 +00:00
|
|
|
return Object.assign(await loadNuxtConfig(argv), { dev: true })
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const errorHandler = (err, instance) => {
|
|
|
|
instance && instance.builder.watchServer()
|
|
|
|
consola.error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start dev
|
2018-10-18 15:57:35 +00:00
|
|
|
async function startDev(oldInstance) {
|
2018-10-17 21:28:25 +00:00
|
|
|
let nuxt, builder
|
|
|
|
|
|
|
|
try {
|
2018-10-18 15:57:35 +00:00
|
|
|
nuxt = new Nuxt(await config())
|
2018-10-17 21:28:25 +00:00
|
|
|
builder = new Builder(nuxt)
|
2018-10-18 15:57:35 +00:00
|
|
|
nuxt.hook('watch:fileChanged', async (builder, fname) => {
|
2018-10-17 21:28:25 +00:00
|
|
|
consola.debug(`[${fname}] changed, Rebuilding the app...`)
|
2018-10-18 15:57:35 +00:00
|
|
|
await startDev({ nuxt: builder.nuxt, builder })
|
2018-10-17 21:28:25 +00:00
|
|
|
})
|
|
|
|
} catch (err) {
|
|
|
|
return errorHandler(err, oldInstance)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => oldInstance && oldInstance.nuxt.clearHook('watch:fileChanged'))
|
|
|
|
.then(() => oldInstance && oldInstance.builder.unwatch())
|
|
|
|
// Start build
|
|
|
|
.then(() => builder.build())
|
|
|
|
// Close old nuxt no mater if build successfully
|
|
|
|
.catch((err) => {
|
|
|
|
oldInstance && oldInstance.nuxt.close()
|
|
|
|
// Jump to eventHandler
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
.then(() => oldInstance && oldInstance.nuxt.close())
|
|
|
|
// Start listening
|
|
|
|
.then(() => nuxt.listen())
|
|
|
|
// Show ready message first time, others will be shown through WebpackBar
|
|
|
|
.then(() => !oldInstance && nuxt.showReady(false))
|
|
|
|
.then(() => builder.watchServer())
|
|
|
|
// Handle errors
|
|
|
|
.catch(err => errorHandler(err, { builder, nuxt }))
|
|
|
|
)
|
2018-10-18 15:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await runAsyncScript(startDev)
|
2018-10-17 21:28:25 +00:00
|
|
|
}
|