Nuxt/bin/nuxt-generate
qm3ster ad0649925d
Refactor cli
Extracted common logic
Improved style consistency
2018-02-07 02:44:29 +02:00

86 lines
2.1 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* eslint-disable no-console */
const parseArgs = require('minimist')
const debug = require('debug')('nuxt:generate')
const { Nuxt, Builder, Generator, Utils } = require('..')
const { loadNuxtConfig } = require('./common/utils')
const argv = parseArgs(process.argv.slice(2), {
alias: {
h: 'help',
c: 'config-file',
s: 'spa',
u: 'universal'
},
boolean: ['h', 's', 'u', 'build'],
string: ['c'],
default: {
c: 'nuxt.config.js',
build: true
}
})
if (argv.help) {
console.log(`
Description
Generate a static web application (server-rendered)
Usage
$ nuxt generate <dir>
Options
--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
--no-build Just run generate for faster builds when just dynamic routes changed. Nuxt build is needed before this command.
`)
process.exit(0)
}
const options = loadNuxtConfig(argv)
options.dev = false // Force production mode (no webpack middleware called)
debug('Generating...')
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
const generateOptions = {
init: true,
build: argv['build']
}
const s = Date.now()
// Setup hooks
nuxt.hook('error', (_err, from) => Utils.fatalError(_err, from))
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))
nuxt.hook('generate:done', (generator, errors) => {
const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`)
if (errors.length) {
/* eslint-disable no-console */
console.log('\n Generate errors summary:')
console.log('\n' + errors.toString())
}
})
generator
.generate(generateOptions)
.then(() => {
debug('Generate done')
process.exit(0)
})
.catch(Utils.fatalError)