forked from hughsk/envify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
38 lines (29 loc) · 844 Bytes
/
custom.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
var through = require('through')
, jstransform = require('jstransform')
, createVisitors = require('./visitors')
var processEnvPattern = /\bprocess\.env\b/
module.exports = function(rootEnv) {
rootEnv = rootEnv || process.env || {}
return function envify(file, argv) {
if (/\.json$/.test(file)) return through()
var buffer = []
argv = argv || {}
return through(write, flush)
function write(data) {
buffer.push(data)
}
function flush() {
var source = buffer.join('')
if (processEnvPattern.test(source)) {
try {
var visitors = createVisitors([argv, rootEnv])
source = jstransform.transform(visitors, source).code
} catch(err) {
return this.emit('error', err)
}
}
this.queue(source)
this.queue(null)
}
}
}