This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.js
104 lines (98 loc) · 2.95 KB
/
utils.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
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
'use strict';
const mergeWith = require('lodash.mergewith');
const flatten = require('flat');
const isPlainObject = require('is-plain-obj');
const escapeRegExp = require('escape-string-regexp');
exports.invariant = (condition, message) => {
if (condition) return;
throw new Error(`Invariant violation: ${message}`);
};
exports.validate = (strategy, checkArgs = () => {}, checkResult = () => {}) =>
Object.defineProperty(
(functions, ...initialArgs) =>
strategy(
functions.map((fn) => (...callArgs) => {
checkArgs(callArgs, initialArgs);
const result = fn(...callArgs);
if (result && result instanceof Promise) {
return result.then((result) => {
checkResult(result, true, callArgs, initialArgs);
return result;
});
} else {
checkResult(result, false, callArgs, initialArgs);
return result;
}
}),
...initialArgs
),
'name',
{ value: strategy.name }
);
exports.merge = (...args) =>
mergeWith({}, ...args, (objValue, srcValue, key) => {
if (Array.isArray(objValue)) {
if ('mixins' === key) {
return [...objValue, ...srcValue].filter(
(curr, index, self) => self.indexOf(curr) === index
);
}
return srcValue;
}
});
exports.placeholdify = (config) => {
const flatConfig = flatten(config);
const configPaths = Object.keys(flatConfig).map(escapeRegExp);
const regExp = new RegExp(`<(${configPaths.join('|')})>`, 'g');
const replaceRecursive = (item) => {
if (Array.isArray(item)) {
return item.map(replaceRecursive);
}
if (isPlainObject(item)) {
return Object.entries(item).reduce(
(result, [key, value]) => ({
...result,
[key]: replaceRecursive(value),
}),
{}
);
}
if (regExp.test(item)) {
return item.replace(regExp, (_, key) =>
replaceRecursive(flatConfig[key] || '')
);
}
return item;
};
return replaceRecursive(config);
};
exports.environmentalize = (_config, whitelist = {}) => {
const _env = {};
const env = global._env || process.env;
const regExp = /\[([a-zA-Z_][a-zA-Z0-9_]*)(?:=(.*?))?\]/g;
const replaceRecursive = (item, path = []) => {
if (Array.isArray(item)) {
return item.map((nestedItem) => replaceRecursive(nestedItem, path));
}
if (isPlainObject(item)) {
return Object.entries(item).reduce(
(result, [key, value]) => ({
...result,
[key]: replaceRecursive(value, [...path, key]),
}),
{}
);
}
if (regExp.test(item)) {
return item.replace(regExp, (_, key, fallback) => {
const replaced = env[key] || fallback || '';
if (whitelist[path.join('.')]) {
_env[key] = replaced;
}
return replaceRecursive(replaced);
});
}
return item;
};
return { ...replaceRecursive(_config), _config, _env };
};