2016-11-08 00:18:26 +00:00
|
|
|
#!/usr/bin/env node
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const parseArgs = require('minimist')
|
2017-07-17 17:59:31 +00:00
|
|
|
const { Nuxt } = require('../')
|
2017-06-16 14:04:40 +00:00
|
|
|
const { join, resolve } = require('path')
|
2016-11-07 01:34:58 +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'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (argv.hostname === '') {
|
|
|
|
console.error(`> Provided hostname argument has no value`)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
Description
|
|
|
|
Starts the application in production mode.
|
|
|
|
The application should be compiled with \`nuxt build\` first.
|
|
|
|
Usage
|
|
|
|
$ nuxt start <dir> -p <port number> -H <hostname>
|
|
|
|
Options
|
2017-08-18 12:46:03 +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|ssr-only] 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-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
|
|
|
let options = {}
|
|
|
|
|
2016-11-07 01:34:58 +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-07 01:34:58 +00:00
|
|
|
}
|
2017-06-20 11:44:47 +00:00
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
2017-06-20 11:44:47 +00:00
|
|
|
|
|
|
|
// Force production mode (no webpack middleware called)
|
|
|
|
options.dev = false
|
2016-11-09 22:59:41 +00:00
|
|
|
|
2017-08-18 10:54:35 +00:00
|
|
|
// Nuxt Mode
|
|
|
|
if (argv['mode']) {
|
|
|
|
options.mode = argv['mode']
|
|
|
|
}
|
|
|
|
|
2017-08-19 10:00:40 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
|
|
|
|
2017-06-16 14:04:40 +00:00
|
|
|
// Check if project is built for production
|
2017-08-19 10:00:40 +00:00
|
|
|
const distDir = resolve(nuxt.options.rootDir, nuxt.options.buildDir || '.nuxt', 'dist')
|
2017-08-05 11:45:31 +00:00
|
|
|
if (!fs.existsSync(distDir)) {
|
2017-06-16 14:04:40 +00:00
|
|
|
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2017-08-19 10:00:40 +00:00
|
|
|
// Check if SSR Bundle is required
|
|
|
|
if (nuxt.options.render.ssr === true) {
|
|
|
|
const ssrBundlePath = resolve(distDir, 'server-bundle.json')
|
|
|
|
if (!fs.existsSync(ssrBundlePath)) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error('> No SSR build! Please start with `-m spa` flag or build using `nuxt build -m universal`')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-19 10:00:40 +00:00
|
|
|
|
2017-06-20 11:44:47 +00:00
|
|
|
nuxt.listen(port, host)
|