Nuxt/bin/nuxt-generate

73 lines
1.7 KiB
Plaintext
Raw Normal View History

2016-11-10 11:33:52 +00:00
#!/usr/bin/env node
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
const { Nuxt, Builder, Generator } = 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',
m: 'mode'
2017-06-11 13:48:20 +00:00
},
boolean: ['h'],
2017-08-18 10:54:35 +00:00
string: ['c', 'm'],
2017-06-11 13:48:20 +00:00
default: {
c: 'nuxt.config.js'
}
})
if (argv.help) {
console.log(`
Description
Generate a static web application (server-rendered)
Usage
$ nuxt generate <dir>
Options
2017-08-18 10:54:35 +00:00
--mode, -m [spa|universal|static] Nuxt Mode (default: universal)
--config-file, -c Path to Nuxt.js config file (default: nuxt.config.js)
--help, -h Displays this message
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') {
console.error(`> Could not load config file ${argv['config-file']}`)
process.exit(1)
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
if (argv['mode']) {
options.mode = argv['mode']
}
2017-06-16 12:42:45 +00:00
debug('Generating...')
const nuxt = new Nuxt(options)
const builder = new Builder(nuxt)
const generator = new Generator(nuxt, builder)
generator.generate()
2017-06-16 12:42:45 +00:00
.then(() => {
debug('Generate done')
process.exit(0)
})
.catch((err) => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})