From 3393d99949424ea6e2994ecdb4e5e5753e8b8ff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=83=C2=BAs=20Legan=C3=83=C2=A9s-Combarro=20=27pirann?= =?UTF-8?q?a?= Date: Mon, 16 Apr 2018 22:36:34 +0200 Subject: [PATCH 1/4] Have a behaviour more alike to `dotenv` --- index.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index a2db67e..beb4e18 100644 --- a/index.js +++ b/index.js @@ -1,20 +1,22 @@ -const path = require("path"); -const fs = require("fs"); +const {readFileSync} = require("fs"); +const {resolve} = require("path"); -module.exports = function dotenvJSON(options) { - const jsonFile = (options && options.path) || ".env.json"; - const jsonString = fs.readFileSync(path.resolve(process.cwd(), jsonFile), { - encoding: "utf8" - }); +module.exports = function dotenvJSON({encoding = 'utf8', path} = {}) { + if(!path) path = resolve(process.cwd(), '.env.json'); try { - const envConfig = JSON.parse(jsonString); + const parsed = JSON.parse(readFileSync(path, {encoding})); - for (const key in envConfig) { - process.env[key] = process.env[key] || envConfig[key]; + const {env} = process + for (const key in parsed) { + if (!env.hasOwnProperty(key)) { + env[key] = parsed[key]; + } } - } catch (err) { - console.error(err); + + return {parsed}; + } catch (error) { + return {error}; } }; From 341876f23a48f1c0841a535ca8ef20f490ea992e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Mon, 16 Apr 2018 22:47:12 +0200 Subject: [PATCH 2/4] Stringify not-string objects in `process.env` --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index beb4e18..2d6be40 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,9 @@ module.exports = function dotenvJSON({encoding = 'utf8', path} = {}) { const {env} = process for (const key in parsed) { if (!env.hasOwnProperty(key)) { - env[key] = parsed[key]; + const value = parsed[key]; + + env[key] = typeof value === 'string' ? value : JSON.stringify(value); } } From 121cc32faa3db955a092198d991fc638e2cf0752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Mon, 16 Apr 2018 22:51:05 +0200 Subject: [PATCH 3/4] Test for JSON stringify in `process.env` --- .env.json | 5 ++++- index.test.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.env.json b/.env.json index 9f1e965..79d65d9 100644 --- a/.env.json +++ b/.env.json @@ -1,3 +1,6 @@ { - "sample": "value1" + "sample": "value1", + "complex": { + "foo": 1234 + } } diff --git a/index.test.js b/index.test.js index 8a6e165..e01f144 100644 --- a/index.test.js +++ b/index.test.js @@ -3,8 +3,12 @@ const assert = require("assert"); const dotenvJSON = require("./index"); assert.ok(!process.env.hasOwnProperty("sample")); +assert.ok(!process.env.hasOwnProperty("complex")); dotenvJSON(); // loads .env.json assert.ok(process.env.hasOwnProperty("sample")); assert.equal(process.env.sample, "value1"); + +assert.ok(process.env.hasOwnProperty("complex")); +assert.equal(process.env.complex, '{"foo":1234}'); From 391d1ab06cba7072780479bf36aabd268f964cc7 Mon Sep 17 00:00:00 2001 From: Fred Kim Date: Mon, 10 Sep 2018 14:36:52 +0900 Subject: [PATCH 4/4] Add options: dir, env - dir: specific location of directory. - env: specific `.env` file with related filename. (i.e. `.env.production.json`) --- index.js | 13 +++++++++---- index.test.js | 17 +++-------------- package.json | 4 ++-- test/.env.json | 3 +++ test/.env.production.json | 3 +++ test/complex.test.js | 14 ++++++++++++++ test/dir.test.js | 10 ++++++++++ test/environment.test.js | 10 ++++++++++ 8 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 test/.env.json create mode 100644 test/.env.production.json create mode 100644 test/complex.test.js create mode 100644 test/dir.test.js create mode 100644 test/environment.test.js diff --git a/index.js b/index.js index 2d6be40..886e23d 100644 --- a/index.js +++ b/index.js @@ -2,9 +2,14 @@ const {readFileSync} = require("fs"); const {resolve} = require("path"); -module.exports = function dotenvJSON({encoding = 'utf8', path} = {}) { - if(!path) path = resolve(process.cwd(), '.env.json'); - +module.exports = function dotenvJSON({encoding = 'utf8', env, dir, path} = {}) { + let dirpath = process.cwd(); + let filename = '.env.json'; + + if (env) filename = env === 'development' ? filename : `.env.${env}.json`; + if (dir) dirpath = resolve(dirpath, dir); + if (!path) path = resolve(dirpath, filename); + try { const parsed = JSON.parse(readFileSync(path, {encoding})); @@ -21,4 +26,4 @@ module.exports = function dotenvJSON({encoding = 'utf8', path} = {}) { } catch (error) { return {error}; } -}; +}; \ No newline at end of file diff --git a/index.test.js b/index.test.js index e01f144..39aa979 100644 --- a/index.test.js +++ b/index.test.js @@ -1,14 +1,3 @@ -const assert = require("assert"); - -const dotenvJSON = require("./index"); - -assert.ok(!process.env.hasOwnProperty("sample")); -assert.ok(!process.env.hasOwnProperty("complex")); - -dotenvJSON(); // loads .env.json - -assert.ok(process.env.hasOwnProperty("sample")); -assert.equal(process.env.sample, "value1"); - -assert.ok(process.env.hasOwnProperty("complex")); -assert.equal(process.env.complex, '{"foo":1234}'); +require('./test/complex.test'); +require('./test/dir.test'); +require('./test/environment.test'); \ No newline at end of file diff --git a/package.json b/package.json index c47ffba..870307b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotenv-json", - "version": "1.0.0", + "version": "1.1.0", "description": "Load environment variables via a JSON file", "main": "index.js", "scripts": { @@ -11,7 +11,7 @@ "url": "git+https://github.com/maxbeatty/dotenv-json.git" }, "keywords": [ - "dotenv" + "dotenv", "dotenv-json" ], "author": "Max Beatty", "license": "MIT", diff --git a/test/.env.json b/test/.env.json new file mode 100644 index 0000000..88765b5 --- /dev/null +++ b/test/.env.json @@ -0,0 +1,3 @@ +{ + "changed_directory": "test_directory" +} diff --git a/test/.env.production.json b/test/.env.production.json new file mode 100644 index 0000000..dbdff89 --- /dev/null +++ b/test/.env.production.json @@ -0,0 +1,3 @@ +{ + "current_filename": ".env.production.json" +} diff --git a/test/complex.test.js b/test/complex.test.js new file mode 100644 index 0000000..ddeffaf --- /dev/null +++ b/test/complex.test.js @@ -0,0 +1,14 @@ +const assert = require("assert"); + +const dotenvJSON = require("../index"); + +assert.ok(!process.env.hasOwnProperty("sample")); +assert.ok(!process.env.hasOwnProperty("complex")); + +dotenvJSON(); // loads .env.json + +assert.ok(process.env.hasOwnProperty("sample")); +assert.equal(process.env.sample, "value1"); + +assert.ok(process.env.hasOwnProperty("complex")); +assert.equal(process.env.complex, '{"foo":1234}'); diff --git a/test/dir.test.js b/test/dir.test.js new file mode 100644 index 0000000..647bff5 --- /dev/null +++ b/test/dir.test.js @@ -0,0 +1,10 @@ +const assert = require("assert"); + +const dotenvJSON = require("../index"); + +assert.ok(!process.env.hasOwnProperty("changed_directory")); + +dotenvJSON({dir: 'test'}); // loads test/.env.json + +assert.ok(process.env.hasOwnProperty("changed_directory")); +assert.equal(process.env.changed_directory, "test_directory"); diff --git a/test/environment.test.js b/test/environment.test.js new file mode 100644 index 0000000..337901f --- /dev/null +++ b/test/environment.test.js @@ -0,0 +1,10 @@ +const assert = require("assert"); + +const dotenvJSON = require("../index"); + +assert.ok(!process.env.hasOwnProperty("current_filename")); + +dotenvJSON({dir: 'test', env: 'production'}); // loads test/.env.production.json + +assert.ok(process.env.hasOwnProperty("current_filename")); +assert.equal(process.env.current_filename, '.env.production.json');