1
|
'use strict';
|
2
|
|
3
|
var stream = require('stream');
|
4
|
var util = require('util');
|
5
|
var replace = require('./replace');
|
6
|
|
7
|
var jsonExtRe = /\.json$/;
|
8
|
|
9
|
module.exports = function(rootEnv) {
|
10
|
rootEnv = rootEnv || process.env;
|
11
|
return function (file, trOpts) {
|
12
|
if (jsonExtRe.test(file)) {
|
13
|
return stream.PassThrough();
|
14
|
}
|
15
|
var envs = trOpts ? [rootEnv, trOpts] : [rootEnv];
|
16
|
return new LooseEnvify(envs);
|
17
|
};
|
18
|
};
|
19
|
|
20
|
function LooseEnvify(envs) {
|
21
|
stream.Transform.call(this);
|
22
|
this._data = '';
|
23
|
this._envs = envs;
|
24
|
}
|
25
|
util.inherits(LooseEnvify, stream.Transform);
|
26
|
|
27
|
LooseEnvify.prototype._transform = function(buf, enc, cb) {
|
28
|
this._data += buf;
|
29
|
cb();
|
30
|
};
|
31
|
|
32
|
LooseEnvify.prototype._flush = function(cb) {
|
33
|
var replaced = replace(this._data, this._envs);
|
34
|
this.push(replaced);
|
35
|
cb();
|
36
|
};
|