1 | #!/usr/bin/env node
|
2 | /* eslint-disable no-console */
|
3 |
|
4 | const fs = require('fs')
|
5 | const parseArgs = require('minimist')
|
6 | const { Nuxt } = require('../')
|
7 | const { resolve } = require('path')
|
8 |
|
9 | const argv = parseArgs(process.argv.slice(2), {
|
10 | alias: {
|
11 | h: 'help',
|
12 | H: 'hostname',
|
13 | p: 'port',
|
14 | c: 'config-file',
|
15 | s: 'spa',
|
16 | u: 'universal'
|
17 | },
|
18 | boolean: ['h', 's', 'u'],
|
19 | string: ['H', 'c'],
|
20 | default: {
|
21 | c: 'nuxt.config.js'
|
22 | }
|
23 | })
|
24 |
|
25 | if (argv.hostname === '') {
|
26 | console.error(`> Provided hostname argument has no value`)
|
27 | process.exit(1)
|
28 | }
|
29 |
|
30 | if (argv.help) {
|
31 | console.log(`
|
32 | Description
|
33 | Starts the application in production mode.
|
34 | The application should be compiled with \`nuxt build\` first.
|
35 | Usage
|
36 | $ nuxt start <dir> -p <port number> -H <hostname>
|
37 | Options
|
38 | --port, -p A port number on which to start the application
|
39 | --hostname, -H Hostname on which to start the application
|
40 | --spa Launch in SPA mode
|
41 | --universal Launch in Universal mode (default)
|
42 | --config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
43 | --help, -h Displays this message
|
44 | `)
|
45 | process.exit(0)
|
46 | }
|
47 |
|
48 | const rootDir = resolve(argv._[0] || '.')
|
49 | const nuxtConfigFile = resolve(rootDir, argv['config-file'])
|
50 |
|
51 | let options = {}
|
52 |
|
53 | if (fs.existsSync(nuxtConfigFile)) {
|
54 | options = require(nuxtConfigFile)
|
55 | } else if (argv['config-file'] !== 'nuxt.config.js') {
|
56 | console.error(`> Could not load config file ${argv['config-file']}`)
|
57 | process.exit(1)
|
58 | }
|
59 |
|
60 | if (typeof options.rootDir !== 'string') {
|
61 | options.rootDir = rootDir
|
62 | }
|
63 |
|
64 | // Force production mode (no webpack middleware called)
|
65 | options.dev = false
|
66 |
|
67 | // Nuxt Mode
|
68 | options.mode = (argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
69 |
|
70 | const nuxt = new Nuxt(options)
|
71 |
|
72 | // Check if project is built for production
|
73 | const distDir = resolve(nuxt.options.rootDir, nuxt.options.buildDir || '.nuxt', 'dist')
|
74 | if (!fs.existsSync(distDir)) {
|
75 | console.error('> No build files found, please run `nuxt build` before launching `nuxt start`')
|
76 | process.exit(1)
|
77 | }
|
78 |
|
79 | // Check if SSR Bundle is required
|
80 | if (nuxt.options.render.ssr === true) {
|
81 | const ssrBundlePath = resolve(distDir, 'server-bundle.json')
|
82 | if (!fs.existsSync(ssrBundlePath)) {
|
83 | console.error('> No SSR build! Please start with `nuxt start --spa` or build using `nuxt build --universal`')
|
84 | process.exit(1)
|
85 | }
|
86 | }
|
87 |
|
88 | const port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
|
89 | const host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
|
90 |
|
91 | nuxt.listen(port, host)
|