Skip to content

Commit 5cc74d2

Browse files
committed
feat(node-api): add noExport option
1 parent e68ec6a commit 5cc74d2

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Takes the suck out of launching your server with various sets of environment var
2020
+ [Exporting Values](#exporting-values)
2121
+ [Options](#options)
2222
+ [Example](#example)
23+
+ [Node.js API](#node-js-api)
2324

2425
## Intro
2526

@@ -183,3 +184,37 @@ test
183184
foobar
184185
```
185186

187+
### Node.js API
188+
189+
```js
190+
declare function defaultEnv(files: Array<string>, options: {
191+
force?: boolean,
192+
print?: boolean,
193+
dotenv?: boolean,
194+
noExport?: boolean,
195+
})
196+
```
197+
198+
```js
199+
var path = require('path')
200+
var defaultEnv = require('defaultEnv')
201+
202+
var defaults = defaultEnv([path.resolve('foo.js')], {noExport: true})
203+
```
204+
205+
#### Options
206+
207+
#### `force`
208+
If true, overwrite existing values for environment variables (by default, existing values will be preserved)
209+
**This does not currently apply to variables loaded via `--dotenv` option.**
210+
211+
#### `print`
212+
See [Exporting Values](#exporting-values)
213+
214+
#### `dotenv`
215+
If true, also loads variables from a .env file in the root directory of your project
216+
(by using `require('dotenv').config()`)
217+
218+
##### `noExport`
219+
If true, doesn't write to `process.env`; only returns the values that would be written.
220+

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ function defaultEnv(envFiles, options) {
3232
}
3333
for (key in defaultValues) {
3434
if (!setvars.hasOwnProperty(key) && (!process.env.hasOwnProperty(key) || options.force || options.print)) {
35-
process.env[key] = setvars[key] = defaultValues[key]
35+
setvars[key] = defaultValues[key]
36+
if (!options.noExport) process.env[key] = defaultValues[key]
3637
}
3738
}
3839
}
3940
if (options.print) {
4041
for (key in setvars) console.log('export ' + key + '=' + setvars[key]) // eslint-disable-line no-console
4142
}
43+
return setvars
4244
}
4345

4446
function run(command, args) {

lib/index.js.flow

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// @flow
22

3-
declare function defaultEnv(envFiles: Array<string>): void;
3+
export type Options = {
4+
force?: boolean,
5+
dotenv?: boolean,
6+
print?: boolean,
7+
noExport?: boolean,
8+
}
9+
10+
declare function defaultEnv(envFiles: Array<string>, options: Options): {[varname: string]: string};
411
declare module.exports: typeof defaultEnv;
512

test/index.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ var root = path.resolve(__dirname, '..')
88

99
describe('defaultenv', function () {
1010
this.timeout(5000)
11-
it('exports defaultEnv function', function () {
12-
expect(process.env.FOO).not.to.exist
13-
defaultEnv([path.resolve(__dirname, 'foo.env')])
14-
expect(process.env.FOO).to.equal('foo')
15-
delete process.env.FOO
11+
describe('node.js API', function () {
12+
it('puts vars on process.env unless noExport is true', function () {
13+
expect(process.env.FOO).not.to.exist
14+
defaultEnv([path.resolve(__dirname, 'foo.env')])
15+
expect(process.env.FOO).to.equal('foo')
16+
delete process.env.FOO
17+
18+
var vars = defaultEnv([path.resolve(__dirname, 'bar.env')], {noExport: true})
19+
expect(process.env.BAR).not.to.exist
20+
expect(vars.BAR).to.equal('bar')
21+
})
1622
})
1723
it('prints usage and exits with no args', function (done) {
1824
var command = process.argv[0] + ' lib/index.js'

0 commit comments

Comments
 (0)