summaryrefslogtreecommitdiffstats
path: root/packages/gitbook/src/api/deprecate.js
blob: c781971ad22412eb6e424bfe120b9df9be002ad2 (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
const is = require('is');
const objectPath = require('object-path');

const logged = {};
const disabled = {};

/**
 * Log a deprecated notice
 *
 * @param {Book|Output} book
 * @param {String} key
 * @param {String} message
 */
function logNotice(book, key, message) {
    if (logged[key] || disabled[key]) return;

    logged[key] = true;

    const logger = book.getLogger();
    logger.warn.ln(message);
}

/**
 * Deprecate a function
 *
 * @param {Book|Output} book
 * @param {String} key: unique identitifer for the deprecated
 * @param {Function} fn
 * @param {String} msg: message to print when called
 * @return {Function}
 */
function deprecateMethod(book, key, fn, msg) {
    return function(...args) {
        logNotice(book, key, msg);
        return fn.apply(this, args);
    };
}

/**
 * Deprecate a property of an object
 *
 * @param {Book|Output} book
 * @param {String} key: unique identitifer for the deprecated
 * @param {Object} instance
 * @param {String|Function} property
 * @param {String} msg: message to print when called
 * @return {Function}
 */
function deprecateField(book, key, instance, property, value, msg) {
    let store = undefined;

    const prepare = () => {
        if (!is.undefined(store)) return;

        if (is.fn(value)) store = value();
        else store = value;
    };

    const getter = () => {
        prepare();

        logNotice(book, key, msg);
        return store;
    };

    const setter = (v) => {
        prepare();

        logNotice(book, key, msg);
        store = v;
        return store;
    };

    Object.defineProperty(instance, property, {
        get: getter,
        set: setter,
        enumerable: false,
        configurable: true
    });
}

/**
 * Enable a deprecation
 * @param {String} key: unique identitifer
 */
function enableDeprecation(key) {
    disabled[key] = false;
}

/**
 * Disable a deprecation
 * @param {String} key: unique identitifer
 */
function disableDeprecation(key) {
    disabled[key] = true;
}

/**
 * Deprecate a method in favor of another one.
 *
 * @param {Book} book
 * @param {String} key
 * @param {Object} instance
 * @param {String} oldName
 * @param {String} newName
 */
function deprecateRenamedMethod(book, key, instance, oldName, newName, msg) {
    msg = msg || ('"' + oldName + '" is deprecated, use "' + newName + '()" instead');
    const fn = objectPath.get(instance, newName);

    instance[oldName] = deprecateMethod(book, key, fn, msg);
}

module.exports = {
    method: deprecateMethod,
    renamedMethod: deprecateRenamedMethod,
    field: deprecateField,
    enable: enableDeprecation,
    disable: disableDeprecation
};