Nuxt/bin/nuxt-dev

138 lines
3.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
2017-06-16 12:42:45 +00:00
const parseArgs = require('minimist')
2018-03-31 16:22:14 +00:00
const consola = require('consola')
2018-03-16 16:12:06 +00:00
const { version } = require('../package.json')
2018-03-31 16:22:14 +00:00
const { Nuxt, Builder } = require('..')
2018-08-09 21:35:55 +00:00
const { loadNuxtConfig, getLatestHost } = require('./common/utils')
2018-03-16 16:12:06 +00:00
2017-06-16 12:42:45 +00:00
const argv = parseArgs(process.argv.slice(2), {
2017-06-11 13:48:20 +00:00
alias: {
h: 'help',
H: 'hostname',
p: 'port',
2017-08-18 10:54:35 +00:00
c: 'config-file',
2017-08-19 13:22:53 +00:00
s: 'spa',
u: 'universal',
v: 'version'
2017-06-11 13:48:20 +00:00
},
boolean: ['h', 's', 'u', 'v'],
2017-08-19 13:22:53 +00:00
string: ['H', 'c'],
2017-06-11 13:48:20 +00:00
default: {
c: 'nuxt.config.js'
}
})
2017-10-30 14:48:19 +00:00
if (argv.version) {
2018-03-31 16:22:14 +00:00
process.stderr.write(version + '\n')
process.exit(0)
}
2017-06-11 13:48:20 +00:00
if (argv.hostname === '') {
2018-04-01 20:20:46 +00:00
consola.fatal('Provided hostname argument has no value')
}
2017-06-11 13:48:20 +00:00
if (argv.help) {
2018-03-31 16:22:14 +00:00
process.stderr.write(`
2017-06-11 13:48:20 +00:00
Description
Starts the application in development mode (hot-code reloading, error
reporting, etc)
Usage
$ nuxt dev <dir> -p <port number> -H <hostname>
Options
2017-08-19 13:22:53 +00:00
--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
2017-06-11 13:48:20 +00:00
`)
process.exit(0)
}
2017-06-20 11:44:47 +00:00
// Start dev
let hooked = false
2017-06-20 11:44:47 +00:00
let dev = startDev()
2017-02-08 13:09:59 +00:00
function startDev(oldInstance) {
2017-06-20 11:44:47 +00:00
// Get latest environment variables
const { port, host } = getLatestHost(argv)
// Error handler
const onError = (err, instance) => {
2018-04-01 20:20:46 +00:00
consola.error(err)
return Promise.resolve(instance) // Wait for next reload
}
2017-06-20 11:44:47 +00:00
// Load options
let options = {}
try {
options = loadAndAugmentNuxtConfig()
2017-06-20 11:44:47 +00:00
} catch (err) {
return onError(err, oldInstance)
2017-06-20 11:44:47 +00:00
}
// 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)
}
2017-06-20 11:44:47 +00:00
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
}
2018-01-11 16:11:50 +00:00
return (
Promise.resolve()
.then(() => {
if (oldInstance && oldInstance.builder) {
return oldInstance.builder.unwatch()
} else {
return nuxt.listen(port, host)
}
})
2018-01-11 16:11:50 +00:00
// Start build
2018-07-25 15:57:43 +00:00
.then(() => builder.build())
2018-01-11 16:11:50 +00:00
// 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()
}
})
2018-01-11 16:11:50 +00:00
// Pass new nuxt to watch chain
.then(() => instance)
// Handle errors
.catch(err => onError(err, instance))
)
}
function loadAndAugmentNuxtConfig() {
const options = loadNuxtConfig(argv)
2017-06-20 11:44:47 +00:00
// Force development mode for add hot reloading and watching changes
options.dev = true
return options
}