loose-envify.js 791 B

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