Nuxt/bin/nuxt-generate
Pim 65f4a030f4 refactor generator + cli tests (#2205)
* Rename this.generateRoutes to this.staticRoutes

* Refactor generator to separate logic

* Move routeCreated hook to generateRoute

Add routeFailed hook for unhandled exceptions

Keep page errors separately until page hooks have been called

* Move debug and report statements to hooks

* pageErrors can be a const

Push pageErrors to errors

* fix done hook, errors are 2nd param

* Add generator hooks to nuxt-build for spa mode

* Added a cli integration test for bin/nuxt-(build|start|generate)

* Removed unnecessary waitFor

* Use pify instead util.promisify for v6 compatibility

* Fix windows build

You cant execute .js files directly on Windows/Appveyor so call node with nuxt-*.js file as argument

* Fix windows build (2)

Use correct folder separators in text search

* Fix possible timing quirck in children.path.test
2017-11-24 02:01:54 +03:30

115 lines
2.9 KiB
JavaScript
Executable File

#!/usr/bin/env node
/* eslint-disable no-console */
// Show logs
process.env.DEBUG = process.env.DEBUG || 'nuxt:*'
const fs = require('fs')
const parseArgs = require('minimist')
const debug = require('debug')('nuxt:generate')
const { Nuxt, Builder, Generator } = require('../')
const resolve = require('path').resolve
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
--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 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)
// Nuxt Mode
options.mode = (argv['spa'] && 'spa') || (argv['universal'] && 'universal') || options.mode
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()
nuxt.hook('generate:distRemoved', function() {
debug('Destination folder cleaned')
})
nuxt.hook('generate:distCopied', function() {
debug('Static & build files copied')
})
nuxt.hook('generate:page', function(page) {
debug('Generate file: ' + page.path)
})
nuxt.hook('generate:done', function(generator, errors) {
const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`)
if (errors.length) {
const report = errors.map(({ type, route, error }) => {
/* istanbul ignore if */
if (type === 'unhandled') {
return `Route: '${route}'\n${error.stack}`
} else {
return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
}
})
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
}
})
generator.generate(generateOptions)
.then(() => {
debug('Generate done')
process.exit(0)
})
.catch((err) => {
console.error(err)
process.exit(1)
})