2016-11-09 22:59:41 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2016-11-10 14:24:22 +00:00
|
|
|
// Show logs
|
2017-07-06 13:02:48 +00:00
|
|
|
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
|
2016-11-10 14:24:22 +00:00
|
|
|
|
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-20 11:44:47 +00:00
|
|
|
const { Nuxt, 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',
|
2017-08-18 10:54:35 +00:00
|
|
|
c: 'config-file',
|
|
|
|
m: 'mode'
|
2017-06-11 13:48:20 +00:00
|
|
|
},
|
|
|
|
boolean: ['h'],
|
2017-08-18 10:54:35 +00:00
|
|
|
string: ['H', 'c', 'm'],
|
2017-06-11 13:48:20 +00:00
|
|
|
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
|
2017-08-18 10:54:35 +00:00
|
|
|
--port, -p A port number on which to start the application
|
|
|
|
--hostname, -H Hostname on which to start the application
|
|
|
|
--mode, -m [spa|universal|static] Nuxt Mode (default: universal)
|
|
|
|
--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-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
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Load config once for chokidar
|
|
|
|
const nuxtConfig = loadNuxtConfig()
|
|
|
|
_.defaultsDeep(nuxtConfig, { watchers: { chokidar: { ignoreInitial: true } } })
|
2016-11-10 01:19:47 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Start dev
|
|
|
|
let dev = startDev()
|
2017-06-18 15:44:01 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Start watching for nuxt.config.js changes
|
|
|
|
chokidar
|
|
|
|
.watch(nuxtConfigFile, nuxtConfig.watchers.chokidar)
|
|
|
|
.on('all', _.debounce(() => {
|
|
|
|
debug('[nuxt.config.js] changed')
|
|
|
|
debug('Rebuilding the app...')
|
|
|
|
dev = dev.then(startDev)
|
|
|
|
}), 2500)
|
2017-02-08 13:09:59 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
function startDev (oldNuxt) {
|
|
|
|
// Get latest environment variables
|
|
|
|
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
|
2016-11-16 17:41:09 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
// Load options
|
|
|
|
let options = {}
|
|
|
|
try {
|
|
|
|
options = loadNuxtConfig()
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
return // Wait for next reload
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create nuxt and builder instance
|
|
|
|
const nuxt = new Nuxt(options)
|
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => builder.build()) // 1- Start build
|
|
|
|
.then(() => oldNuxt ? oldNuxt.close() : Promise.resolve()) // 2- Close old nuxt after successful build
|
|
|
|
.then(() => nuxt.listen(port, host)) // 3- Start listening
|
|
|
|
.then(() => nuxt) // 4- Pass new nuxt to watch chain
|
2016-11-16 17:41:09 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
function loadNuxtConfig () {
|
|
|
|
let options = {}
|
|
|
|
|
|
|
|
if (fs.existsSync(nuxtConfigFile)) {
|
|
|
|
delete require.cache[nuxtConfigFile]
|
|
|
|
options = require(nuxtConfigFile)
|
|
|
|
} else if (argv['config-file'] !== 'nuxt.config.js') {
|
|
|
|
console.error(`> Could not load config file ${argv['config-file']}`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force development mode for add hot reloading and watching changes
|
|
|
|
options.dev = true
|
|
|
|
|
2017-08-18 10:54:35 +00:00
|
|
|
// Nuxt Mode
|
|
|
|
if (argv['mode']) {
|
|
|
|
options.mode = argv['mode']
|
|
|
|
}
|
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
return options
|
|
|
|
}
|