env-create
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -1,6 +0,6 @@ | ||
const fs = require("fs") | ||
const path = require("path") | ||
const fs = require('fs') | ||
const path = require('path') | ||
const DEFAULT_ENV_FILENAME = ".env.json" | ||
const DEFAULT_ENCODING = "utf8" | ||
const DEFAULT_ENV_FILENAME = '.env.json' | ||
const DEFAULT_ENCODING = 'utf8' | ||
@@ -14,9 +14,10 @@ /** | ||
const load = (options) => { | ||
const debug = Boolean(options && options.debug) | ||
const jsonPath = (options && options.path) || getDefaultPath() | ||
let jsonPath = (options && options.path) || getDefaultPath() | ||
const encoding = (options && options.encoding) || DEFAULT_ENCODING | ||
if (debug) {console.log(">>>debug mode<<<")} | ||
if (jsonPath.length < 1) {jsonPath = getDefaultPath()} | ||
if (debug) {console.log('>>>debug mode<<<')} | ||
try { | ||
@@ -29,2 +30,3 @@ const envString = fs.readFileSync(jsonPath, encoding) | ||
process.env[key] = JSON.stringify(envJson[key]) | ||
if (debug) {console.log(`>>>Creating ${key}`)} | ||
} else if (debug) { | ||
@@ -31,0 +33,0 @@ console.log(`"${key}" is already defined in \`process.env\` and will not be overwritten`) |
@@ -1,15 +0,15 @@ | ||
const fs = require("fs") | ||
const chai = require("chai") | ||
const fs = require('fs') | ||
const chai = require('chai') | ||
chai.should() | ||
const sandbox = require("sinon").createSandbox() | ||
const envCreate = require("./main") | ||
const sandbox = require('sinon').createSandbox() | ||
const envCreate = require('./main') | ||
let readFileSyncStub | ||
const topObject1 = "{\"name\":\"secret name\"}" | ||
const topObject2 = "{\"id\":\"secret id\"}" | ||
const topObject1 = '{"name":"secret name"}' | ||
const topObject2 = '{"id":"secret id"}' | ||
const mockReadResponse = `{"secret1":${topObject1}, "secret2": ${topObject2}}` | ||
describe("load() should", () => { | ||
describe('load() should', () => { | ||
beforeEach(() => { | ||
readFileSyncStub = sandbox.stub(fs, "readFileSync").returns(mockReadResponse) | ||
readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns(mockReadResponse) | ||
}) | ||
@@ -21,3 +21,3 @@ | ||
it("return status:true when everything works", () => { | ||
it('return status:true when everything works', () => { | ||
const result = envCreate.load() | ||
@@ -27,3 +27,3 @@ result.status.should.be.true | ||
it("have default filename when no parameter passed", () => { | ||
it('have default filename when no parameter passed', () => { | ||
envCreate.load() | ||
@@ -33,4 +33,9 @@ readFileSyncStub.args[0][0].should.contain(envCreate.DEFAULT_ENV_FILENAME) | ||
it("have passed path when option parameter is supplied", () => { | ||
const fakePath = "../fake.json" | ||
it('have default filename when path property is empty', () => { | ||
envCreate.load({path: ''}) | ||
readFileSyncStub.args[0][0].should.contain(envCreate.DEFAULT_ENV_FILENAME) | ||
}) | ||
it('have passed path when option parameter is supplied', () => { | ||
const fakePath = '../fake.json' | ||
envCreate.load({path: fakePath}) | ||
@@ -40,4 +45,4 @@ readFileSyncStub.args[0][0].should.contain(fakePath) | ||
it("should have passed encoding when supplied", () => { | ||
const fakeEncoding = "utf16" | ||
it('should have passed encoding when supplied', () => { | ||
const fakeEncoding = 'utf16' | ||
envCreate.load({encoding: fakeEncoding}) | ||
@@ -47,3 +52,3 @@ readFileSyncStub.args[0][1].should.contain(fakeEncoding) | ||
it("creates the expected process.env variables", () => { | ||
it('creates the expected process.env variables', () => { | ||
envCreate.load() | ||
@@ -54,4 +59,4 @@ process.env.secret1.should.equal(topObject1) | ||
it("will not overwrite an existing property", () => { | ||
const preExists = "I am therefore I am" | ||
it('will not overwrite an existing property', () => { | ||
const preExists = 'I am therefore I am' | ||
process.env.secret1 = preExists | ||
@@ -62,7 +67,8 @@ envCreate.load({debug: true}) | ||
it("returns error value when it throws", () => { | ||
it('returns error value when it throws', () => { | ||
readFileSyncStub.throws() | ||
const getError = envCreate.load() | ||
getError.error.should.be.instanceOf(Error) | ||
getError.status.should.be.false | ||
}) | ||
}) |
{ | ||
"name": "env-create", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Read in a .env.json file that contains valid JSON and assign top level properties to environment variables", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -14,3 +14,3 @@ # env-create | ||
## Basic use | ||
## Basic usage | ||
@@ -40,3 +40,3 @@ Let's assume you have a `.env.json` at the root level of your project with the following contents | ||
# Option usage | ||
## Option usage | ||
@@ -50,2 +50,7 @@ Using a relative path to go up one folder out of your project and into an ENV_VARS folder to get the file named `gsweet.env.json` | ||
``` | ||
You can also use an absolute path which is likely preferred if you store authentication data that is required among multiple projects | ||
```javascript | ||
require('env-create').load({path: "/User/yourUserName/ENV_VARS/gsweet.env.json", encode: "utf8", debug: "true"});) | ||
``` | ||
@@ -56,2 +61,1 @@ | ||
Inspired by [dotenv](https://github.com/motdotla/dotenv) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
9083
97
58