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:*'
|
|
|
|
|
2016-12-09 18:40:59 +00:00
|
|
|
var _ = require('lodash')
|
|
|
|
var debug = require('debug')('nuxt:build')
|
2016-12-15 17:48:31 +00:00
|
|
|
debug.color = 2 // force green color
|
2016-12-09 18:40:59 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
var Nuxt = require('../')
|
|
|
|
var chokidar = require('chokidar')
|
|
|
|
var resolve = require('path').resolve
|
2017-04-19 20:01:26 +00:00
|
|
|
var without = require('lodash').without
|
|
|
|
|
|
|
|
var nuxtConfigFileName = 'nuxt.config.js'
|
|
|
|
|
|
|
|
// --config-file option
|
|
|
|
var indexOfConfig = false
|
|
|
|
if (process.argv.indexOf('--config-file') !== -1) {
|
|
|
|
indexOfConfig = process.argv.indexOf('--config-file')
|
|
|
|
} else if (process.argv.indexOf('-c') !== -1) {
|
|
|
|
indexOfConfig = process.argv.indexOf('-c')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (indexOfConfig !== false) {
|
|
|
|
nuxtConfigFileName = process.argv.slice(indexOfConfig)[1]
|
|
|
|
process.argv = without(process.argv, '--config-file', '-c', nuxtConfigFileName)
|
|
|
|
}
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2016-12-09 18:40:59 +00:00
|
|
|
var rootDir = resolve(process.argv.slice(2)[0] || '.')
|
2017-04-19 20:01:26 +00:00
|
|
|
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
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)
|
|
|
|
}
|
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
2016-11-10 01:19:47 +00:00
|
|
|
options.dev = true // Add hot reloading and watching changes
|
|
|
|
|
2016-12-09 18:40:59 +00:00
|
|
|
var nuxt = new Nuxt(options)
|
2017-02-08 13:09:59 +00:00
|
|
|
var server = new nuxt.Server(nuxt)
|
|
|
|
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
|
|
|
|
listenOnConfigChanges(nuxt, server)
|
|
|
|
|
2016-12-07 17:47:47 +00:00
|
|
|
nuxt.build()
|
2016-11-09 22:59:41 +00:00
|
|
|
.catch((err) => {
|
2016-12-09 17:54:41 +00:00
|
|
|
console.error(err) // eslint-disable-line no-console
|
2016-11-10 01:19:47 +00:00
|
|
|
process.exit(1)
|
2016-11-09 22:59:41 +00:00
|
|
|
})
|
2016-11-16 17:41:09 +00:00
|
|
|
|
|
|
|
function listenOnConfigChanges (nuxt, server) {
|
|
|
|
// Listen on nuxt.config.js changes
|
2016-12-09 18:40:59 +00:00
|
|
|
var 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]
|
2016-12-09 18:40:59 +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
|
|
|
}
|
|
|
|
options.rootDir = rootDir
|
|
|
|
nuxt.close()
|
|
|
|
.then(() => {
|
2016-12-14 14:30:44 +00:00
|
|
|
nuxt.renderer = null
|
|
|
|
debug('Rebuilding the app...')
|
2016-12-07 17:47:47 +00:00
|
|
|
return new Nuxt(options).build()
|
2016-11-16 17:41:09 +00:00
|
|
|
})
|
|
|
|
.then((nuxt) => {
|
|
|
|
server.nuxt = nuxt
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2016-12-12 14:01:30 +00:00
|
|
|
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
2016-11-16 17:41:09 +00:00
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
}, 200)
|
2017-04-19 20:01:26 +00:00
|
|
|
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
2017-03-25 14:16:07 +00:00
|
|
|
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
|
2016-11-16 17:41:09 +00:00
|
|
|
.on('all', build)
|
|
|
|
}
|