main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* @flow */
  2. /*::
  3. type DotenvParseOptions = {
  4. debug?: boolean
  5. }
  6. // keys and values from src
  7. type DotenvParseOutput = { [string]: string }
  8. type DotenvConfigOptions = {
  9. path?: string, // path to .env file
  10. encoding?: string, // encoding of .env file
  11. debug?: string // turn on logging for debugging purposes
  12. }
  13. type DotenvConfigOutput = {
  14. parsed?: DotenvParseOutput,
  15. error?: Error
  16. }
  17. */
  18. const fs = require('fs')
  19. const path = require('path')
  20. function log (message /*: string */) {
  21. console.log(`[dotenv][DEBUG] ${message}`)
  22. }
  23. const NEWLINE = '\n'
  24. const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/
  25. const RE_NEWLINES = /\\n/g
  26. const NEWLINES_MATCH = /\r\n|\n|\r/
  27. // Parses src into an Object
  28. function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {
  29. const debug = Boolean(options && options.debug)
  30. const obj = {}
  31. // convert Buffers before splitting into lines and processing
  32. src.toString().split(NEWLINES_MATCH).forEach(function (line, idx) {
  33. // matching "KEY' and 'VAL' in 'KEY=VAL'
  34. const keyValueArr = line.match(RE_INI_KEY_VAL)
  35. // matched?
  36. if (keyValueArr != null) {
  37. const key = keyValueArr[1]
  38. // default undefined or missing values to empty string
  39. let val = (keyValueArr[2] || '')
  40. const end = val.length - 1
  41. const isDoubleQuoted = val[0] === '"' && val[end] === '"'
  42. const isSingleQuoted = val[0] === "'" && val[end] === "'"
  43. // if single or double quoted, remove quotes
  44. if (isSingleQuoted || isDoubleQuoted) {
  45. val = val.substring(1, end)
  46. // if double quoted, expand newlines
  47. if (isDoubleQuoted) {
  48. val = val.replace(RE_NEWLINES, NEWLINE)
  49. }
  50. } else {
  51. // remove surrounding whitespace
  52. val = val.trim()
  53. }
  54. obj[key] = val
  55. } else if (debug) {
  56. log(`did not match key and value when parsing line ${idx + 1}: ${line}`)
  57. }
  58. })
  59. return obj
  60. }
  61. // Populates process.env from .env file
  62. function config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */ {
  63. let dotenvPath = path.resolve(process.cwd(), '.env')
  64. let encoding /*: string */ = 'utf8'
  65. let debug = false
  66. if (options) {
  67. if (options.path != null) {
  68. dotenvPath = options.path
  69. }
  70. if (options.encoding != null) {
  71. encoding = options.encoding
  72. }
  73. if (options.debug != null) {
  74. debug = true
  75. }
  76. }
  77. try {
  78. // specifying an encoding returns a string instead of a buffer
  79. const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug })
  80. Object.keys(parsed).forEach(function (key) {
  81. if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
  82. process.env[key] = parsed[key]
  83. } else if (debug) {
  84. log(`"${key}" is already defined in \`process.env\` and will not be overwritten`)
  85. }
  86. })
  87. return { parsed }
  88. } catch (e) {
  89. return { error: e }
  90. }
  91. }
  92. module.exports.config = config
  93. module.exports.parse = parse