Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
var Syntax = require('jstransform').Syntax
2
var utils = require('jstransform/src/utils')
3
4
function create(envs) {
5
var args = [].concat(envs[0]._ || []).concat(envs[1]._ || [])
6
var purge = args.indexOf('purge') !== -1
7
8
function visitProcessEnv(traverse, node, path, state) {
9
var key = node.property.name || node.property.value
10
11
for (var i = 0; i < envs.length; i++) {
12
var value = envs[i][key]
13
if (value !== undefined) {
14
replaceEnv(node, state, value)
15
return false
16
}
17
}
18
19
if (purge) {
20
replaceEnv(node, state, undefined)
21
}
22
23
return false
24
}
25
26
function replaceEnv(node, state, value) {
27
utils.catchup(node.range[0], state)
28
utils.append(JSON.stringify(value), state)
29
utils.move(node.range[1], state)
30
}
31
32
visitProcessEnv.test = function(node, path, state) {
33
return (
34
node.type === Syntax.MemberExpression
35
&& !(path[0].type === Syntax.AssignmentExpression && path[0].left === node)
36
&& node.property.type === (node.computed ? Syntax.Literal : Syntax.Identifier)
37
&& node.object.computed === false
38
&& node.object.type === Syntax.MemberExpression
39
&& node.object.object.type === Syntax.Identifier
40
&& node.object.object.name === 'process'
41
&& node.object.property.type === Syntax.Identifier
42
&& node.object.property.name === 'env'
43
)
44
}
45
46
return [visitProcessEnv]
47
}
48
49
module.exports = create
50
51