summaryrefslogtreecommitdiffstats
path: root/lib/api/encodeGlobal.js
blob: fa336962b745d5e3ed276988908c71459b0c3b06 (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
var fs = require('../utils/fs');
var Promise = require('../utils/promise');
var PathUtils = require('../utils/path');

var deprecate = require('./deprecate');
var encodeConfig = require('./encodeConfig');
var fileToURL = require('../output/helper/fileToURL');

/**
    Encode a global context into a JS object
    It's the context for page's hook, etc

    @param {Output} output
    @return {Object}
*/
function encodeGlobal(output) {
    var book = output.getBook();
    var logger = output.getLogger();
    var outputFolder = output.getRoot();

    var result = {
        log: logger,
        config: encodeConfig(output, book.getConfig()),

        isMultilingual: function() {
            return book.isMultilingual();
        },

        isLanguageBook: function() {
            return book.isLanguageBook();
        },

        isSubBook: deprecate.method(output, 'this.isSubBook', function() {
            return book.isLanguageBook();
        }, '"isSubBook" is deprecated, use "isLanguageBook()" instead'),

        output: {
            /**
                Name of the generator being used
                {String}
            */
            name: output.getGenerator(),

            /**
                Return absolute path to the root folder of output
                @return {String}
            */
            root: function() {
                return outputFolder;
            },

            /**
                Convert a filepath into an url
                @return {String}
            */
            toURL: function(filePath) {
                return fileToURL(output, filePath);
            },

            /**
                Write a file to the output folder,
                It creates the required folder

                @param {String} fileName
                @param {Buffer} content
                @return {Promise}
            */
            writeFile: function(fileName, content) {
                return Promise()
                .then(function() {
                    var filePath = PathUtils.resolveInRoot(outputFolder, fileName);

                    return fs.ensure(filePath)
                    .then(function() {
                        return fs.writeFile(filePath, content);
                    });
                });
            }
        }
    };

    deprecate.field(output, 'this.book', result, 'book',
        result, '"book" property is deprecated, use "this" directly instead');

    deprecate.field(output, 'this.options', result, 'options',
        result.config.values, '"options" property is deprecated, use config.get(key) instead');

    return result;
}

module.exports = encodeGlobal;