Skip to content

Commit

Permalink
Merge 91d4b57 into 825c1b2
Browse files Browse the repository at this point in the history
  • Loading branch information
assafshp committed Apr 12, 2017
2 parents 825c1b2 + 91d4b57 commit 22b093b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ function parse (src) {
function config (options) {
var path = '.env'
var encoding = 'utf8'
var parseFunc = parse

if (options) {
if (options.path) {
path = options.path
if (options.path.toLowerCase().endsWith('json')) {
parseFunc = JSON.parse
}
}
if (options.encoding) {
encoding = options.encoding
Expand All @@ -57,7 +61,7 @@ function config (options) {

try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = parse(fs.readFileSync(path, { encoding: encoding }))
var parsedObj = parseFunc(fs.readFileSync(path, { encoding: encoding }))

Object.keys(parsedObj).forEach(function (key) {
process.env[key] = process.env[key] || parsedObj[key]
Expand Down
15 changes: 15 additions & 0 deletions test/.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"NODE_ENV":"development",
"BASIC":"basic",
"AFTER_LINE":"after_line",
"UNDEFINED_EXPAND":"$TOTALLY_UNDEFINED_ENV_KEY",
"EMPTY":"",
"SINGLE_QUOTES":"single_quotes",
"DOUBLE_QUOTES":"double_quotes",
"EXPAND_NEWLINES":"expand\nnewlines",
"DONT_EXPAND_NEWLINES_1":"dontexpand\nnewlines",
"DONT_EXPAND_NEWLINES_2":"dontexpand\nnewlines",
"EQUAL_SIGNS":"equals==",
"INCLUDE_SPACE":"some spaced out string",
"USERNAME":"therealnerdybeast@example.tld"
}
8 changes: 8 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ describe('dotenv', function () {
done()
})

it('takes option for json file path', function (done) {
var testPath = 'test/.env.json'
dotenv.config({path: testPath})

readFileSyncStub.args[0][0].should.eql(testPath)
done()
})

it('takes option for encoding', function (done) {
var testEncoding = 'base64'
dotenv.config({encoding: testEncoding})
Expand Down

0 comments on commit 22b093b

Please sign in to comment.