-
Notifications
You must be signed in to change notification settings - Fork 16
/
filterEnv.js
45 lines (40 loc) · 1.05 KB
/
filterEnv.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
var fs = require('fs')
var dockerfile = process.argv[2] || 'Dockerfile.test'
var envCopy = Object.assign({}, process.env)
var dockerfileLines
try {
dockerfileLines = fs.readFileSync(dockerfile, 'utf8').split(/\r?\n/g)
} catch (e) {
dockerfileLines = []
}
// Remove all variables from process.env that exist in the given Dockerfile
var inEnv = false
dockerfileLines.forEach(function(line) {
if (!inEnv && !/^ENV\s/.test(line)) {
inEnv = false
return
}
var pieces = line.trim().split(/\s+/g)
if (!inEnv) {
pieces = pieces.slice(1)
}
if (pieces[pieces.length - 1] == '\\') {
inEnv = true
pieces.pop()
} else {
inEnv = false
}
if (pieces.length == 2 && !~pieces[0].indexOf('=')) {
pieces = [pieces.join('=')]
}
pieces.forEach(piece => delete envCopy[piece.split('=')[0]])
})
// Also remove variables that don't make sense to pass
delete envCopy.HOME
delete envCopy.HOSTNAME
delete envCopy.PWD
delete envCopy.TERM
delete envCopy.SHELL
delete envCopy.SHLVL
delete envCopy._
console.log(Object.keys(envCopy).join('\n'))