2016-11-09 22:59:41 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2016-11-10 14:24:22 +00:00
|
|
|
// Show logs
|
|
|
|
process.env.DEBUG = 'nuxt:*'
|
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const _ = require('lodash')
|
|
|
|
const debug = require('debug')('nuxt:build')
|
2016-12-15 17:48:31 +00:00
|
|
|
debug.color = 2 // force green color
|
2017-06-16 12:42:45 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const parseArgs = require('minimist')
|
2017-06-18 15:44:01 +00:00
|
|
|
const { Nuxt, Server, Builder } = require('../')
|
2017-06-16 12:42:45 +00:00
|
|
|
const chokidar = require('chokidar')
|
|
|
|
const resolve = require('path').resolve
|
2017-04-19 20:01:26 +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',
|
|
|
|
c: 'config-file'
|
|
|
|
},
|
|
|
|
boolean: ['h'],
|
|
|
|
string: ['H', 'c'],
|
|
|
|
default: {
|
|
|
|
c: 'nuxt.config.js'
|
|
|
|
}
|
|
|
|
})
|
2017-04-19 20:01:26 +00:00
|
|
|
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.hostname === '') {
|
|
|
|
console.error(`> Provided hostname argument has no value`)
|
|
|
|
process.exit(1)
|
2017-04-19 20:01:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 13:48:20 +00:00
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
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
|
|
|
|
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
|
|
|
--help, -h Displays this message
|
|
|
|
`)
|
|
|
|
process.exit(0)
|
2017-04-19 20:01:26 +00:00
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const rootDir = resolve(argv._[0] || '.')
|
|
|
|
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
2016-12-07 17:47:47 +00:00
|
|
|
|
2016-12-09 18:40:59 +00:00
|
|
|
var options = {}
|
2016-11-09 22:59:41 +00:00
|
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
|
|
options = require(nuxtConfigFile)
|
2017-06-11 13:48:20 +00:00
|
|
|
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
|
|
|
console.error(`> Could not load config file ${argv['config-file']}`)
|
|
|
|
process.exit(1)
|
2016-11-09 22:59:41 +00:00
|
|
|
}
|
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
2017-06-11 13:48:20 +00:00
|
|
|
// Force development mode: add hot reloading and watching changes
|
|
|
|
options.dev = true
|
2016-11-10 01:19:47 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
2017-06-18 15:44:01 +00:00
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
|
|
|
const host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
|
2017-06-18 15:44:01 +00:00
|
|
|
const server = new Server(nuxt)
|
|
|
|
server.listen(port, host)
|
2017-02-08 13:09:59 +00:00
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
builder.build().then(() => {
|
|
|
|
listenOnConfigChanges(nuxt, server)
|
|
|
|
})
|
2016-11-16 17:41:09 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
function listenOnConfigChanges (nuxt, server) {
|
2016-11-16 17:41:09 +00:00
|
|
|
// Listen on nuxt.config.js changes
|
2017-06-16 12:42:45 +00:00
|
|
|
const build = _.debounce(() => {
|
2016-12-14 14:30:44 +00:00
|
|
|
debug('[nuxt.config.js] changed')
|
2016-11-16 17:41:09 +00:00
|
|
|
delete require.cache[nuxtConfigFile]
|
2017-06-18 15:44:01 +00:00
|
|
|
var options = {}
|
2016-11-16 17:41:09 +00:00
|
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
2016-12-12 14:01:30 +00:00
|
|
|
try {
|
|
|
|
options = require(nuxtConfigFile)
|
|
|
|
} catch (e) {
|
|
|
|
return console.error(e) // eslint-disable-line no-console
|
|
|
|
}
|
2016-11-16 17:41:09 +00:00
|
|
|
}
|
2017-06-18 15:44:01 +00:00
|
|
|
|
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
|
|
|
|
2016-11-16 17:41:09 +00:00
|
|
|
nuxt.close()
|
2017-05-31 14:21:16 +00:00
|
|
|
.then(() => {
|
|
|
|
debug('Rebuilding the app...')
|
2017-06-16 12:42:45 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
2017-06-18 15:44:01 +00:00
|
|
|
const builder = new Builder(nuxt)
|
2017-05-31 14:21:16 +00:00
|
|
|
server.nuxt = nuxt
|
2017-06-18 15:44:01 +00:00
|
|
|
return builder.build()
|
2017-05-31 14:21:16 +00:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
|
|
|
process.exit(1)
|
|
|
|
})
|
2016-11-16 17:41:09 +00:00
|
|
|
}, 200)
|
2017-06-11 13:48:20 +00:00
|
|
|
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
|
2017-05-31 14:21:16 +00:00
|
|
|
.on('all', build)
|
2016-11-16 17:41:09 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
|
|
|
module.exports = nuxt
|