summaryrefslogtreecommitdiffstats
path: root/bin/build.js
blob: 4f7429ae7c5e26792587c7d971b0153c44eec737 (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
var path = require('path');
var Q = require('q');
var _ = require('lodash');
var tmp = require('tmp');

var utils = require('./utils');

var generate = require("../lib/generate");
var parse = require("../lib/parse");
var fs = require('../lib/generate/fs');
var generators = require("../lib/generate").generators;

var buildFunc = function(dir, options) {
    dir = dir || process.cwd();
    outputDir = options.output || path.join(dir, '_book');

    console.log('Starting build ...');
    // Get repo's URL
    return utils.gitURL(dir)
    .then(function(url) {
        // Get ID of repo
        return utils.githubID(url);
    }, function(err) {
        return null;
    })
    .then(function(repoID) {
        var githubID = options.github || repoID;

        if(!githubID) {
            throw new Error('Needs a githubID (username/repo). Either set repo origin to a github repo or use the -g flag');
        }

        var parts = githubID.split('/', 2);
        var user = parts[0], repo = parts[1];

        var title = options.title || utils.titleCase(repo);

        return generate.folder(
            _.extend(options.options || {}, {
                input: dir,
                output: outputDir,
                title: title,
                description: options.intro,
                github: githubID,
                githubHost: options.githubHost,
                generator: options.format,
                theme: options.theme
            })
        );
    })
    .then(function(output) {
        console.log("Successfuly built !");
        return output;
    }, utils.logError);
};

var buildFiles = function(dir, outputFile, options, masterOptions) {
    var ext = masterOptions.extension;

    outputFile = outputFile || path.resolve(dir, "book."+ext);

    Q.nfcall(tmp.dir)
    .then(function(tmpDir) {
        return buildFunc(
            dir,
            _.extend(options, {
                output: tmpDir,
                format: masterOptions.format,
                options: masterOptions.options
            })
        )
        .then(function(_options) {
            var copyPDF = function(lang) {
                var _outputFile = outputFile;
                var _tmpDir = tmpDir;

                if (lang) {
                    _outputFile = _outputFile.slice(0, -path.extname(_outputFile).length)+"_"+lang+path.extname(_outputFile);
                    _tmpDir = path.join(_tmpDir, lang);
                }

                console.log("Generating in", _outputFile);
                return fs.copy(
                    path.join(_tmpDir, "index."+ext),
                    _outputFile
                );
            };

            // Multi-langs book
            return Q()
            .then(function() {
                if (_options.langsSummary) {
                    console.log("Generating for all the languages");
                    return Q.all(
                        _.map(_options.langsSummary.list, function(lang) {
                            return copyPDF(lang.lang);
                        })
                    );
                } else {
                    return copyPDF();
                }
            })
            .then(function() {
                return fs.remove(tmpDir);
            })
            .fail(utils.logError);
        });
    })
};

module.exports = {
    folder: buildFunc,
    files: buildFiles
};