UNPKG

2.65 kBPlain TextView Raw
1#!/usr/bin/env node
2/* eslint-disable no-console */
3
4const fs = require('fs')
5const parseArgs = require('minimist')
6const { Nuxt } = require('../')
7const { resolve } = require('path')
8
9const 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
25if (argv.hostname === '') {
26 console.error(`> Provided hostname argument has no value`)
27 process.exit(1)
28}
29
30if (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
48const rootDir = resolve(argv._[0] || '.')
49const nuxtConfigFile = resolve(rootDir, argv['config-file'])
50
51let options = {}
52
53if (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
60if (typeof options.rootDir !== 'string') {
61 options.rootDir = rootDir
62}
63
64// Force production mode (no webpack middleware called)
65options.dev = false
66
67// Nuxt Mode
68options.mode = (argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
69
70const nuxt = new Nuxt(options)
71
72// Check if project is built for production
73const distDir = resolve(nuxt.options.rootDir, nuxt.options.buildDir || '.nuxt', 'dist')
74if (!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
80if (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
88const port = argv.port || process.env.PORT || process.env.npm_package_config_nuxt_port
89const host = argv.hostname || process.env.HOST || process.env.npm_package_config_nuxt_host
90
91nuxt.listen(port, host)