@jdeighan/env 1.1.4 → 2.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,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/env",
3
3
  "type": "module",
4
- "version": "1.1.4",
4
+ "version": "2.0.0",
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.2.18",
35
- "@jdeighan/string-input": "^1.1.4",
34
+ "@jdeighan/coffee-utils": "^2.1.4",
35
+ "@jdeighan/string-input": "^3.0.1",
36
36
  "coffeescript": "^2.5.1",
37
37
  "cross-env": "^7.0.3"
38
38
  }
@@ -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
- say, undef, pass, error, rtrim, isArray, isFunction, rtrunc,
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'
@@ -101,15 +102,16 @@ export class EnvLoader extends PLLParser
101
102
 
102
103
  dump: () ->
103
104
 
104
- say "=== Environment Variables: ==="
105
+ log "=== Environment Variables: ==="
105
106
  for name in @names()
106
- say " #{name} = '#{@getVar(name)}'"
107
+ log " #{name} = '#{@getVar(name)}'"
107
108
  return
108
109
 
109
110
  # ..........................................................
110
111
 
111
- mapString: (str) ->
112
+ mapNode: (str) ->
112
113
 
114
+ debug "enter mapNode('#{escapeStr(str)}')"
113
115
  if lMatches = str.match(///^
114
116
  ([A-Za-z_\.]+) # identifier
115
117
  \s*
@@ -122,7 +124,7 @@ export class EnvLoader extends PLLParser
122
124
  return undef
123
125
  if @stripPrefix
124
126
  key = key.substring(@prefix.length)
125
- return {
127
+ result = {
126
128
  type: 'assign',
127
129
  key,
128
130
  value: rtrim(value),
@@ -138,9 +140,9 @@ export class EnvLoader extends PLLParser
138
140
  $///)
139
141
  [_, neg, key] = lMatches
140
142
  if neg
141
- return {type: 'if_falsy', key}
143
+ result = {type: 'if_falsy', key}
142
144
  else
143
- return {type: 'if_truthy', key}
145
+ result = {type: 'if_truthy', key}
144
146
  else if lMatches = str.match(///^
145
147
  if
146
148
  \s+
@@ -158,9 +160,16 @@ export class EnvLoader extends PLLParser
158
160
  (.*)
159
161
  $///)
160
162
  [_, key, op, value] = lMatches
161
- return {type: 'compare', key, op, value: value.trim()}
163
+ result = {
164
+ type: 'compare',
165
+ key,
166
+ op,
167
+ value: value.trim(),
168
+ }
162
169
  else
163
170
  error "Invalid line: '#{str}'"
171
+ debug "return from mapNode():", result
172
+ return result
164
173
 
165
174
  # ..........................................................
166
175
 
@@ -197,7 +206,6 @@ export class EnvLoader extends PLLParser
197
206
  procEnv: (tree) ->
198
207
 
199
208
  debug "enter procEnv()"
200
- debug tree, "TREE:"
201
209
 
202
210
  for h in tree
203
211
  switch h.node.type
@@ -244,6 +252,7 @@ export class EnvLoader extends PLLParser
244
252
 
245
253
  debug "enter load()"
246
254
  tree = @getTree()
255
+ debug "TREE", tree
247
256
  assert tree?, "load(): tree is undef"
248
257
  assert isArray(tree), "load(): tree is not an array"
249
258
  @procEnv tree
@@ -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';
@@ -120,17 +124,18 @@ export var EnvLoader = class EnvLoader extends PLLParser {
120
124
  // ..........................................................
121
125
  dump() {
122
126
  var i, len, name, ref;
123
- say("=== Environment Variables: ===");
127
+ log("=== Environment Variables: ===");
124
128
  ref = this.names();
125
129
  for (i = 0, len = ref.length; i < len; i++) {
126
130
  name = ref[i];
127
- say(` ${name} = '${this.getVar(name)}'`);
131
+ log(` ${name} = '${this.getVar(name)}'`);
128
132
  }
129
133
  }
130
134
 
131
135
  // ..........................................................
132
- mapString(str) {
133
- var _, key, lMatches, neg, op, value;
136
+ mapNode(str) {
137
+ var _, key, lMatches, neg, op, result, value;
138
+ debug(`enter mapNode('${escapeStr(str)}')`);
134
139
  if (lMatches = str.match(/^([A-Za-z_\.]+)\s*=\s*(.*)$/)) { // identifier
135
140
  [_, key, value] = lMatches;
136
141
  if (this.prefix && (key.indexOf(this.prefix) !== 0)) {
@@ -139,7 +144,7 @@ export var EnvLoader = class EnvLoader extends PLLParser {
139
144
  if (this.stripPrefix) {
140
145
  key = key.substring(this.prefix.length);
141
146
  }
142
- return {
147
+ result = {
143
148
  type: 'assign',
144
149
  key,
145
150
  value: rtrim(value)
@@ -147,12 +152,12 @@ export var EnvLoader = class EnvLoader extends PLLParser {
147
152
  } else if (lMatches = str.match(/^if\s+(?:(not)\s+)?([A-Za-z_]+)$/)) { // identifier
148
153
  [_, neg, key] = lMatches;
149
154
  if (neg) {
150
- return {
155
+ result = {
151
156
  type: 'if_falsy',
152
157
  key
153
158
  };
154
159
  } else {
155
- return {
160
+ result = {
156
161
  type: 'if_truthy',
157
162
  key
158
163
  };
@@ -160,15 +165,17 @@ export var EnvLoader = class EnvLoader extends PLLParser {
160
165
  } else if (lMatches = str.match(/^if\s+([A-Za-z_][A-Za-z0-9_]*)\s*(is|isnt|>|>=|<|<=)\s*(.*)$/)) { // identifier (key)
161
166
  // comparison operator
162
167
  [_, key, op, value] = lMatches;
163
- return {
168
+ result = {
164
169
  type: 'compare',
165
170
  key,
166
171
  op,
167
172
  value: value.trim()
168
173
  };
169
174
  } else {
170
- return error(`Invalid line: '${str}'`);
175
+ error(`Invalid line: '${str}'`);
171
176
  }
177
+ debug("return from mapNode():", result);
178
+ return result;
172
179
  }
173
180
 
174
181
  // ..........................................................
@@ -207,7 +214,6 @@ export var EnvLoader = class EnvLoader extends PLLParser {
207
214
  procEnv(tree) {
208
215
  var h, i, key, len, op, value;
209
216
  debug("enter procEnv()");
210
- debug(tree, "TREE:");
211
217
  for (i = 0, len = tree.length; i < len; i++) {
212
218
  h = tree[i];
213
219
  switch (h.node.type) {
@@ -256,6 +262,7 @@ export var EnvLoader = class EnvLoader extends PLLParser {
256
262
  var tree;
257
263
  debug("enter load()");
258
264
  tree = this.getTree();
265
+ debug("TREE", tree);
259
266
  assert(tree != null, "load(): tree is undef");
260
267
  assert(isArray(tree), "load(): tree is not an array");
261
268
  this.procEnv(tree);