Skip to content

Commit

Permalink
feat: Combines the 'safe' and 'sample' configuration property.
Browse files Browse the repository at this point in the history
closes #16
  • Loading branch information
mrsteele committed Sep 9, 2016
1 parent 6eeb6c5 commit 4348916
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ module.exports = {
Use the following properties to configure your instance.

* **path** (`'./env'`) - The path to your environment variables.
* **safe** (`false`) - If false, just load the variables, if true, load the file in the *sample* property.
* **sample** (`'./.env.example'`) - The sample file to use for validation.
* **safe** (`false`) - If false ignore safe-mode, if true load `'./.env.example'`, if a string load that file as the sample.
* **systemvars** (`false`) - Set to true if you would rather load all system variables as well (useful for CI purposes).

### LICENSE
Expand Down
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ class Dotenv {
options = Object.assign({
path: './.env',
safe: false,
sample: './.env.example',
systemvars: false
}, options)

if (options.sample) {
console.warn('dotend-webpack: "options.sample" is a deprecated property. Please update your configuration to use simple "options.safe".')
}

let vars = (options.systemvars) ? Object.assign({}, process.env) : {}
const env = this.loadFile(options.path)
const blueprint = (options.safe) ? this.loadFile(options.sample) : env

let blueprint = env
if (options.safe) {
let file = './.env.example'
if (options.safe !== true) {
file = options.safe
}
blueprint = this.loadFile(file)
}

Object.keys(blueprint).map(key => {
const value = env[key] || env[key]
Expand Down
8 changes: 4 additions & 4 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ function runTests (Obj, name) {

describe('Safe configuration', () => {
it('Should load successfully if variables defined', () => {
envTest({path: envEmpty, safe: true, sample: envEmptyExample}).should.deep.equal(envEmptyJson)
envTest({path: envSimple, safe: true, sample: envSimpleExample}).should.deep.equal(envSimpleJson)
envTest({path: envEmpty, safe: envEmptyExample}).should.deep.equal(envEmptyJson)
envTest({path: envSimple, safe: envSimpleExample}).should.deep.equal(envSimpleJson)
})

it('Should fail if env does not match sample.', () => {
function errorTest () {
envTest({path: envEmpty, safe: true, sample: envSimpleExample})
envTest({path: envEmpty, safe: envSimpleExample})
}

errorTest.should.throw('Missing environment variable')
Expand All @@ -76,7 +76,7 @@ function runTests (Obj, name) {

it('Should fail on safe mode', () => {
function errorTest () {
envTest({path: envMissingOne, safe: true, sample: envMissingOneExample})
envTest({path: envMissingOne, safe: envMissingOneExample})
}

errorTest.should.throw('Missing environment variable')
Expand Down

0 comments on commit 4348916

Please sign in to comment.