File tree Expand file tree Collapse file tree 4 files changed +57
-7
lines changed Expand file tree Collapse file tree 4 files changed +57
-7
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ Takes the suck out of launching your server with various sets of environment var
20
20
+ [ Exporting Values] ( #exporting-values )
21
21
+ [ Options] ( #options )
22
22
+ [ Example] ( #example )
23
+ + [ Node.js API] ( #node-js-api )
23
24
24
25
## Intro
25
26
@@ -183,3 +184,37 @@ test
183
184
foobar
184
185
```
185
186
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
+
Original file line number Diff line number Diff line change @@ -32,13 +32,15 @@ function defaultEnv(envFiles, options) {
32
32
}
33
33
for ( key in defaultValues ) {
34
34
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 ]
36
37
}
37
38
}
38
39
}
39
40
if ( options . print ) {
40
41
for ( key in setvars ) console . log ( 'export ' + key + '=' + setvars [ key ] ) // eslint-disable-line no-console
41
42
}
43
+ return setvars
42
44
}
43
45
44
46
function run ( command , args ) {
Original file line number Diff line number Diff line change 1
1
// @flow
2
2
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};
4
11
declare module.exports: typeof defaultEnv;
5
12
Original file line number Diff line number Diff line change @@ -8,11 +8,17 @@ var root = path.resolve(__dirname, '..')
8
8
9
9
describe ( 'defaultenv' , function ( ) {
10
10
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
+ } )
16
22
} )
17
23
it ( 'prints usage and exits with no args' , function ( done ) {
18
24
var command = process . argv [ 0 ] + ' lib/index.js'
You can’t perform that action at this time.
0 commit comments