@jdeighan/env 5.1.10 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@jdeighan/env",
3
3
  "type": "module",
4
- "version": "5.1.10",
4
+ "version": "6.0.0",
5
5
  "description": "enhanced syntax for .env files",
6
6
  "main": "./src/EnvLoaderEx.js",
7
7
  "exports": {
8
- ".": "./src/EnvLoaderEx.js",
9
- "./lib": "./src/EnvLib.js"
8
+ ".": "./src/EnvLoaderEx.js"
10
9
  },
11
10
  "engines": {
12
11
  "node": ">=12.0.0"
@@ -32,8 +31,8 @@
32
31
  },
33
32
  "homepage": "https://github.com/johndeighan/env#readme",
34
33
  "dependencies": {
35
- "@jdeighan/coffee-utils": "^3.0.7",
36
- "@jdeighan/string-input": "^6.0.6",
34
+ "@jdeighan/coffee-utils": "^4.0.0",
35
+ "@jdeighan/string-input": "^7.0.0",
37
36
  "coffeescript": "^2.6.1",
38
37
  "cross-env": "^7.0.3"
39
38
  }
@@ -9,8 +9,24 @@ import {
9
9
  import {log} from '@jdeighan/coffee-utils/log'
10
10
  import {debug} from '@jdeighan/coffee-utils/debug'
11
11
  import {slurp, pathTo, mkpath} from '@jdeighan/coffee-utils/fs'
12
+ import {hPrivEnv, hPrivEnvCallbacks} from '@jdeighan/coffee-utils/privenv'
12
13
  import {PLLParser} from '@jdeighan/string-input'
13
14
 
15
+ hDefCallbacks = {
16
+ getVar: (name) ->
17
+ return process.env[name]
18
+ setVar: (name, value) ->
19
+ process.env[name] = value
20
+ return
21
+ clearVar: (name) ->
22
+ delete process.env[name]
23
+ return
24
+ clearAll: () ->
25
+ process.env ={}
26
+ names: () ->
27
+ return Object.keys(process.env)
28
+ }
29
+
14
30
  # ---------------------------------------------------------------------------
15
31
 
16
32
  export class EnvLoader extends PLLParser
@@ -25,7 +41,10 @@ export class EnvLoader extends PLLParser
25
41
 
26
42
  super contents
27
43
  {@prefix, @stripPrefix, @hCallbacks} = hOptions
28
- @checkCallbacks()
44
+ if @hCallbacks?
45
+ @checkCallbacks()
46
+ else
47
+ @hCallbacks = hDefCallbacks
29
48
 
30
49
  # ..........................................................
31
50
 
@@ -44,51 +63,34 @@ export class EnvLoader extends PLLParser
44
63
 
45
64
  getVar: (name) ->
46
65
 
47
- if @hCallbacks
48
- return @hCallbacks.getVar(name)
49
- else
50
- return process.env[name]
51
- return
66
+ return @hCallbacks.getVar(name)
52
67
 
53
68
  # ..........................................................
54
69
 
55
70
  setVar: (name, value) ->
56
71
 
57
- if @hCallbacks
58
- @hCallbacks.setVar name, value
59
- else
60
- process.env[name] = value
72
+ @hCallbacks.setVar name, value
61
73
  return
62
74
 
63
75
  # ..........................................................
64
76
 
65
77
  clearVar: (name) ->
66
78
 
67
- if @hCallbacks
68
- @hCallbacks.clearVar name
69
- else
70
- delete process.env[name]
79
+ @hCallbacks.clearVar name
71
80
  return
72
81
 
73
82
  # ..........................................................
74
83
 
75
84
  clearAll: () ->
76
85
 
77
- if @hCallbacks
78
- @hCallbacks.clearAll
79
- else
80
- process.env = {}
86
+ @hCallbacks.clearAll
81
87
  return
82
88
 
83
89
  # ..........................................................
84
90
 
85
91
  names: () ->
86
92
 
87
- if @hCallbacks
88
- return @hCallbacks.names()
89
- else
90
- return Object.keys(process.env)
91
- return
93
+ return @hCallbacks.names()
92
94
 
93
95
  # ..........................................................
94
96
 
@@ -252,14 +254,6 @@ export class EnvLoader extends PLLParser
252
254
  return
253
255
 
254
256
  # ---------------------------------------------------------------------------
255
- # ---------------------------------------------------------------------------
256
- # Load environment from a file
257
-
258
- export loadEnvFile = (filepath, hOptions={}) ->
259
-
260
- debug "LOADENV #{filepath}"
261
- return loadEnvString slurp(filepath), hOptions
262
-
263
257
  # ---------------------------------------------------------------------------
264
258
  # Load environment from a string
265
259
 
@@ -269,7 +263,16 @@ export loadEnvString = (contents, hOptions={}) ->
269
263
  env = new EnvLoader(contents, hOptions)
270
264
  env.load()
271
265
  debug "return from loadEnvString()"
272
- return env
266
+ return
267
+
268
+ # ---------------------------------------------------------------------------
269
+ # Load environment from a file
270
+
271
+ export loadEnvFile = (filepath, hOptions={}) ->
272
+
273
+ debug "LOADENV #{filepath}"
274
+ loadEnvString slurp(filepath), hOptions
275
+ return
273
276
 
274
277
  # ---------------------------------------------------------------------------
275
278
  # Load environment from .env file
@@ -277,6 +280,7 @@ export loadEnvString = (contents, hOptions={}) ->
277
280
  export loadEnvFrom = (searchDir, rootName='DIR_ROOT', hOptions={}) ->
278
281
  # --- valid options:
279
282
  # onefile - load only the first file found
283
+ # hCallbacks - getVar, setVar, clearVar, clearAll, names
280
284
 
281
285
  debug "enter loadEnvFrom('#{searchDir}', rootName=#{rootName})"
282
286
  path = pathTo('.env', searchDir, "up")
@@ -284,22 +288,40 @@ export loadEnvFrom = (searchDir, rootName='DIR_ROOT', hOptions={}) ->
284
288
  debug "return from loadEnvFrom() - no .env file found"
285
289
  return
286
290
  debug "found .env file: #{path}"
287
- lPaths = [path]
288
291
 
289
292
  # --- Don't set root directory if it's already defined
290
293
  if rootName && not process.env[rootName]
291
294
  root = mkpath(rtrunc(path, 5))
292
295
  debug "set env var #{rootName} to #{root}"
293
- process.env[rootName] = root
296
+ if hOptions.hCallbacks
297
+ hOptions.hCallbacks.setVar(rootName, root)
298
+ else
299
+ hDefCallbacks.setVar(rootName, root)
294
300
 
301
+ lPaths = [path] # --- build an array of paths
295
302
  if not hOptions.onefile
296
303
  # --- search upward for .env files, but process top down
297
304
  while path = pathTo('.env', resolve(rtrunc(path, 5), '..'), "up")
298
305
  debug "found .env file: #{path}"
299
306
  lPaths.unshift path
300
307
 
301
- hEnv = {}
302
308
  for path in lPaths
303
- hEnv = Object.assign(hEnv, loadEnvFile(path, hOptions))
309
+ loadEnvFile(path, hOptions)
304
310
  debug "return from loadEnvFrom()"
305
- return hEnv
311
+ return
312
+
313
+ # ---------------------------------------------------------------------------
314
+ # Instead of loading into process.env,
315
+ # this loads into hPrivEnv from '@jdeighan/coffee-utils/privenv'
316
+
317
+ export loadPrivEnvFrom = (searchDir, rootName='DIR_ROOT', hInit=undef) ->
318
+
319
+ hPrivEnvCallbacks.clearAll()
320
+
321
+ # --- Load any vars found in hInit
322
+ if hInit?
323
+ for name,value of hInit
324
+ hPrivEnvCallbacks.setVar name, value
325
+
326
+ loadEnvFrom(searchDir, rootName, {hCallbacks: hPrivEnvCallbacks})
327
+ return
@@ -1,5 +1,7 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
- // EnvLoaderEx.coffee
2
+ // EnvLoaderEx.coffee
3
+ var hDefCallbacks;
4
+
3
5
  import {
4
6
  strict as assert
5
7
  } from 'assert';
@@ -35,10 +37,33 @@ import {
35
37
  mkpath
36
38
  } from '@jdeighan/coffee-utils/fs';
37
39
 
40
+ import {
41
+ hPrivEnv,
42
+ hPrivEnvCallbacks
43
+ } from '@jdeighan/coffee-utils/privenv';
44
+
38
45
  import {
39
46
  PLLParser
40
47
  } from '@jdeighan/string-input';
41
48
 
49
+ hDefCallbacks = {
50
+ getVar: function(name) {
51
+ return process.env[name];
52
+ },
53
+ setVar: function(name, value) {
54
+ process.env[name] = value;
55
+ },
56
+ clearVar: function(name) {
57
+ delete process.env[name];
58
+ },
59
+ clearAll: function() {
60
+ return process.env = {};
61
+ },
62
+ names: function() {
63
+ return Object.keys(process.env);
64
+ }
65
+ };
66
+
42
67
  // ---------------------------------------------------------------------------
43
68
  export var EnvLoader = class EnvLoader extends PLLParser {
44
69
  constructor(contents, hOptions = {}) {
@@ -49,7 +74,11 @@ export var EnvLoader = class EnvLoader extends PLLParser {
49
74
  // getVar, setVar, clearVar, clearAll, names
50
75
  super(contents);
51
76
  ({prefix: this.prefix, stripPrefix: this.stripPrefix, hCallbacks: this.hCallbacks} = hOptions);
52
- this.checkCallbacks();
77
+ if (this.hCallbacks != null) {
78
+ this.checkCallbacks();
79
+ } else {
80
+ this.hCallbacks = hDefCallbacks;
81
+ }
53
82
  }
54
83
 
55
84
  // ..........................................................
@@ -72,47 +101,27 @@ export var EnvLoader = class EnvLoader extends PLLParser {
72
101
 
73
102
  // ..........................................................
74
103
  getVar(name) {
75
- if (this.hCallbacks) {
76
- return this.hCallbacks.getVar(name);
77
- } else {
78
- return process.env[name];
79
- }
104
+ return this.hCallbacks.getVar(name);
80
105
  }
81
106
 
82
107
  // ..........................................................
83
108
  setVar(name, value) {
84
- if (this.hCallbacks) {
85
- this.hCallbacks.setVar(name, value);
86
- } else {
87
- process.env[name] = value;
88
- }
109
+ this.hCallbacks.setVar(name, value);
89
110
  }
90
111
 
91
112
  // ..........................................................
92
113
  clearVar(name) {
93
- if (this.hCallbacks) {
94
- this.hCallbacks.clearVar(name);
95
- } else {
96
- delete process.env[name];
97
- }
114
+ this.hCallbacks.clearVar(name);
98
115
  }
99
116
 
100
117
  // ..........................................................
101
118
  clearAll() {
102
- if (this.hCallbacks) {
103
- this.hCallbacks.clearAll;
104
- } else {
105
- process.env = {};
106
- }
119
+ this.hCallbacks.clearAll;
107
120
  }
108
121
 
109
122
  // ..........................................................
110
123
  names() {
111
- if (this.hCallbacks) {
112
- return this.hCallbacks.names();
113
- } else {
114
- return Object.keys(process.env);
115
- }
124
+ return this.hCallbacks.names();
116
125
  }
117
126
 
118
127
  // ..........................................................
@@ -266,13 +275,6 @@ export var EnvLoader = class EnvLoader extends PLLParser {
266
275
  };
267
276
 
268
277
  // ---------------------------------------------------------------------------
269
- // ---------------------------------------------------------------------------
270
- // Load environment from a file
271
- export var loadEnvFile = function(filepath, hOptions = {}) {
272
- debug(`LOADENV ${filepath}`);
273
- return loadEnvString(slurp(filepath), hOptions);
274
- };
275
-
276
278
  // ---------------------------------------------------------------------------
277
279
  // Load environment from a string
278
280
  export var loadEnvString = function(contents, hOptions = {}) {
@@ -281,15 +283,22 @@ export var loadEnvString = function(contents, hOptions = {}) {
281
283
  env = new EnvLoader(contents, hOptions);
282
284
  env.load();
283
285
  debug("return from loadEnvString()");
284
- return env;
286
+ };
287
+
288
+ // ---------------------------------------------------------------------------
289
+ // Load environment from a file
290
+ export var loadEnvFile = function(filepath, hOptions = {}) {
291
+ debug(`LOADENV ${filepath}`);
292
+ loadEnvString(slurp(filepath), hOptions);
285
293
  };
286
294
 
287
295
  // ---------------------------------------------------------------------------
288
296
  // Load environment from .env file
289
297
  export var loadEnvFrom = function(searchDir, rootName = 'DIR_ROOT', hOptions = {}) {
290
- var hEnv, i, lPaths, len, path, root;
298
+ var i, lPaths, len, path, root;
291
299
  // --- valid options:
292
300
  // onefile - load only the first file found
301
+ // hCallbacks - getVar, setVar, clearVar, clearAll, names
293
302
  debug(`enter loadEnvFrom('${searchDir}', rootName=${rootName})`);
294
303
  path = pathTo('.env', searchDir, "up");
295
304
  if (path == null) {
@@ -297,13 +306,17 @@ export var loadEnvFrom = function(searchDir, rootName = 'DIR_ROOT', hOptions = {
297
306
  return;
298
307
  }
299
308
  debug(`found .env file: ${path}`);
300
- lPaths = [path];
301
309
  // --- Don't set root directory if it's already defined
302
310
  if (rootName && !process.env[rootName]) {
303
311
  root = mkpath(rtrunc(path, 5));
304
312
  debug(`set env var ${rootName} to ${root}`);
305
- process.env[rootName] = root;
313
+ if (hOptions.hCallbacks) {
314
+ hOptions.hCallbacks.setVar(rootName, root);
315
+ } else {
316
+ hDefCallbacks.setVar(rootName, root);
317
+ }
306
318
  }
319
+ lPaths = [path]; // --- build an array of paths
307
320
  if (!hOptions.onefile) {
308
321
  // --- search upward for .env files, but process top down
309
322
  while (path = pathTo('.env', resolve(rtrunc(path, 5), '..'), "up")) {
@@ -311,11 +324,27 @@ export var loadEnvFrom = function(searchDir, rootName = 'DIR_ROOT', hOptions = {
311
324
  lPaths.unshift(path);
312
325
  }
313
326
  }
314
- hEnv = {};
315
327
  for (i = 0, len = lPaths.length; i < len; i++) {
316
328
  path = lPaths[i];
317
- hEnv = Object.assign(hEnv, loadEnvFile(path, hOptions));
329
+ loadEnvFile(path, hOptions);
318
330
  }
319
331
  debug("return from loadEnvFrom()");
320
- return hEnv;
332
+ };
333
+
334
+ // ---------------------------------------------------------------------------
335
+ // Instead of loading into process.env,
336
+ // this loads into hPrivEnv from '@jdeighan/coffee-utils/privenv'
337
+ export var loadPrivEnvFrom = function(searchDir, rootName = 'DIR_ROOT', hInit = undef) {
338
+ var name, value;
339
+ hPrivEnvCallbacks.clearAll();
340
+ // --- Load any vars found in hInit
341
+ if (hInit != null) {
342
+ for (name in hInit) {
343
+ value = hInit[name];
344
+ hPrivEnvCallbacks.setVar(name, value);
345
+ }
346
+ }
347
+ loadEnvFrom(searchDir, rootName, {
348
+ hCallbacks: hPrivEnvCallbacks
349
+ });
321
350
  };
package/src/EnvLib.coffee DELETED
@@ -1,41 +0,0 @@
1
- # EnvLib.coffee
2
-
3
- import {strict as assert} from 'assert'
4
-
5
- import {undef} from '@jdeighan/coffee-utils'
6
- import {log} from '@jdeighan/coffee-utils/log'
7
- import {mkpath, slurp} from '@jdeighan/coffee-utils/fs'
8
- import {loadEnvFrom} from '@jdeighan/env'
9
-
10
- # NOTE: use by:
11
- # 1. call loadEnvLibFrom()
12
- # 2. import hEnv and use directly
13
- # NOTE: keys are case sensitive
14
- export hEnv = {}
15
-
16
- # ---------------------------------------------------------------------------
17
- # Define custom callbacks to use with loadEnvFrom
18
-
19
- hCallbacks = {
20
- getVar: (name) ->
21
- return hEnv[name]
22
- setVar: (name, value) ->
23
- hEnv[name] = value
24
- return
25
- clearVar: (name) ->
26
- delete hEnv[name]
27
- return
28
- clearAll: () ->
29
- hEnv = {}
30
- return
31
- names: () ->
32
- return Object.keys(hEnv)
33
- }
34
-
35
- # ---------------------------------------------------------------------------
36
-
37
- export loadEnvLibFrom = (searchDir, rootName='DIR_ROOT', hInit={}) ->
38
-
39
- hEnv = hInit # reset, if there's been a previous call
40
- loadEnvFrom(searchDir, rootName, {hCallbacks})
41
- return hEnv
package/src/EnvLib.js DELETED
@@ -1,57 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // EnvLib.coffee
3
- var hCallbacks;
4
-
5
- import {
6
- strict as assert
7
- } from 'assert';
8
-
9
- import {
10
- undef
11
- } from '@jdeighan/coffee-utils';
12
-
13
- import {
14
- log
15
- } from '@jdeighan/coffee-utils/log';
16
-
17
- import {
18
- mkpath,
19
- slurp
20
- } from '@jdeighan/coffee-utils/fs';
21
-
22
- import {
23
- loadEnvFrom
24
- } from '@jdeighan/env';
25
-
26
- // NOTE: use by:
27
- // 1. call loadEnvLibFrom()
28
- // 2. import hEnv and use directly
29
- // NOTE: keys are case sensitive
30
- export var hEnv = {};
31
-
32
- // ---------------------------------------------------------------------------
33
- // Define custom callbacks to use with loadEnvFrom
34
- hCallbacks = {
35
- getVar: function(name) {
36
- return hEnv[name];
37
- },
38
- setVar: function(name, value) {
39
- hEnv[name] = value;
40
- },
41
- clearVar: function(name) {
42
- delete hEnv[name];
43
- },
44
- clearAll: function() {
45
- hEnv = {};
46
- },
47
- names: function() {
48
- return Object.keys(hEnv);
49
- }
50
- };
51
-
52
- // ---------------------------------------------------------------------------
53
- export var loadEnvLibFrom = function(searchDir, rootName = 'DIR_ROOT', hInit = {}) {
54
- hEnv = hInit; // reset, if there's been a previous call
55
- loadEnvFrom(searchDir, rootName, {hCallbacks});
56
- return hEnv;
57
- };