Skip to content

Commit

Permalink
feat(dotenv): read .env unless --no-dotenv option is given
Browse files Browse the repository at this point in the history
BREAKING CHANGE: before `defaultenv` wouldn't read `.env` unless it was passed by name or the
`--dotenv` option was given.  Now the default behavior is to read the `.env` file in the project
root directory unless you give the `--no-dotenv` option.
  • Loading branch information
jedwards1211 committed Aug 24, 2017
1 parent 5cc74d2 commit d203d2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ variable defaults.

## Usage

### `.env` file

If this is present in the root directory of your project, variables will be loaded from it using
[`dotenv`](https://www.npmjs.com/package/dotenv) unless you provide the `--no-dotenv` option.

### `dotenv`-style files

`defaultenv` uses [`dotenv`](https://www.npmjs.com/package/dotenv) to parse each file. Here's an example of what
Expand Down Expand Up @@ -155,8 +160,8 @@ Overwrite existing values for environment variables (by default, existing values
#### `-p`, `--print`
See [Exporting Values](#exporting-values)

#### `--dotenv`
also loads variables from a .env file in the root directory of your project (by using `require('dotenv').config()`)
#### `--no-dotenv`
Prevents loading variables from a .env file in the root directory of your project

### Example

Expand Down Expand Up @@ -190,7 +195,7 @@ foobar
declare function defaultEnv(files: Array<string>, options: {
force?: boolean,
print?: boolean,
dotenv?: boolean,
noDotenv?: boolean,
noExport?: boolean,
})
```
Expand All @@ -211,8 +216,8 @@ If true, overwrite existing values for environment variables (by default, existi
#### `print`
See [Exporting Values](#exporting-values)

#### `dotenv`
If true, also loads variables from a .env file in the root directory of your project
#### `noDotenv`
Unless this is true, also loads variables from a .env file in the root directory of your project
(by using `require('dotenv').config()`)

##### `noExport`
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function defaultEnv(envFiles, options) {
options = options || {}
var setvars = {}
var key
if (options.dotenv) {
if (!options.noDotenv) {
var parsed = dotenv.config().parsed
for (key in parsed) setvars[key] = parsed[key]
}
Expand Down Expand Up @@ -95,7 +95,7 @@ if (!module.parent) {
}

var options = {
dotenv: myArgs.indexOf('--dotenv') >= 0,
noDotenv: myArgs.indexOf('--no-dotenv') >= 0,
force: myArgs.indexOf('-f') >= 0 || myArgs.indexOf('--force') >= 0,
print: myArgs.indexOf('-p') >= 0 || myArgs.indexOf('--print') >= 0,
}
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

export type Options = {
force?: boolean,
dotenv?: boolean,
print?: boolean,
noDotenv?: boolean,
noExport?: boolean,
}

Expand Down
30 changes: 15 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ describe('defaultenv', function () {
describe('node.js API', function () {
it('puts vars on process.env unless noExport is true', function () {
expect(process.env.FOO).not.to.exist
defaultEnv([path.resolve(__dirname, 'foo.env')])
defaultEnv([path.resolve(__dirname, 'foo.env')], {noDotenv: true})
expect(process.env.FOO).to.equal('foo')
delete process.env.FOO

var vars = defaultEnv([path.resolve(__dirname, 'bar.env')], {noExport: true})
var vars = defaultEnv([path.resolve(__dirname, 'bar.env')], {noDotenv: true, noExport: true})
expect(process.env.BAR).not.to.exist
expect(vars.BAR).to.equal('bar')
})
Expand All @@ -32,7 +32,7 @@ describe('defaultenv', function () {
})
it('works for single env file', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.env ' +
'lib/index.js --no-dotenv test/foo.env ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -45,7 +45,7 @@ describe('defaultenv', function () {
})
it('works for single js file', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.js ' +
'lib/index.js --no-dotenv test/foo.js ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -58,7 +58,7 @@ describe('defaultenv', function () {
})
it('works for multiple js files', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.js test/bar.js -- ' +
'lib/index.js --no-dotenv test/foo.js test/bar.js -- ' +
process.argv[0] + " -p 'process.env.FOOBAR'"
exec(command, {
cwd: root,
Expand All @@ -71,7 +71,7 @@ describe('defaultenv', function () {
})
it('requires js env files to export an object', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/notObject.js ' +
'lib/index.js --no-dotenv test/notObject.js ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -84,7 +84,7 @@ describe('defaultenv', function () {
})
it('requires all values exported from js env files to be strings', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/notStringValue.js ' +
'lib/index.js --no-dotenv test/notStringValue.js ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -97,7 +97,7 @@ describe('defaultenv', function () {
})
it('works for single json file', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.json ' +
'lib/index.js --no-dotenv test/foo.json ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -110,7 +110,7 @@ describe('defaultenv', function () {
})
it('works for multiple env files', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.env test/bar.env -- ' +
'lib/index.js --no-dotenv test/foo.env test/bar.env -- ' +
process.argv[0] + " -p 'process.env.FOO + process.env.BAR'"
exec(command, {
cwd: root,
Expand All @@ -136,7 +136,7 @@ describe('defaultenv', function () {
})
it("clobbers existing environment variables with -f option", function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js -f test/foo.env ' +
'lib/index.js --no-dotenv -f test/foo.env ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand All @@ -149,7 +149,7 @@ describe('defaultenv', function () {
})
it('mimics signal from spawned process', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.env ' +
'lib/index.js --no-dotenv test/foo.env ' +
process.argv[0] + ' test/signal.js'
exec(command, {
cwd: root,
Expand All @@ -162,7 +162,7 @@ describe('defaultenv', function () {
})
it('mimics code from spawned process', function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js test/foo.env ' +
'lib/index.js --no-dotenv test/foo.env ' +
process.argv[0] + " -e 'process.exit(1)'"
exec(command, {
cwd: root,
Expand All @@ -173,7 +173,7 @@ describe('defaultenv', function () {
})
})
it("outputs export script with -p option", function (done) {
var command = process.argv[0] + ' lib/index.js -p test/foo.env test/bar.js'
var command = process.argv[0] + ' lib/index.js --no-dotenv -p test/foo.env test/bar.js'
exec(command, {
cwd: root,
env: {FOO: 'bar'},
Expand All @@ -182,9 +182,9 @@ describe('defaultenv', function () {
done()
})
})
it("loads .env when --dotenv option is given", function (done) {
it("loads .env when --no-dotenv option is not given", function (done) {
var command = process.argv[0] + ' ' +
'lib/index.js --dotenv test/foo.env ' +
'lib/index.js test/foo.env ' +
process.argv[0] + " -p 'process.env.FOO'"
exec(command, {
cwd: root,
Expand Down

0 comments on commit d203d2a

Please sign in to comment.