/
deprecate-config.js
55 lines (46 loc) · 1.68 KB
/
deprecate-config.js
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
"use strict";
const dotProp = require("dot-prop");
const log = require("npmlog");
const path = require("path");
module.exports = compose(
// add new predicates HERE
remap("command.publish.npmTag", "command.publish.distTag", { alsoRoot: true }),
remap("command.publish.cdVersion", "command.publish.bump", { alsoRoot: true }),
remap("command.publish.ignore", "command.publish.ignoreChanges"),
remap("commands", "command"),
(config, filepath) => ({ config, filepath })
);
/**
* Remap deprecated config properties, if they exist.
* The returned predicate mutates the `config` parameter.
*
* @param {String} search Path to deprecated option
* @param {String} target Path of renamed option
* @param {Object} opts Optional configuration object
* @param {Boolean} opts.alsoRoot Whether to check root config as well
* @return {Function} predicate accepting (config, filepath)
*/
function remap(search, target, { alsoRoot } = {}) {
const pathsToSearch = [search];
if (alsoRoot) {
// root config is overwritten by "more specific" nested config
pathsToSearch.unshift(search.split(".").pop());
}
return obj => {
for (const searchPath of pathsToSearch) {
if (dotProp.has(obj.config, searchPath)) {
const localPath = path.relative(".", obj.filepath);
log.warn(
"project",
`Deprecated key "${searchPath}" found in ${localPath}\nPlease rename "${searchPath}" => "${target}"`
);
dotProp.set(obj.config, target, dotProp.get(obj.config, searchPath));
dotProp.delete(obj.config, searchPath);
}
}
return obj;
};
}
function compose(...funcs) {
return funcs.reduce((a, b) => (...args) => a(b(...args)));
}