Building gitbook in a gulp task
I'm trying to build my book using gulp:
var gitbook = require('gitbook');
gulp.task('gitbook-build', function (cb) {
var book = new gitbook.Book('book/', {
"config": {
"output": "build/"
}
});
book.generate('website')
.then(function () {
cb();
});
});
Strangely, the build completes successfully and the callback does get reached, but gulp still hangs as if there were more async code yet to be run:
$ gulp gitbook-build
[00:18:10] Using gulpfile ~/project/gulpfile.js
[00:18:10] Starting 'gitbook-build'...
info: start generation with website generator
info: clean website generator
info: OK
info: generation is finished
[00:18:10] Finished 'gitbook-build' after 191 ms
I have to ctrl-c to get out of it. Is this the correct way to build a gitbook programmatically?
You're not catching error:
book.generate('website')
.then(function () {
cb();
}, cb);
Thanks, @SamyPesse, but it still hangs after completion even if I catch the error exactly as you have shown. (The build completes successfully so the error does not trigger.)
When I test the generation code in a simple JS file (no Gulp) it also hangs in the terminal and does not terminate the program after completion.
In gitbook-cli I see that gitbook commands passed to runPromise(p) end in the following:
return p
.then(function() {
process.exit(0);
}
Without terminating the promise chain this way (by quitting the process), the generate(format) function will hang node even after it has returned successfully. This is obviously a problem for anyone trying to invoke gitbook in the middle of a gulp script.
@SamyPesse, do you have any idea if this issue could be resolved in the generator itself?
I'm encountering this problem as well. I would like to be able to generate gitbook docs through a gulp script.
Now needed book.parse() first or would be incomplete build:
book.parse().then(function(){
return book.generate("website");
}).then(function(){
cb();
});
+1 The issue with hanging after completion should be fixed. It makes Gitbook unusable from JavaScript.
I'm using
'use strict';
var gulp = require('gulp'),
Q = require('q'),
gitbook = require('gitbook');
gulp.task('gitbook', function () {
var book = new gitbook.Book('Documentation/', {
config: {
output: 'DocumentationBook/'
}
});
return Q.all(book.parse())
.then(function () {
return book.generate('website');
});
});
which works very well. The process terminates after generating the books. (I'm generating a multilingual book, this is why I'm using Q.all).
Hi there! I'm new to gitbook and gulp and everything... So I'm sorry for my stupid question.. I tried your code ManuelRauber but I get the error message: Cannot find module 'q' and Cannot find module 'gitbook'. Where or how can I install these modules? Thanks for your answer.
@lisamanuelal if you're new to GitBook, gulp and co, why not use GitBook.com, it's free to use (for open source and public projects) and will automatically build and host your documentation (you can keep a GitHub repo and use webhooks to integrate with GitBook.com).
Otherwise you can simply install those modules with npm install:
npm install --save q gitbook
@ManuelRauber hi Manuel!! i am trying to create a dynamic gitbook based on user inputs. can you please help me with the below method: var book = new gitbook.Book('Documentation/', { config: { output: 'DocumentationBook/' } });
Does Documentation path contains the books.json file?
@rahuljain983 Yes, it's the typical gitbook structure as mentioned in their documentation.
@ManuelRauber can you please share an example of the structure. that would be of great help.
@rahuljain983 It's in the documentation, please take a look there.
@ManuelRauber ok. Thank you very much for your precious time. i will check the documentation. :)
@ManuelRauber i am getting the below error on the code. var http = require('http'); var gitbook = require('gitbook'); var server= http.createServer(function(req,res) { var book = new gitbook.Book('./sample/', { config: { output: './_book/' } }); }); server.listen(3000);

my sample folder contains : book.json and readme.md , summary.md
@rahuljain983 According to the call stack provided this issue has currently nothing to do with your code. Did you truncate something of the image?
@ManuelRauber no!! i have send you the complete image
@rahuljain983 Please make sure, you're executing the code you've sent above, normally within the stack trace there should be a line with your own file. Otherwise the error is from another node library called immutable.js.
@ManuelRauber Thankyou Manuel!! currently, i am not able to find why i am getting this error. i will try to get rid of this error :)
@ManuelRauber : Hey Manuel!! below code worked for me. var rootPath = path.join(__dirname, 'sample') var outputPath = path.join(__dirname, '_book') var fs = gitbook.createNodeFS(rootPath) var book = gitbook.Book.createForFS(fs) var Parse = gitbook.Parse var Output = gitbook.Output var Generator = Output.getGenerator('website')
return Parse.parseBook(book) .then(function (resultBook) { return Output.generate(Generator, resultBook, { root: outputPath }) }) but i want to create a pdf ebook. can you help me with this please?