Skip to content

Commit

Permalink
[breaking change] Use dotenv.parse(), replace options.loadDotEnv with…
Browse files Browse the repository at this point in the history
… options.dotEnvPath

This is a breaking change because the env vars in the `.env` file will
no longer be added to `process.env`. See discussion on #25
  • Loading branch information
af committed Feb 16, 2017
1 parent 98ae305 commit 93b86da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ positional arguments:
* `environment` - An object containing your env vars (eg. `process.env`)
* `validators` - An object that specifies the format of required vars.
* `options` - An (optional) object, which supports the following keys:
* `strict` - If true, the output of `cleanEnv` will *only* contain the env
vars that were specified in the `validators` argument.
* `strict` - (default: `false`) If true, the output of `cleanEnv` will *only*
contain the env vars that were specified in the `validators` argument.
* `reporter` - Pass in a function to override the default error handling and
console output. See `lib/reporter.js` for the default implementation.
* `transformer` - A function used to transform the cleaned environment object
before it is returned from `cleanEnv`
* `dotEnvPath` - (default: `'.env'`) Path to the file that is parsed by
[dotenv](https://github.com/motdotla/dotenv) to
optionally load more env vars at runtime. Pass `null` if you want
to skip `dotenv` processing entirely and only load from `process.env`.

By default, `cleanEnv()` will log an error message and exit if any required
env vars are missing or invalid.
Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('fs')
const dotenv = require('dotenv')
const { EnvError, EnvMissingError, makeValidator,
bool, num, str, json, url, email } = require('./lib/validators')
Expand Down Expand Up @@ -36,8 +37,17 @@ function formatSpecDescription(spec) {
return `${spec.desc}${egText}${docsText}` || ''
}

function extendWithDotEnv(inputEnv) {
const { parsed } = dotenv.config()
// Extend an env var object with the values parsed from a ".env"
// file, whose path is given by the second argument.
function extendWithDotEnv(inputEnv, dotEnvPath = '.env') {
let dotEnvBuffer = null
try {
dotEnvBuffer = fs.readFileSync(dotEnvPath)
} catch (err) {
if (err.code === 'ENOENT') return inputEnv
throw err
}
const parsed = dotenv.parse(dotEnvBuffer)
return extend(parsed, inputEnv)
}

Expand All @@ -46,7 +56,9 @@ function cleanEnv(inputEnv, specs = {}, options = {}) {
let defaultNodeEnv = ''
const errors = {}
const shouldLoadDotEnv = (options.loadDotEnv !== false)
const env = shouldLoadDotEnv ? extendWithDotEnv(inputEnv) : inputEnv
const env = (options.dotEnvPath !== null)
? extendWithDotEnv(inputEnv, options.dotEnvPath)
: inputEnv
const varKeys = Object.keys(specs)

// If validation for NODE_ENV isn't specified, use the default validation:
Expand Down
13 changes: 11 additions & 2 deletions tests/test_dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ test('.env test in strict mode', () => {
assert.deepEqual(env, { MYNUM: 4 })
})

test('can opt out of dotenv with loadDotEnv=false', () => {
const env = cleanEnv({ FOO: 'bar' }, {}, { loadDotEnv: false })
test('can opt out of dotenv with dotEnvPath=null', () => {
const env = cleanEnv({ FOO: 'bar' }, {}, { dotEnvPath: null })
assert.deepEqual(env, { FOO: 'bar' })
})

test('can use a custom .env file name', () => {
const path = '.custom-env'
fs.writeFileSync(path, 'CUSTOM=hi')

const env = cleanEnv({ FOO: 'bar' }, {}, { dotEnvPath: path })
assert.deepEqual(env, { FOO: 'bar', CUSTOM: 'hi' })
fs.unlinkSync(path)
})

0 comments on commit 93b86da

Please sign in to comment.