generator

This commit is contained in:
Alexandre Chopin 2016-11-10 12:33:52 +01:00
parent 982e49e0aa
commit e0641e1e51
5 changed files with 89 additions and 1 deletions

View File

@ -8,7 +8,8 @@ const commands = new Set([
defaultCommand,
'init',
'build',
'start'
'start',
'generate'
])
let cmd = process.argv[2]

29
bin/nuxt-generate Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env node
const fs = require('fs')
const Nuxt = require('../')
const { resolve } = require('path')
const rootDir = resolve(process.argv.slice(2)[0] || '.')
const nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
let options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.build = true // Enable building
options.dev = false // Force production mode (no webpack middlewares called)
console.log('[nuxt] Generating...')
new Nuxt(options)
.then((nuxt) => nuxt.generate())
.then(() => {
console.log('[nuxt] Generate done')
})
.catch((err) => {
console.error(err)
process.exit()
})

54
lib/generate.js Normal file
View File

@ -0,0 +1,54 @@
'use strict'
// const fs = require('fs')
const { ncp } = require('ncp')
// const debug = require('debug')('nuxt:generate')
const _ = require('lodash')
const { resolve } = require('path')
const defaults = {
dir: 'dist',
routeParams: {}
}
module.exports = function * () {
/*
** Set variables
*/
this.options.generate = _.defaultsDeep(this.options.generate, defaults)
var srcStaticPath = resolve(this.dir, 'static')
var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist')
var distPath = resolve(this.dir, this.options.generate.dir)
var distNuxtPath = resolve(distPath, '_nuxt')
/*
** Copy static and built files
*/
ncp(srcStaticPath, distNuxtPath, function (err) {
if (err) {
return console.log(err)
}
console.log('[nuxt] Static files copied')
})
ncp(srcBuiltPath, distNuxtPath, function (err) {
if (err) {
return console.log(err)
}
console.log('[nuxt] Built files copied')
})
/*
** Generate html files from routes
*/
var promises = []
this.options.routes.forEach((route) => {
var promise = this.renderRoute(route.path).then((html) => {
return {
path: route.path,
html
}
})
promises.push(promise)
})
// Promise.all(promises).then((page) => {
// verifier erreur
// })
}

View File

@ -8,6 +8,7 @@ const pify = require('pify')
const ansiHTML = require('ansi-html')
const serialize = require('serialize-javascript')
const build = require('./build')
const generate = require('./generate')
const serveStatic = require('serve-static')
const { resolve, join } = require('path')
const { encodeHtml, getContext, setAnsiColors } = require('./utils')
@ -48,6 +49,8 @@ class Nuxt {
this.serveStatic = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist')))
// Add this.build
this.build = build.bind(this)
// Add this.generate
this.generate = generate.bind(this)
// Launch build and set this.renderer
return co(this.build)
.then(() => {

View File

@ -36,6 +36,7 @@
"lodash": "^4.16.6",
"lru-cache": "^4.0.1",
"mkdirp-then": "^1.2.0",
"ncp": "^2.0.0",
"pify": "^2.3.0",
"serialize-javascript": "^1.3.0",
"serve-static": "^1.11.1",