You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

4 years ago
  1. 'use strict'
  2. var dotenvExpand = function (config) {
  3. var interpolate = function (env) {
  4. var matches = env.match(/\$([a-zA-Z0-9_]+)|\${([a-zA-Z0-9_]+)}/g) || []
  5. matches.forEach(function (match) {
  6. var key = match.replace(/\$|{|}/g, '')
  7. // process.env value 'wins' over .env file's value
  8. var variable = process.env[key] || config.parsed[key] || ''
  9. // Resolve recursive interpolations
  10. variable = interpolate(variable)
  11. env = env.replace(match, variable)
  12. })
  13. return env
  14. }
  15. for (var configKey in config.parsed) {
  16. var value = process.env[configKey] || config.parsed[configKey]
  17. if (config.parsed[configKey].substring(0, 2) === '\\$') {
  18. config.parsed[configKey] = value.substring(1)
  19. } else if (config.parsed[configKey].indexOf('\\$') > 0) {
  20. config.parsed[configKey] = value.replace(/\\\$/g, '$')
  21. } else {
  22. config.parsed[configKey] = interpolate(value)
  23. }
  24. }
  25. for (var processKey in config.parsed) {
  26. process.env[processKey] = config.parsed[processKey]
  27. }
  28. return config
  29. }
  30. module.exports = dotenvExpand