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