summaryrefslogtreecommitdiffstats
path: root/bin/gitbook.js
blob: c2c2e31902c048af2a8fbf1e99cad94b174c4231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /usr/bin/env node

var Q = require('q');
var _ = require('lodash');
var path = require('path');
var prog = require('commander');
var tinylr = require('tiny-lr-fork');

var pkg = require('../package.json');
var generators = require("../lib/generate").generators;
var initDir = require("../lib/generate/init");
var fs = require('../lib/generate/fs');

var utils = require('./utils');
var build = require('./build');
var Server = require('./server');

// General options
prog
.version(pkg.version);

build.command(prog.command('build [source_dir]'))
.description('Build a gitbook from a directory')
.action(build.folder);

build.command(prog.command('serve [source_dir]'))
.description('Build then serve a gitbook from a directory')
.option('-p, --port <port>', 'Port for server to listen on', 4000)
.option('--no-watch', 'Disable restart with file watching')
.action(function(dir, options) {
    var server = new Server();

    // init livereload server
    var lrOptions = {port: 35729};
    var lrServer = tinylr(lrOptions);
    var lrPath = undefined;
    lrServer.listen(lrOptions.port, function(err) {
      if (err) { return console.log(err); }
      console.log('Live reload server started on port: ' + lrOptions.port);
    });

    var generate = function() {
        if (server.isRunning()) console.log("Stopping server");

        server.stop()
        .then(function() {
            return build.folder(dir, _.extend(options || {}, {
                cache: false,
                defaultsPlugins: ["livereload"]
            }));
        })
        .then(function(_options) {
            console.log();
            console.log('Starting server ...');
            return server.start(_options.output, options.port)
            .then(function() {
                console.log('Serving book on http://localhost:'+options.port);

                if (lrPath) {
                  // trigger livereload
                  lrServer.changed({body:{files:[lrPath]}})
                }

                if (!options.watch) return;
                return utils.watch(_options.input)
                .then(function(filepath) {
                    // set livereload path
                    lrPath = filepath;
                    console.log("Restart after change in files");
                    console.log('');
                    return generate();
                })
            })
        })
        .fail(utils.logError);
    };

    console.log('Press CTRL+C to quit ...');
    console.log('')
    generate();
});

build.commandEbook(prog.command('ebook [source_dir]'))
.description('Build a gitbook as a eBook (format detected according to the extension)')
.action(function(dir, options) {
    var ext = options.output ? path.extname(options.output) : "epub";

    build.file(dir, _.extend(options, {
        extension: ext,
        format: "ebook"
    }));
});

build.commandEbook(prog.command('pdf [source_dir]'))
.description('Build a gitbook as a PDF')
.action(function(dir, options) {
    build.file(dir, _.extend(options, {
        extension: "pdf",
        format: "ebook"
    }));
});

build.commandEbook(prog.command('epub [source_dir]'))
.description('Build a gitbook as a ePub book')
.action(function(dir, options) {
    build.file(dir, _.extend(options, {
        extension: "epub",
        format: "ebook"
    }));
});

build.commandEbook(prog.command('mobi [source_dir]'))
.description('Build a gitbook as a Mobi book')
.action(function(dir, options) {
    build.file(dir, _.extend(options, {
        extension: "mobi",
        format: "ebook"
    }));
});

prog
.command('init [source_dir]')
.description('Create files and folders based on contents of SUMMARY.md')
.action(function(dir) {
    dir = dir || process.cwd();
    return initDir(dir);
});

// Parse and fallback to help if no args
if(_.isEmpty(prog.parse(process.argv).args) && process.argv.length === 2) {
    prog.help();
}