2016-11-10 11:33:52 +00:00
|
|
|
#!/usr/bin/env node
|
2017-10-31 13:16:40 +00:00
|
|
|
/* eslint-disable no-console */
|
2016-11-10 11:33:52 +00:00
|
|
|
|
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 fs = require('fs')
|
|
|
|
const parseArgs = require('minimist')
|
|
|
|
const debug = require('debug')('nuxt:generate')
|
2016-11-10 11:33:52 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
const { Nuxt, Builder, Generator, Utils } = require('../')
|
2017-06-16 12:42:45 +00:00
|
|
|
const resolve = require('path').resolve
|
|
|
|
|
|
|
|
const argv = parseArgs(process.argv.slice(2), {
|
2017-06-11 13:48:20 +00:00
|
|
|
alias: {
|
|
|
|
h: 'help',
|
2017-08-18 10:54:35 +00:00
|
|
|
c: 'config-file',
|
2017-08-19 13:22:53 +00:00
|
|
|
s: 'spa',
|
|
|
|
u: 'universal'
|
2017-06-11 13:48:20 +00:00
|
|
|
},
|
2017-10-27 15:15:34 +00:00
|
|
|
boolean: ['h', 's', 'u', 'build'],
|
2017-08-19 13:22:53 +00:00
|
|
|
string: ['c'],
|
2017-06-11 13:48:20 +00:00
|
|
|
default: {
|
2017-10-27 15:15:34 +00:00
|
|
|
c: 'nuxt.config.js',
|
|
|
|
build: true
|
2017-06-11 13:48:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (argv.help) {
|
|
|
|
console.log(`
|
|
|
|
Description
|
|
|
|
Generate a static web application (server-rendered)
|
|
|
|
Usage
|
|
|
|
$ nuxt generate <dir>
|
|
|
|
Options
|
2017-08-19 13:22:53 +00:00
|
|
|
--spa Launch in SPA mode
|
|
|
|
--universal Launch in Universal mode (default)
|
|
|
|
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
|
|
|
|
--help, -h Displays this message
|
2017-10-27 15:15:34 +00:00
|
|
|
--no-build Just run generate for faster builds when just dynamic routes changed. Nuxt build is needed before this command.
|
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
|
|
|
|
2016-12-09 18:40:59 +00:00
|
|
|
var options = {}
|
2016-11-10 11:33:52 +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') {
|
2018-01-11 16:11:50 +00:00
|
|
|
Utils.fatalError('Could not load config file: ' + argv['config-file'])
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|
|
|
|
if (typeof options.rootDir !== 'string') {
|
|
|
|
options.rootDir = rootDir
|
|
|
|
}
|
2017-02-03 14:09:27 +00:00
|
|
|
options.dev = false // Force production mode (no webpack middleware called)
|
2016-11-10 11:33:52 +00:00
|
|
|
|
2017-08-18 10:54:35 +00:00
|
|
|
// Nuxt Mode
|
2018-01-11 16:11:50 +00:00
|
|
|
options.mode =
|
|
|
|
(argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
|
2017-08-18 10:54:35 +00:00
|
|
|
|
2017-06-16 12:42:45 +00:00
|
|
|
debug('Generating...')
|
2017-06-18 15:44:01 +00:00
|
|
|
const nuxt = new Nuxt(options)
|
|
|
|
const builder = new Builder(nuxt)
|
|
|
|
const generator = new Generator(nuxt, builder)
|
2017-10-27 15:15:34 +00:00
|
|
|
|
|
|
|
const generateOptions = {
|
|
|
|
init: true,
|
|
|
|
build: argv['build']
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
const s = Date.now()
|
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
// Setup hooks
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
nuxt.hook('generate:distRemoved', () => debug('Destination folder cleaned'))
|
|
|
|
|
|
|
|
nuxt.hook('generate:distCopied', () => debug('Static & build files copied'))
|
|
|
|
|
|
|
|
nuxt.hook('generate:page', page => debug('Generate file: ' + page.path))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
nuxt.hook('generate:done', (generator, errors) => {
|
2017-11-23 22:31:54 +00:00
|
|
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
|
|
|
|
|
|
|
debug(`HTML Files generated in ${duration}s`)
|
2018-01-12 21:21:49 +00:00
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
if (errors.length) {
|
2018-01-11 18:20:35 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
console.log('\n Generate errors summary:')
|
|
|
|
console.log('\n' + errors.toString())
|
2017-11-23 22:31:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-01-11 16:11:50 +00:00
|
|
|
generator
|
|
|
|
.generate(generateOptions)
|
2017-06-16 12:42:45 +00:00
|
|
|
.then(() => {
|
|
|
|
debug('Generate done')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
2018-01-11 16:11:50 +00:00
|
|
|
.catch(err => {
|
|
|
|
Utils.fatalError(err)
|
2017-06-16 12:42:45 +00:00
|
|
|
})
|