-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (28 loc) · 855 Bytes
/
index.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
var debug = require('debug')('duo-env')
module.exports = function duoPlugin(config) {
config = config || {}
if (config.pick) config.pick = asArray(config.pick)
config.name = config.name || 'env'
var processEnv = config.pick ? pickKeys(config.pick, process.env) : process.env
var first = true
return function env(file, duo) {
if (first) {
debug('Exposing env vars: %j', processEnv)
duo.include(config.name, compileHashModule(processEnv), 'js')
first = false
}
}
}
// Helpers
function asArray(x){
return Array.isArray(x) ? x : [x]
}
function compileHashModule(hash) {
return 'module.exports = ' + JSON.stringify(hash) + ';'
}
function pickKeys(keys, oldHash) {
return keys.reduce(function(newHash, key){
if (oldHash.hasOwnProperty(key)) newHash[key] = oldHash[key]
return newHash
}, {})
}