Skip to content

Commit

Permalink
fix: Resolving issue with safe config
Browse files Browse the repository at this point in the history
closes #160
  • Loading branch information
mrsteele committed Jan 18, 2019
1 parent 924800a commit 66ea8ec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST=nope
TEST2=hidefault
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"webpack": "^1 || ^2 || ^3 || ^4"
},
"dependencies": {
"dotenv": "^5.0.1",
"dotenv-defaults": "^1.0.2"
},
"devDependencies": {
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class Dotenv {
if (safe !== true) {
file = safe
}
blueprint = this.loadFile({ file, defaults, silent })
blueprint = this.loadFile({
file,
silent
})
}

Object.keys(blueprint).map(key => {
Expand All @@ -74,6 +77,11 @@ class Dotenv {
}
})

// add the leftovers
if (safe) {
Object.assign(vars, env)
}

const formatData = Object.keys(vars).reduce((obj, key) => {
const v = vars[key]
const vKey = `process.env.${key}`
Expand Down
2 changes: 2 additions & 0 deletions test/envs/.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TEST=nope
TEST2=youcanseethis
13 changes: 13 additions & 0 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const envMissingOneExample = path.resolve(__dirname, './envs/.missingone.example
const envSystemvars = path.resolve(__dirname, './envs/.systemvars')
const envSystemvarsExample = path.resolve(__dirname, './envs/.systemvars.example')
const envExpanded = path.resolve(__dirname, './envs/.expanded')
const envDefaults = path.resolve(__dirname, './envs/.defaults')

const buildExpectation = (obj) => Object.keys(obj).reduce((all, key) => {
all[`process.env.${key}`] = JSON.stringify(obj[key])
Expand All @@ -28,6 +29,8 @@ const envDefJson = buildExpectation({ TEST: 'hi' })
const envEmptyJson = buildExpectation({})
const envSimpleJson = buildExpectation({ TEST: 'testing' })
const envMissingOneJson = buildExpectation({ TEST: '', TEST2: 'Hello' })
const envDefaultsJson = buildExpectation({ TEST: 'hi', TEST2: 'hidefault' })
const envDefaultsJson2 = buildExpectation({ TEST: 'hi', TEST2: 'youcanseethis' })

/*
NODE_ENV=test
Expand Down Expand Up @@ -170,6 +173,16 @@ function runTests (Obj, name) {
})
})

describe('Defaults configuration', () => {
it('should support default configurations', () => {
envTest({ defaults: true }).should.deep.equal(envDefaultsJson)
})

it('should support string configurations', () => {
envTest({ defaults: envDefaults }).should.deep.equal(envDefaultsJson2)
})
})

describe('System variables', () => {
it('Should allow system env variables', () => {
const test = envTest({ path: envSimple, systemvars: true })
Expand Down

0 comments on commit 66ea8ec

Please sign in to comment.