diff --git a/bin/nuxt b/bin/nuxt index c12f2f3e38..4ec27a0e79 100755 --- a/bin/nuxt +++ b/bin/nuxt @@ -8,7 +8,8 @@ const commands = new Set([ defaultCommand, 'init', 'build', - 'start' + 'start', + 'generate' ]) let cmd = process.argv[2] diff --git a/bin/nuxt-generate b/bin/nuxt-generate new file mode 100755 index 0000000000..a7daf45f49 --- /dev/null +++ b/bin/nuxt-generate @@ -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() +}) diff --git a/lib/generate.js b/lib/generate.js new file mode 100644 index 0000000000..6ab1a6cd48 --- /dev/null +++ b/lib/generate.js @@ -0,0 +1,66 @@ +'use strict' + +const fs = require('fs-extra') +const co = require('co') +const pify = require('pify') +const debug = require('debug')('nuxt:generate') +const _ = require('lodash') +const { resolve, join } = require('path') +const copy = pify(fs.copy) +const remove = pify(fs.remove) +const writeFile = pify(fs.writeFile) + +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') + co(function * () { + /* + ** Clean destination folder + */ + try { + yield remove(distPath) + debug('Destination folder cleaned') + } catch (e) {} + /* + ** Copy static and built files + */ + yield [ + copy(srcStaticPath, distPath), + copy(srcBuiltPath, distNuxtPath) + ] + debug('Static & build files copied') + }) + .then(() => { + /* + ** Generate html files from routes + */ + var promises = [] + this.options.routes.forEach((route) => { + var promise = this.renderRoute(route.path) + .then(({ html }) => { + var path = route.path + path += path[path.length - 1] === '/' ? 'index.html' : '.html' + debug('Generate file : ' + path) + path = join(distPath, path) + return writeFile(path, html, 'utf8') + }) + console.log(promise) + promises.push(promise) + }) + return Promise.all(promises) + }) + .then((pages) => { + debug('HTML Files generated') + }) +} diff --git a/lib/nuxt.js b/lib/nuxt.js index 95943640a5..e63eacf973 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -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') @@ -51,6 +52,8 @@ class Nuxt { this.serveStaticNuxt = 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(() => { diff --git a/package.json b/package.json index b7fb97af8d..4dca6f773b 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "es6-promise": "^4.0.5", "extract-text-webpack-plugin": "2.0.0-beta.4", "file-loader": "^0.9.0", + "fs-extra": "^1.0.0", "glob-promise": "^1.0.6", "hash-sum": "^1.0.2", "lodash": "^4.16.6",