• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const fs = require('fs')
4const path = require('path')
5
6/*
7 * Parses a string or buffer into an object
8 * @param {(string|Buffer)} src - source to be parsed
9 * @returns {Object} keys and values from src
10*/
11function parse (src) {
12  const obj = {}
13
14  // convert Buffers before splitting into lines and processing
15  src.toString().split('\n').forEach(function (line) {
16    // matching "KEY' and 'VAL' in 'KEY=VAL'
17    const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
18    // matched?
19    if (keyValueArr != null) {
20      const key = keyValueArr[1]
21
22      // default undefined or missing values to empty string
23      let value = keyValueArr[2] || ''
24
25      // expand newlines in quoted values
26      const len = value ? value.length : 0
27      if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
28        value = value.replace(/\\n/gm, '\n')
29      }
30
31      // remove any surrounding quotes and extra spaces
32      value = value.replace(/(^['"]|['"]$)/g, '').trim()
33
34      obj[key] = value
35    }
36  })
37
38  return obj
39}
40
41/*
42 * Main entry point into dotenv. Allows configuration before loading .env
43 * @param {Object} options - options for parsing .env file
44 * @param {string} [options.path=.env] - path to .env file
45 * @param {string} [options.encoding=utf8] - encoding of .env file
46 * @returns {Object} parsed object or error
47*/
48function config (options) {
49  let dotenvPath = path.resolve(process.cwd(), '.env')
50  let encoding = 'utf8'
51
52  if (options) {
53    if (options.path) {
54      dotenvPath = options.path
55    }
56    if (options.encoding) {
57      encoding = options.encoding
58    }
59  }
60
61  try {
62    // specifying an encoding returns a string instead of a buffer
63    const parsed = parse(fs.readFileSync(dotenvPath, { encoding }))
64
65    Object.keys(parsed).forEach(function (key) {
66      if (!process.env.hasOwnProperty(key)) {
67        process.env[key] = parsed[key]
68      }
69    })
70
71    return { parsed }
72  } catch (e) {
73    return { error: e }
74  }
75}
76
77module.exports.config = config
78module.exports.load = config
79module.exports.parse = parse
80