@jdeighan/env 1.1.6 → 2.0.2
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 +3 -3
- package/src/EnvLoaderEx.coffee +32 -47
- package/src/EnvLoaderEx.js +39 -56
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdeighan/env",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.2",
|
|
5
5
|
"description": "enhanced syntax for .env files",
|
|
6
6
|
"main": "./src/EnvLoaderEx.js",
|
|
7
7
|
"exports": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/johndeighan/env#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@jdeighan/coffee-utils": "^1.
|
|
35
|
-
"@jdeighan/string-input": "^
|
|
34
|
+
"@jdeighan/coffee-utils": "^2.1.5",
|
|
35
|
+
"@jdeighan/string-input": "^3.0.3",
|
|
36
36
|
"coffeescript": "^2.5.1",
|
|
37
37
|
"cross-env": "^7.0.3"
|
|
38
38
|
}
|
package/src/EnvLoaderEx.coffee
CHANGED
|
@@ -4,8 +4,9 @@ import {strict as assert} from 'assert'
|
|
|
4
4
|
import {dirname, resolve, parse as parse_fname} from 'path';
|
|
5
5
|
|
|
6
6
|
import {
|
|
7
|
-
|
|
7
|
+
undef, pass, error, rtrim, isArray, isFunction, rtrunc, escapeStr,
|
|
8
8
|
} from '@jdeighan/coffee-utils'
|
|
9
|
+
import {log} from '@jdeighan/coffee-utils/log'
|
|
9
10
|
import {debug} from '@jdeighan/coffee-utils/debug'
|
|
10
11
|
import {slurp, pathTo, mkpath} from '@jdeighan/coffee-utils/fs'
|
|
11
12
|
import {PLLParser} from '@jdeighan/string-input'
|
|
@@ -14,9 +15,8 @@ import {PLLParser} from '@jdeighan/string-input'
|
|
|
14
15
|
|
|
15
16
|
export class EnvLoader extends PLLParser
|
|
16
17
|
|
|
17
|
-
constructor: (contents,
|
|
18
|
+
constructor: (contents, hOptions={}) ->
|
|
18
19
|
# --- Valid options:
|
|
19
|
-
# hInitialVars - hash of initial env var values
|
|
20
20
|
# prefix - load only vars with this prefix
|
|
21
21
|
# stripPrefix - remove the prefix before setting vars
|
|
22
22
|
# hCallbacks - callbacks to replace:
|
|
@@ -24,15 +24,8 @@ export class EnvLoader extends PLLParser
|
|
|
24
24
|
|
|
25
25
|
|
|
26
26
|
super contents
|
|
27
|
-
|
|
28
|
-
@prefix = @hOptions.prefix
|
|
29
|
-
@stripPrefix = @hOptions.stripPrefix
|
|
30
|
-
|
|
31
|
-
@hCallbacks = @hOptions.hCallbacks
|
|
27
|
+
{@prefix, @stripPrefix, @hCallbacks} = hOptions
|
|
32
28
|
@checkCallbacks()
|
|
33
|
-
if @hOptions.hInitialVars
|
|
34
|
-
for key,value of @hOptions.hInitialVars
|
|
35
|
-
@setVar key, value
|
|
36
29
|
|
|
37
30
|
# ..........................................................
|
|
38
31
|
|
|
@@ -101,15 +94,16 @@ export class EnvLoader extends PLLParser
|
|
|
101
94
|
|
|
102
95
|
dump: () ->
|
|
103
96
|
|
|
104
|
-
|
|
97
|
+
log "=== Environment Variables: ==="
|
|
105
98
|
for name in @names()
|
|
106
|
-
|
|
99
|
+
log " #{name} = '#{@getVar(name)}'"
|
|
107
100
|
return
|
|
108
101
|
|
|
109
102
|
# ..........................................................
|
|
110
103
|
|
|
111
|
-
|
|
104
|
+
mapNode: (str) ->
|
|
112
105
|
|
|
106
|
+
debug "enter mapNode('#{escapeStr(str)}')"
|
|
113
107
|
if lMatches = str.match(///^
|
|
114
108
|
([A-Za-z_\.]+) # identifier
|
|
115
109
|
\s*
|
|
@@ -122,7 +116,7 @@ export class EnvLoader extends PLLParser
|
|
|
122
116
|
return undef
|
|
123
117
|
if @stripPrefix
|
|
124
118
|
key = key.substring(@prefix.length)
|
|
125
|
-
|
|
119
|
+
result = {
|
|
126
120
|
type: 'assign',
|
|
127
121
|
key,
|
|
128
122
|
value: rtrim(value),
|
|
@@ -138,9 +132,9 @@ export class EnvLoader extends PLLParser
|
|
|
138
132
|
$///)
|
|
139
133
|
[_, neg, key] = lMatches
|
|
140
134
|
if neg
|
|
141
|
-
|
|
135
|
+
result = {type: 'if_falsy', key}
|
|
142
136
|
else
|
|
143
|
-
|
|
137
|
+
result = {type: 'if_truthy', key}
|
|
144
138
|
else if lMatches = str.match(///^
|
|
145
139
|
if
|
|
146
140
|
\s+
|
|
@@ -158,9 +152,16 @@ export class EnvLoader extends PLLParser
|
|
|
158
152
|
(.*)
|
|
159
153
|
$///)
|
|
160
154
|
[_, key, op, value] = lMatches
|
|
161
|
-
|
|
155
|
+
result = {
|
|
156
|
+
type: 'compare',
|
|
157
|
+
key,
|
|
158
|
+
op,
|
|
159
|
+
value: value.trim(),
|
|
160
|
+
}
|
|
162
161
|
else
|
|
163
162
|
error "Invalid line: '#{str}'"
|
|
163
|
+
debug "return from mapNode():", result
|
|
164
|
+
return result
|
|
164
165
|
|
|
165
166
|
# ..........................................................
|
|
166
167
|
|
|
@@ -197,7 +198,6 @@ export class EnvLoader extends PLLParser
|
|
|
197
198
|
procEnv: (tree) ->
|
|
198
199
|
|
|
199
200
|
debug "enter procEnv()"
|
|
200
|
-
debug tree, "TREE:"
|
|
201
201
|
|
|
202
202
|
for h in tree
|
|
203
203
|
switch h.node.type
|
|
@@ -244,6 +244,7 @@ export class EnvLoader extends PLLParser
|
|
|
244
244
|
|
|
245
245
|
debug "enter load()"
|
|
246
246
|
tree = @getTree()
|
|
247
|
+
debug "TREE", tree
|
|
247
248
|
assert tree?, "load(): tree is undef"
|
|
248
249
|
assert isArray(tree), "load(): tree is not an array"
|
|
249
250
|
@procEnv tree
|
|
@@ -252,11 +253,11 @@ export class EnvLoader extends PLLParser
|
|
|
252
253
|
|
|
253
254
|
# ---------------------------------------------------------------------------
|
|
254
255
|
# ---------------------------------------------------------------------------
|
|
255
|
-
# Load environment from a
|
|
256
|
+
# Load environment from a file
|
|
256
257
|
|
|
257
258
|
export loadEnvFile = (filepath, hOptions={}) ->
|
|
258
259
|
|
|
259
|
-
debug "
|
|
260
|
+
debug "LOADENV #{filepath}"
|
|
260
261
|
return loadEnvString slurp(filepath), hOptions
|
|
261
262
|
|
|
262
263
|
# ---------------------------------------------------------------------------
|
|
@@ -273,35 +274,19 @@ export loadEnvString = (contents, hOptions={}) ->
|
|
|
273
274
|
# ---------------------------------------------------------------------------
|
|
274
275
|
# Load environment from .env file
|
|
275
276
|
|
|
276
|
-
export loadEnvFrom = (searchDir, hOptions={}) ->
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
# recurse - load all .env files found by searching up
|
|
280
|
-
# rootName - env var name of first .env file found
|
|
281
|
-
# any option accepted by EnvLoader
|
|
282
|
-
# hInitialVars - hash of initial env var values
|
|
283
|
-
# prefix - load only vars with this prefix
|
|
284
|
-
# stripPrefix - remove the prefix before setting vars
|
|
285
|
-
# hCallbacks - callbacks to replace:
|
|
286
|
-
# getVar, setVar, clearVar, clearAll, names
|
|
287
|
-
|
|
288
|
-
debug "enter loadEnvFrom('#{searchDir}')"
|
|
289
|
-
{rootName, hInitialVars, recurse} = hOptions
|
|
277
|
+
export loadEnvFrom = (searchDir, rootName='DIR_ROOT', hOptions={}) ->
|
|
278
|
+
|
|
279
|
+
debug "enter loadEnvFrom('#{searchDir}', rootName=#{rootName})"
|
|
290
280
|
path = pathTo('.env', searchDir, "up")
|
|
291
281
|
if not path?
|
|
292
|
-
return
|
|
282
|
+
return
|
|
293
283
|
if rootName
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
while path = pathTo('.env', resolve(rtrunc(path, 5), '..'), "up")
|
|
301
|
-
lPaths.unshift path
|
|
302
|
-
for path in lPaths
|
|
303
|
-
env = loadEnvFile path, hOptions
|
|
304
|
-
else
|
|
284
|
+
process.env[rootName] = mkpath(rtrunc(path, 5))
|
|
285
|
+
|
|
286
|
+
lPaths = [path]
|
|
287
|
+
while path = pathTo('.env', resolve(rtrunc(path, 5), '..'), "up")
|
|
288
|
+
lPaths.unshift path
|
|
289
|
+
for path in lPaths
|
|
305
290
|
env = loadEnvFile path, hOptions
|
|
306
291
|
debug "return from loadEnvFrom()"
|
|
307
292
|
return env
|
package/src/EnvLoaderEx.js
CHANGED
|
@@ -11,16 +11,20 @@ import {
|
|
|
11
11
|
} from 'path';
|
|
12
12
|
|
|
13
13
|
import {
|
|
14
|
-
say,
|
|
15
14
|
undef,
|
|
16
15
|
pass,
|
|
17
16
|
error,
|
|
18
17
|
rtrim,
|
|
19
18
|
isArray,
|
|
20
19
|
isFunction,
|
|
21
|
-
rtrunc
|
|
20
|
+
rtrunc,
|
|
21
|
+
escapeStr
|
|
22
22
|
} from '@jdeighan/coffee-utils';
|
|
23
23
|
|
|
24
|
+
import {
|
|
25
|
+
log
|
|
26
|
+
} from '@jdeighan/coffee-utils/log';
|
|
27
|
+
|
|
24
28
|
import {
|
|
25
29
|
debug
|
|
26
30
|
} from '@jdeighan/coffee-utils/debug';
|
|
@@ -37,21 +41,15 @@ import {
|
|
|
37
41
|
|
|
38
42
|
// ---------------------------------------------------------------------------
|
|
39
43
|
export var EnvLoader = class EnvLoader extends PLLParser {
|
|
40
|
-
constructor(contents,
|
|
41
|
-
|
|
44
|
+
constructor(contents, hOptions = {}) {
|
|
45
|
+
// --- Valid options:
|
|
46
|
+
// prefix - load only vars with this prefix
|
|
47
|
+
// stripPrefix - remove the prefix before setting vars
|
|
48
|
+
// hCallbacks - callbacks to replace:
|
|
49
|
+
// getVar, setVar, clearVar, clearAll, names
|
|
42
50
|
super(contents);
|
|
43
|
-
this.
|
|
44
|
-
this.prefix = this.hOptions.prefix;
|
|
45
|
-
this.stripPrefix = this.hOptions.stripPrefix;
|
|
46
|
-
this.hCallbacks = this.hOptions.hCallbacks;
|
|
51
|
+
({prefix: this.prefix, stripPrefix: this.stripPrefix, hCallbacks: this.hCallbacks} = hOptions);
|
|
47
52
|
this.checkCallbacks();
|
|
48
|
-
if (this.hOptions.hInitialVars) {
|
|
49
|
-
ref = this.hOptions.hInitialVars;
|
|
50
|
-
for (key in ref) {
|
|
51
|
-
value = ref[key];
|
|
52
|
-
this.setVar(key, value);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
// ..........................................................
|
|
@@ -120,17 +118,18 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
120
118
|
// ..........................................................
|
|
121
119
|
dump() {
|
|
122
120
|
var i, len, name, ref;
|
|
123
|
-
|
|
121
|
+
log("=== Environment Variables: ===");
|
|
124
122
|
ref = this.names();
|
|
125
123
|
for (i = 0, len = ref.length; i < len; i++) {
|
|
126
124
|
name = ref[i];
|
|
127
|
-
|
|
125
|
+
log(` ${name} = '${this.getVar(name)}'`);
|
|
128
126
|
}
|
|
129
127
|
}
|
|
130
128
|
|
|
131
129
|
// ..........................................................
|
|
132
|
-
|
|
133
|
-
var _, key, lMatches, neg, op, value;
|
|
130
|
+
mapNode(str) {
|
|
131
|
+
var _, key, lMatches, neg, op, result, value;
|
|
132
|
+
debug(`enter mapNode('${escapeStr(str)}')`);
|
|
134
133
|
if (lMatches = str.match(/^([A-Za-z_\.]+)\s*=\s*(.*)$/)) { // identifier
|
|
135
134
|
[_, key, value] = lMatches;
|
|
136
135
|
if (this.prefix && (key.indexOf(this.prefix) !== 0)) {
|
|
@@ -139,7 +138,7 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
139
138
|
if (this.stripPrefix) {
|
|
140
139
|
key = key.substring(this.prefix.length);
|
|
141
140
|
}
|
|
142
|
-
|
|
141
|
+
result = {
|
|
143
142
|
type: 'assign',
|
|
144
143
|
key,
|
|
145
144
|
value: rtrim(value)
|
|
@@ -147,12 +146,12 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
147
146
|
} else if (lMatches = str.match(/^if\s+(?:(not)\s+)?([A-Za-z_]+)$/)) { // identifier
|
|
148
147
|
[_, neg, key] = lMatches;
|
|
149
148
|
if (neg) {
|
|
150
|
-
|
|
149
|
+
result = {
|
|
151
150
|
type: 'if_falsy',
|
|
152
151
|
key
|
|
153
152
|
};
|
|
154
153
|
} else {
|
|
155
|
-
|
|
154
|
+
result = {
|
|
156
155
|
type: 'if_truthy',
|
|
157
156
|
key
|
|
158
157
|
};
|
|
@@ -160,15 +159,17 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
160
159
|
} else if (lMatches = str.match(/^if\s+([A-Za-z_][A-Za-z0-9_]*)\s*(is|isnt|>|>=|<|<=)\s*(.*)$/)) { // identifier (key)
|
|
161
160
|
// comparison operator
|
|
162
161
|
[_, key, op, value] = lMatches;
|
|
163
|
-
|
|
162
|
+
result = {
|
|
164
163
|
type: 'compare',
|
|
165
164
|
key,
|
|
166
165
|
op,
|
|
167
166
|
value: value.trim()
|
|
168
167
|
};
|
|
169
168
|
} else {
|
|
170
|
-
|
|
169
|
+
error(`Invalid line: '${str}'`);
|
|
171
170
|
}
|
|
171
|
+
debug("return from mapNode():", result);
|
|
172
|
+
return result;
|
|
172
173
|
}
|
|
173
174
|
|
|
174
175
|
// ..........................................................
|
|
@@ -207,7 +208,6 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
207
208
|
procEnv(tree) {
|
|
208
209
|
var h, i, key, len, op, value;
|
|
209
210
|
debug("enter procEnv()");
|
|
210
|
-
debug(tree, "TREE:");
|
|
211
211
|
for (i = 0, len = tree.length; i < len; i++) {
|
|
212
212
|
h = tree[i];
|
|
213
213
|
switch (h.node.type) {
|
|
@@ -256,6 +256,7 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
256
256
|
var tree;
|
|
257
257
|
debug("enter load()");
|
|
258
258
|
tree = this.getTree();
|
|
259
|
+
debug("TREE", tree);
|
|
259
260
|
assert(tree != null, "load(): tree is undef");
|
|
260
261
|
assert(isArray(tree), "load(): tree is not an array");
|
|
261
262
|
this.procEnv(tree);
|
|
@@ -266,9 +267,9 @@ export var EnvLoader = class EnvLoader extends PLLParser {
|
|
|
266
267
|
|
|
267
268
|
// ---------------------------------------------------------------------------
|
|
268
269
|
// ---------------------------------------------------------------------------
|
|
269
|
-
// Load environment from a
|
|
270
|
+
// Load environment from a file
|
|
270
271
|
export var loadEnvFile = function(filepath, hOptions = {}) {
|
|
271
|
-
debug(`
|
|
272
|
+
debug(`LOADENV ${filepath}`);
|
|
272
273
|
return loadEnvString(slurp(filepath), hOptions);
|
|
273
274
|
};
|
|
274
275
|
|
|
@@ -285,40 +286,22 @@ export var loadEnvString = function(contents, hOptions = {}) {
|
|
|
285
286
|
|
|
286
287
|
// ---------------------------------------------------------------------------
|
|
287
288
|
// Load environment from .env file
|
|
288
|
-
export var loadEnvFrom = function(searchDir, hOptions = {}) {
|
|
289
|
-
var env,
|
|
290
|
-
|
|
291
|
-
// hInitialVars - set these env vars first
|
|
292
|
-
// recurse - load all .env files found by searching up
|
|
293
|
-
// rootName - env var name of first .env file found
|
|
294
|
-
// any option accepted by EnvLoader
|
|
295
|
-
// hInitialVars - hash of initial env var values
|
|
296
|
-
// prefix - load only vars with this prefix
|
|
297
|
-
// stripPrefix - remove the prefix before setting vars
|
|
298
|
-
// hCallbacks - callbacks to replace:
|
|
299
|
-
// getVar, setVar, clearVar, clearAll, names
|
|
300
|
-
debug(`enter loadEnvFrom('${searchDir}')`);
|
|
301
|
-
({rootName, hInitialVars, recurse} = hOptions);
|
|
289
|
+
export var loadEnvFrom = function(searchDir, rootName = 'DIR_ROOT', hOptions = {}) {
|
|
290
|
+
var env, i, lPaths, len, path;
|
|
291
|
+
debug(`enter loadEnvFrom('${searchDir}', rootName=${rootName})`);
|
|
302
292
|
path = pathTo('.env', searchDir, "up");
|
|
303
293
|
if (path == null) {
|
|
304
|
-
return
|
|
294
|
+
return;
|
|
305
295
|
}
|
|
306
296
|
if (rootName) {
|
|
307
|
-
|
|
308
|
-
hInitialVars = hOptions.hInitialVars = {};
|
|
309
|
-
}
|
|
310
|
-
hInitialVars[rootName] = mkpath(rtrunc(path, 5));
|
|
297
|
+
process.env[rootName] = mkpath(rtrunc(path, 5));
|
|
311
298
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
path = lPaths[i];
|
|
319
|
-
env = loadEnvFile(path, hOptions);
|
|
320
|
-
}
|
|
321
|
-
} else {
|
|
299
|
+
lPaths = [path];
|
|
300
|
+
while (path = pathTo('.env', resolve(rtrunc(path, 5), '..'), "up")) {
|
|
301
|
+
lPaths.unshift(path);
|
|
302
|
+
}
|
|
303
|
+
for (i = 0, len = lPaths.length; i < len; i++) {
|
|
304
|
+
path = lPaths[i];
|
|
322
305
|
env = loadEnvFile(path, hOptions);
|
|
323
306
|
}
|
|
324
307
|
debug("return from loadEnvFrom()");
|