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.js b/index.js index a2db67e..886e23d 100644 --- a/index.js +++ b/index.js @@ -1,20 +1,29 @@ -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', 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 envConfig = JSON.parse(jsonString); + const parsed = JSON.parse(readFileSync(path, {encoding})); + + const {env} = process + for (const key in parsed) { + if (!env.hasOwnProperty(key)) { + const value = parsed[key]; - for (const key in envConfig) { - process.env[key] = process.env[key] || envConfig[key]; + env[key] = typeof value === 'string' ? value : JSON.stringify(value); + } } - } catch (err) { - console.error(err); + + return {parsed}; + } catch (error) { + return {error}; } -}; +}; \ No newline at end of file diff --git a/index.test.js b/index.test.js index 8a6e165..39aa979 100644 --- a/index.test.js +++ b/index.test.js @@ -1,10 +1,3 @@ -const assert = require("assert"); - -const dotenvJSON = require("./index"); - -assert.ok(!process.env.hasOwnProperty("sample")); - -dotenvJSON(); // loads .env.json - -assert.ok(process.env.hasOwnProperty("sample")); -assert.equal(process.env.sample, "value1"); +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');