-
Notifications
You must be signed in to change notification settings - Fork 59
/
env-plugin.js
39 lines (37 loc) · 1.28 KB
/
env-plugin.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
module.exports = function envPlugin(options = {}) {
const replaceEnv = (value) => {
const matchList = [...value.matchAll(/\{\{(.+?)}}/g)]
if (matchList && matchList.length > 0) {
matchList.forEach((item) => {
if (item) {
let [matchStr, name] = item;
name = name.replaceAll(" ", "").toUpperCase()
if (process.env[name]) {
value = value.replaceAll(matchStr, process.env[name])
}
}
})
}
return value
}
return (tree) => {
tree.children = tree.children.map((node) => {
if (
node.children && node.children.length && node.children.length > 0
) {
node.children = node.children.map(item => {
try {
Object.keys(item).map(key => {
item[key] = replaceEnv(item[key])
})
} catch (_) {
}
return item
})
} else if (node["type"] === 'code' && node["value"]) {
node["value"] = replaceEnv(node["value"])
}
return node;
});
};
};