main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Parses src into an Object
  27. function parse (src /*: string | Buffer */, options /*: ?DotenvParseOptions */) /*: DotenvParseOutput */ {
  28. const debug = Boolean(options && options.debug)
  29. const obj = {}
  30. // convert Buffers before splitting into lines and processing
  31. src.toString().split(NEWLINE).forEach(function (line, idx) {
  32. // matching "KEY' and 'VAL' in 'KEY=VAL'
  33. const keyValueArr = line.match(RE_INI_KEY_VAL)
  34. // matched?
  35. if (keyValueArr != null) {
  36. const key = keyValueArr[1]
  37. // default undefined or missing values to empty string
  38. let val = (keyValueArr[2] || '')
  39. const end = val.length - 1
  40. const isDoubleQuoted = val[0] === '"' && val[end] === '"'
  41. const isSingleQuoted = val[0] === "'" && val[end] === "'"
  42. // if single or double quoted, remove quotes
  43. if (isSingleQuoted || isDoubleQuoted) {
  44. val = val.substring(1, end)
  45. // if double quoted, expand newlines
  46. if (isDoubleQuoted) {
  47. val = val.replace(RE_NEWLINES, NEWLINE)
  48. }
  49. } else {
  50. // remove surrounding whitespace
  51. val = val.trim()
  52. }
  53. obj[key] = val
  54. } else if (debug) {
  55. log(`did not match key and value when parsing line ${idx + 1}: ${line}`)
  56. }
  57. })
  58. return obj
  59. }
  60. // Populates process.env from .env file
  61. function config (options /*: ?DotenvConfigOptions */) /*: DotenvConfigOutput */ {
  62. let dotenvPath = path.resolve(process.cwd(), '.env')
  63. let encoding /*: string */ = 'utf8'
  64. let debug = false
  65. if (options) {
  66. if (options.path != null) {
  67. dotenvPath = options.path
  68. }
  69. if (options.encoding != null) {
  70. encoding = options.encoding
  71. }
  72. if (options.debug != null) {
  73. debug = true
  74. }
  75. }
  76. try {
  77. // specifying an encoding returns a string instead of a buffer
  78. const parsed = parse(fs.readFileSync(dotenvPath, { encoding }), { debug })
  79. Object.keys(parsed).forEach(function (key) {
  80. if (!process.env.hasOwnProperty(key)) {
  81. process.env[key] = parsed[key]
  82. } else if (debug) {
  83. log(`"${key}" is already defined in \`process.env\` and will not be overwritten`)
  84. }
  85. })
  86. return { parsed }
  87. } catch (e) {
  88. return { error: e }
  89. }
  90. }
  91. module.exports.config = config
  92. module.exports.parse = parse