From d6d26de328c020b8216ada3228e512ca5ab3fdb4 Mon Sep 17 00:00:00 2001 From: assafhp Date: Wed, 12 Apr 2017 17:12:48 +0300 Subject: [PATCH] added json support --- lib/main.js | 6 +++++- test/.env.json | 15 +++++++++++++++ test/main.js | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/.env.json diff --git a/lib/main.js b/lib/main.js index eb96e7fd..d7daf29c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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 @@ -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] diff --git a/test/.env.json b/test/.env.json new file mode 100644 index 00000000..af6e598d --- /dev/null +++ b/test/.env.json @@ -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" +} \ No newline at end of file diff --git a/test/main.js b/test/main.js index 7d6c3cf1..688cc87d 100644 --- a/test/main.js +++ b/test/main.js @@ -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})