Skip to content

Commit

Permalink
feat(node-api): add noExport option
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Jul 29, 2017
1 parent e68ec6a commit 5cc74d2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Takes the suck out of launching your server with various sets of environment var
+ [Exporting Values](#exporting-values)
+ [Options](#options)
+ [Example](#example)
+ [Node.js API](#node-js-api)

## Intro

Expand Down Expand Up @@ -183,3 +184,37 @@ test
foobar
```

### Node.js API

```js
declare function defaultEnv(files: Array<string>, options: {
force?: boolean,
print?: boolean,
dotenv?: boolean,
noExport?: boolean,
})
```

```js
var path = require('path')
var defaultEnv = require('defaultEnv')

var defaults = defaultEnv([path.resolve('foo.js')], {noExport: true})
```

#### Options

#### `force`
If true, overwrite existing values for environment variables (by default, existing values will be preserved)
**This does not currently apply to variables loaded via `--dotenv` option.**

#### `print`
See [Exporting Values](#exporting-values)

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

##### `noExport`
If true, doesn't write to `process.env`; only returns the values that would be written.

4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ function defaultEnv(envFiles, options) {
}
for (key in defaultValues) {
if (!setvars.hasOwnProperty(key) && (!process.env.hasOwnProperty(key) || options.force || options.print)) {
process.env[key] = setvars[key] = defaultValues[key]
setvars[key] = defaultValues[key]
if (!options.noExport) process.env[key] = defaultValues[key]
}
}
}
if (options.print) {
for (key in setvars) console.log('export ' + key + '=' + setvars[key]) // eslint-disable-line no-console
}
return setvars
}

function run(command, args) {
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js.flow
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// @flow

declare function defaultEnv(envFiles: Array<string>): void;
export type Options = {
force?: boolean,
dotenv?: boolean,
print?: boolean,
noExport?: boolean,
}

declare function defaultEnv(envFiles: Array<string>, options: Options): {[varname: string]: string};
declare module.exports: typeof defaultEnv;

16 changes: 11 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ var root = path.resolve(__dirname, '..')

describe('defaultenv', function () {
this.timeout(5000)
it('exports defaultEnv function', function () {
expect(process.env.FOO).not.to.exist
defaultEnv([path.resolve(__dirname, 'foo.env')])
expect(process.env.FOO).to.equal('foo')
delete process.env.FOO
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')])
expect(process.env.FOO).to.equal('foo')
delete process.env.FOO

var vars = defaultEnv([path.resolve(__dirname, 'bar.env')], {noExport: true})
expect(process.env.BAR).not.to.exist
expect(vars.BAR).to.equal('bar')
})
})
it('prints usage and exits with no args', function (done) {
var command = process.argv[0] + ' lib/index.js'
Expand Down

0 comments on commit 5cc74d2

Please sign in to comment.