summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-05-10 13:08:19 +0200
committerSamy Pessé <samypesse@gmail.com>2016-05-10 13:08:19 +0200
commit852fae3a099cc45c88a0b28b55a13d47644e53e4 (patch)
treea37c29a3bc0f4332706091c86412e9b6e4ee63a3
parentd342d31c8e2dd8e18ddaf863edecc5b1cb82cb98 (diff)
downloadgitbook-852fae3a099cc45c88a0b28b55a13d47644e53e4.zip
gitbook-852fae3a099cc45c88a0b28b55a13d47644e53e4.tar.gz
gitbook-852fae3a099cc45c88a0b28b55a13d47644e53e4.tar.bz2
Fix #1232: add option "--no-watch" to disable watcher and live reloading
-rw-r--r--lib/cli/serve.js45
1 files changed, 36 insertions, 9 deletions
diff --git a/lib/cli/serve.js b/lib/cli/serve.js
index aa2d60e..9d53408 100644
--- a/lib/cli/serve.js
+++ b/lib/cli/serve.js
@@ -16,11 +16,23 @@ var watch = require('./watch');
var server, lrServer, lrPath;
+function waitForCtrlC() {
+ var d = Promise.defer();
+
+ process.on('SIGINT', function() {
+ d.resolve();
+ });
+
+ return d.promise;
+}
+
+
function generateBook(args, kwargs) {
var port = kwargs.port;
var outputFolder = getOutputFolder(args);
var book = getBook(args, kwargs);
var Generator = Output.getGenerator(kwargs.format);
+ var hasLiveReloading = kwargs['watch'];
// Stop server if running
if (server.isRunning()) console.log('Stopping server');
@@ -29,8 +41,10 @@ function generateBook(args, kwargs) {
.then(function() {
return Parse.parseBook(book)
.then(function(resultBook) {
- // Enable livereload plugin
- resultBook = ConfigModifier.addPlugin(resultBook, 'livereload');
+ if (hasLiveReloading) {
+ // Enable livereload plugin
+ resultBook = ConfigModifier.addPlugin(resultBook, 'livereload');
+ }
return Output.generate(Generator, resultBook, {
root: outputFolder
@@ -55,7 +69,9 @@ function generateBook(args, kwargs) {
}
})
.then(function() {
- if (!kwargs.watch) return;
+ if (!hasLiveReloading) {
+ return waitForCtrlC();
+ }
return watch(book.getRoot())
.then(function(filepath) {
@@ -84,7 +100,7 @@ module.exports = {
},
{
name: 'watch',
- description: 'Enable/disable file watcher',
+ description: 'Enable file watcher and live reloading',
defaults: true
},
options.log,
@@ -92,13 +108,24 @@ module.exports = {
],
exec: function(args, kwargs) {
server = new Server();
- lrServer = tinylr({});
+ var hasWatch = kwargs['watch'];
- return Promise.nfcall(lrServer.listen.bind(lrServer), kwargs.lrport)
+ return Promise()
+ .then(function() {
+ if (!hasWatch) {
+ return;
+ }
+
+ lrServer = tinylr({});
+ 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('');
+
+ });
+ })
.then(function() {
- console.log('Live reload server started on port:', kwargs.lrport);
- console.log('Press CTRL+C to quit ...');
- console.log('');
return generateBook(args, kwargs);
});
}