diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-18 15:08:34 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-18 15:08:34 +0100 |
commit | b99c499d2ea67268760a7c8265a0669b016a56f1 (patch) | |
tree | f7e95ab1096abaf9579bf670205b1fb5b8b848b8 /lib/cli/index.js | |
parent | 709b388dfcc641fab25d297618b6ffe49f5cd677 (diff) | |
download | gitbook-b99c499d2ea67268760a7c8265a0669b016a56f1.zip gitbook-b99c499d2ea67268760a7c8265a0669b016a56f1.tar.gz gitbook-b99c499d2ea67268760a7c8265a0669b016a56f1.tar.bz2 |
Add back serve command
Diffstat (limited to 'lib/cli/index.js')
-rw-r--r-- | lib/cli/index.js | 109 |
1 files changed, 108 insertions, 1 deletions
diff --git a/lib/cli/index.js b/lib/cli/index.js index c9a2e8f..0492c29 100644 --- a/lib/cli/index.js +++ b/lib/cli/index.js @@ -1,7 +1,16 @@ +/* eslint-disable no-console */ + var path = require('path'); +var tinylr = require('tiny-lr'); +var Promise = require('../utils/promise'); var PluginsManager = require('../plugins'); +var Book = require('../book'); +var NodeFS = require('../fs/node'); + var helper = require('./helper'); +var Server = require('./server'); +var watch = require('./watch'); module.exports = { commands: [ @@ -60,8 +69,106 @@ module.exports = { return output.generate(); }); }) - } + }, + + { + name: 'serve [book]', + description: 'Build then serve a book from a directory', + options: [ + { + name: 'port', + description: 'Port for server to listen on', + defaults: 4000 + }, + { + name: 'lrport', + description: 'Port for livereload server to listen on', + defaults: 35729 + }, + { + name: 'watch', + description: 'Enable/disable file watcher', + defaults: true + }, + helper.options.format, + helper.options.log + ], + exec: function(args, kwargs) { + var input = args[0] || process.cwd(); + var server = new Server(); + + // Init livereload server + var lrServer = tinylr({}); + var port = kwargs.port; + var lrPath; + + var generate = function() { + // Stop server if running + if (server.isRunning()) console.log('Stopping server'); + return server.stop() + + // Generate the book + .then(function() { + var book = new Book({ + fs: new NodeFS(), + root: input, + 'config': { + 'defaultsPlugins': ['livereload'] + }, + 'logLevel': kwargs.log + }); + + return book.parse() + .then(function() { + var Out = helper.FORMATS[kwargs.format]; + var output = new Out(book); + + return output.generate() + .thenResolve(output); + }); + }) + + // Start server and watch changes + .then(function(output) { + console.log(); + console.log('Starting server ...'); + return server.start(output.root(), port) + .then(function() { + console.log('Serving book on http://localhost:'+port); + + if (lrPath) { + // trigger livereload + lrServer.changed({ + body: { + files: [lrPath] + } + }); + } + + if (!kwargs.watch) return; + + return watch(output.book.root) + .then(function(filepath) { + // set livereload path + lrPath = filepath; + console.log('Restart after change in file', filepath); + console.log(''); + return generate(); + }); + }); + }); + }; + + return Promise.nfcall(lrServer.listen.bind(lrServer), kwargs.lrport) + .then(function() { + console.log('Live reload server started on port:', kwargs.lrport); + console.log('Press CTRL+C to quit ...'); + console.log(''); + return generate(); + }); + } + } ] }; |