summaryrefslogtreecommitdiffstats
path: root/lib/config/index.js
blob: 272e92a7d54a61d4be18bb03b58459065453f798 (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
var _ = require('lodash');
var semver = require('semver');

var gitbook = require('../gitbook');
var Promise = require('../utils/promise');
var validator = require('./validator');
var plugins = require('./plugins');

// Config files to tested (sorted)
var CONFIG_FILES = [
    'book.js',
    'book.json'
];

/*
Config is an interface for the book's configuration stored in "book.json" (or "book.js")
*/

function Config(book, baseConfig) {
    this.book = book;
    this.fs = book.fs;
    this.log = book.log;
    this.path = '';

    this.replace(baseConfig || {});
}

// Load configuration of the book
// and verify that the configuration is satisfying
Config.prototype.load = function() {
    var that = this;

    // Try all potential configuration file
    return Promise.some(CONFIG_FILES, function(filename) {
        that.log.debug.ln('try loading configuration from', filename);

        return that.fs.loadAsObject(that.book.resolve(filename))
        .then(function(_config) {
            that.log.debug.ln('configuration loaded from', filename);

            that.path = filename;
            return that.replace(_config);
        })
        .fail(function(err) {
            if (err.code != 'MODULE_NOT_FOUND') throw(err);
            else return Promise(false);
        });
    })
    .then(function() {
        if (!that.book.isLanguageBook()) {
            if (!gitbook.satisfies(that.options.gitbook)) {
                throw new Error('GitBook version doesn\'t satisfy version required by the book: '+that.options.gitbook);
            }
            if (that.options.gitbook != '*' && !semver.satisfies(semver.inc(gitbook.version, 'patch'), that.options.gitbook)) {
                that.log.warn.ln('gitbook version specified in your book.json might be too strict for future patches, \''+(_.first(gitbook.version.split('.'))+'.x.x')+'\' is more adequate');
            }
        }

        that.options.plugins = plugins.toList(that.options.plugins);
        that.options.defaultsPlugins = plugins.toList(that.options.defaultsPlugins || '', false);
        that.options.plugins = _.union(that.options.plugins, that.options.defaultsPlugins);
        that.options.plugins = _.uniq(that.options.plugins, 'name');

        that.options.gitbook = gitbook.version;
    });
};

// Replace the whole configuration
Config.prototype.replace = function(options) {
    var that = this;

    this.options = validator.validate(options);

    // options.input == book.root
    Object.defineProperty(this.options, 'input', {
        get: function () {
            return that.book.root;
        }
    });

    // options.originalInput == book.parent.root
    Object.defineProperty(this.options, 'originalInput', {
        get: function () {
            return that.book.parent? that.book.parent.root : undefined;
        }
    });
};

// Return true if book has a configuration file
Config.prototype.exists = function() {
    return Boolean(this.path);
};

// Return path to a structure file
// Strip the extension by default
Config.prototype.getStructure = function(name, dontStripExt) {
    var filename = this.options.structure[name];
    if (dontStripExt) return filename;

    filename = filename.split('.').slice(0, -1).join('.');
    return filename;
};

// Return a configuration using a key and a default value
Config.prototype.get = function(key, def) {
    return _.get(this.options, key, def);
};

// Update a configuration
Config.prototype.set = function(key, value) {
    return _.set(this.options, key, value);
};

// Return a dump of the configuration
Config.prototype.dump = function() {
    return _.cloneDeep(this.options);
};

// Return templating context
Config.prototype.getContext = function() {
    return {
        config: this.book.config.dump()
    };
};

module.exports = Config;