gitbook icon indicating copy to clipboard operation
gitbook copied to clipboard

Building gitbook in a gulp task

Open mitchchn opened this issue 11 years ago • 21 comments

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?

mitchchn avatar May 05 '15 04:05 mitchchn

You're not catching error:

book.generate('website')
    .then(function () {
      cb();
    }, cb);

SamyPesse avatar May 05 '15 06:05 SamyPesse

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.

mitchchn avatar May 05 '15 15:05 mitchchn

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?

mitchchn avatar May 08 '15 03:05 mitchchn

I'm encountering this problem as well. I would like to be able to generate gitbook docs through a gulp script.

blaffoy avatar May 13 '15 14:05 blaffoy

Now needed book.parse() first or would be incomplete build:

book.parse().then(function(){
    return book.generate("website");
}).then(function(){
    cb();
});

watert avatar Sep 18 '15 10:09 watert

+1 The issue with hanging after completion should be fixed. It makes Gitbook unusable from JavaScript.

AdamDz avatar Oct 23 '15 18:10 AdamDz

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).

ManuelRauber avatar Dec 09 '15 11:12 ManuelRauber

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.

ghost avatar Apr 20 '16 13:04 ghost

@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

AaronO avatar Apr 20 '16 13:04 AaronO

@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 avatar May 09 '18 05:05 rahuljain983

@rahuljain983 Yes, it's the typical gitbook structure as mentioned in their documentation.

ManuelRauber avatar May 09 '18 06:05 ManuelRauber

@ManuelRauber can you please share an example of the structure. that would be of great help.

rahuljain983 avatar May 09 '18 06:05 rahuljain983

@rahuljain983 It's in the documentation, please take a look there.

ManuelRauber avatar May 09 '18 06:05 ManuelRauber

@ManuelRauber ok. Thank you very much for your precious time. i will check the documentation. :)

rahuljain983 avatar May 09 '18 06:05 rahuljain983

@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);

image

my sample folder contains : book.json and readme.md , summary.md

rahuljain983 avatar May 09 '18 06:05 rahuljain983

@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 avatar May 09 '18 07:05 ManuelRauber

@ManuelRauber no!! i have send you the complete image

rahuljain983 avatar May 09 '18 07:05 rahuljain983

@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 avatar May 09 '18 07:05 ManuelRauber

@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 :)

rahuljain983 avatar May 09 '18 07:05 rahuljain983

@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?

rahuljain983 avatar May 10 '18 05:05 rahuljain983