#!/usr/bin/env node
const parseArgs = require('minimist')
const consola = require('consola')
const { version } = require('../package.json')
const { Nuxt, Builder } = require('..')
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
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(version + '\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
-p -H
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)
}
// Start dev
let hooked = false
let dev = startDev()
function startDev(oldInstance) {
// Get latest environment variables
const { port, host } = getLatestHost(argv)
// Error handler
const onError = (err, instance) => {
consola.error(err)
return Promise.resolve(instance) // Wait for next reload
}
// Load options
let options = {}
try {
options = loadAndAugmentNuxtConfig()
} catch (err) {
return onError(err, oldInstance)
}
// Create nuxt and builder instance
let nuxt
let builder
let instance
try {
nuxt = new Nuxt(options)
builder = new Builder(nuxt)
instance = { nuxt, builder }
} catch (err) {
return onError(err, oldInstance)
}
if (!hooked) {
nuxt.hook('watch:fileChanged', (fname) => {
consola.debug(`[${fname}] changed`)
dev = dev.then((instance) => {
consola.debug('Rebuilding the app...')
return startDev(instance)
})
})
hooked = true
}
return (
Promise.resolve()
.then(() => {
if (oldInstance && oldInstance.builder) {
return oldInstance.builder.unwatch()
} else {
return nuxt.listen(port, host)
}
})
// Start build
.then(() => builder.build())
// Close old nuxt after successful build
.then(
() =>
oldInstance && oldInstance.nuxt
? oldInstance.nuxt.close()
: Promise.resolve()
)
// Start listening
.then(() => {
if (oldInstance) {
return nuxt.listen(port, host)
} else {
return Promise.resolve()
}
})
// Pass new nuxt to watch chain
.then(() => instance)
// Handle errors
.catch(err => onError(err, instance))
)
}
function loadAndAugmentNuxtConfig() {
const options = loadNuxtConfig(argv)
// Force development mode for add hot reloading and watching changes
options.dev = true
return options
}