Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .env.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"sample": "value1"
"sample": "value1",
"complex": {
"foo": 1234
}
}
37 changes: 23 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -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};
}
};
};
13 changes: 3 additions & 10 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -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');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -11,7 +11,7 @@
"url": "git+https://github.com/maxbeatty/dotenv-json.git"
},
"keywords": [
"dotenv"
"dotenv", "dotenv-json"
],
"author": "Max Beatty",
"license": "MIT",
Expand Down
3 changes: 3 additions & 0 deletions test/.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"changed_directory": "test_directory"
}
3 changes: 3 additions & 0 deletions test/.env.production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"current_filename": ".env.production.json"
}
14 changes: 14 additions & 0 deletions test/complex.test.js
Original file line number Diff line number Diff line change
@@ -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}');
10 changes: 10 additions & 0 deletions test/dir.test.js
Original file line number Diff line number Diff line change
@@ -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");
10 changes: 10 additions & 0 deletions test/environment.test.js
Original file line number Diff line number Diff line change
@@ -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');