mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-06 06:03:58 +00:00
71 lines
2.1 KiB
JavaScript
Executable File
71 lines
2.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const fs = require('fs')
|
|
const parseArgs = require('minimist')
|
|
const { Nuxt, Server } = require('../')
|
|
const { join, resolve } = require('path')
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
|
alias: {
|
|
h: 'help',
|
|
H: 'hostname',
|
|
p: 'port',
|
|
c: 'config-file'
|
|
},
|
|
boolean: ['h'],
|
|
string: ['H', 'c'],
|
|
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
|
|
--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)
|
|
}
|
|
|
|
const rootDir = resolve(argv._[0] || '.')
|
|
const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
|
|
|
var options = {}
|
|
if (fs.existsSync(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
|
|
}
|
|
options.dev = false // Force production mode (no webpack middleware called)
|
|
|
|
// Check if project is built for production
|
|
const distDir = join(options.rootDir, options.buildDir || '.nuxt', 'dist' )
|
|
if (!fs.existsSync(join(distDir,'server-bundle.json'))) {
|
|
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`') // eslint-disable-line no-console
|
|
process.exit(1)
|
|
}
|
|
|
|
const nuxt = new Nuxt(options)
|
|
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
|
|
new Server(nuxt).listen(port, host)
|
|
|
|
module.exports = nuxt
|