@jdeighan/env 9.1.18 → 9.1.21

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": "9.1.18",
4
+ "version": "9.1.21",
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": "^7.0.19",
35
- "@jdeighan/mapper": "^14.0.8",
34
+ "@jdeighan/coffee-utils": "^7.0.22",
35
+ "@jdeighan/mapper": "^15.0.2",
36
36
  "coffeescript": "^2.6.1",
37
37
  "cross-env": "^7.0.3"
38
38
  },
@@ -3,12 +3,12 @@
3
3
  import pathlib from 'path'
4
4
 
5
5
  import {
6
- assert, undef, pass, error, rtrim, isArray, isFunction,
6
+ assert, undef, pass, error, rtrim, isArray, isHash, isFunction,
7
7
  rtrunc, escapeStr, croak,
8
8
  } from '@jdeighan/coffee-utils'
9
9
  import {log} from '@jdeighan/coffee-utils/log'
10
10
  import {debug} from '@jdeighan/coffee-utils/debug'
11
- import {slurp, pathTo, mkpath} from '@jdeighan/coffee-utils/fs'
11
+ import {slurp, pathTo, mkpath, parseSource} from '@jdeighan/coffee-utils/fs'
12
12
  import {TreeMapper} from '@jdeighan/mapper/tree'
13
13
 
14
14
  hDefCallbacks = {
@@ -259,10 +259,13 @@ export class EnvLoader extends TreeMapper
259
259
  # ---------------------------------------------------------------------------
260
260
  # Load environment from a string
261
261
 
262
- export loadEnvString = (contents, hOptions={}, source=undef) ->
262
+ export loadEnvString = (contents, hOptions) ->
263
263
 
264
- debug "enter loadEnvString()"
265
- env = new EnvLoader(contents, source, hOptions)
264
+ debug "enter loadEnvString()", hOptions
265
+ assert isHash(hOptions),\
266
+ "loadEnvString(): 2nd arg not a hash #{typeof hOptions}"
267
+ assert hOptions.source, "loadEnvString(): Missing source"
268
+ env = new EnvLoader(contents, hOptions.source, hOptions)
266
269
  env.load()
267
270
  debug "return from loadEnvString()"
268
271
  return
@@ -274,20 +277,24 @@ export loadEnvFile = (filepath, hOptions={}) ->
274
277
 
275
278
  debug "enter loadEnvFile #{filepath}"
276
279
  contents = slurp filepath
277
- loadEnvString contents, hOptions, filepath
280
+ hOptions.source = filepath
281
+ loadEnvString contents, hOptions
278
282
  debug "return from loadEnvFile"
279
283
  return
280
284
 
281
285
  # ---------------------------------------------------------------------------
282
286
  # Load environment from .env file
283
287
 
284
- export loadEnvFrom = (searchDir, hOptions={}) ->
288
+ export loadEnvFrom = (source, hOptions={}) ->
285
289
  # --- valid options:
286
290
  # onefile - load only the first file found
287
291
  # hCallbacks - getVar, setVar, clearVar, clearAll, names
288
292
 
289
- debug "enter loadEnvFrom '#{searchDir}'"
290
- path = pathTo('.env', searchDir, "up")
293
+ source = mkpath(source)
294
+ debug "enter loadEnvFrom '#{source}'"
295
+ hSourceInfo = parseSource(source)
296
+ debug 'hSourceInfo', hSourceInfo
297
+ path = pathTo('.env', source, "up")
291
298
  if ! path?
292
299
  debug "return from loadEnvFrom() - no .env file found"
293
300
  return
@@ -11,6 +11,7 @@ import {
11
11
  error,
12
12
  rtrim,
13
13
  isArray,
14
+ isHash,
14
15
  isFunction,
15
16
  rtrunc,
16
17
  escapeStr,
@@ -28,7 +29,8 @@ import {
28
29
  import {
29
30
  slurp,
30
31
  pathTo,
31
- mkpath
32
+ mkpath,
33
+ parseSource
32
34
  } from '@jdeighan/coffee-utils/fs';
33
35
 
34
36
  import {
@@ -269,10 +271,12 @@ export var EnvLoader = class EnvLoader extends TreeMapper {
269
271
  // ---------------------------------------------------------------------------
270
272
  // ---------------------------------------------------------------------------
271
273
  // Load environment from a string
272
- export var loadEnvString = function(contents, hOptions = {}, source = undef) {
274
+ export var loadEnvString = function(contents, hOptions) {
273
275
  var env;
274
- debug("enter loadEnvString()");
275
- env = new EnvLoader(contents, source, hOptions);
276
+ debug("enter loadEnvString()", hOptions);
277
+ assert(isHash(hOptions), `loadEnvString(): 2nd arg not a hash ${typeof hOptions}`);
278
+ assert(hOptions.source, "loadEnvString(): Missing source");
279
+ env = new EnvLoader(contents, hOptions.source, hOptions);
276
280
  env.load();
277
281
  debug("return from loadEnvString()");
278
282
  };
@@ -283,19 +287,23 @@ export var loadEnvFile = function(filepath, hOptions = {}) {
283
287
  var contents;
284
288
  debug(`enter loadEnvFile ${filepath}`);
285
289
  contents = slurp(filepath);
286
- loadEnvString(contents, hOptions, filepath);
290
+ hOptions.source = filepath;
291
+ loadEnvString(contents, hOptions);
287
292
  debug("return from loadEnvFile");
288
293
  };
289
294
 
290
295
  // ---------------------------------------------------------------------------
291
296
  // Load environment from .env file
292
- export var loadEnvFrom = function(searchDir, hOptions = {}) {
293
- var filepath, i, lPaths, len, path;
297
+ export var loadEnvFrom = function(source, hOptions = {}) {
298
+ var filepath, hSourceInfo, i, lPaths, len, path;
294
299
  // --- valid options:
295
300
  // onefile - load only the first file found
296
301
  // hCallbacks - getVar, setVar, clearVar, clearAll, names
297
- debug(`enter loadEnvFrom '${searchDir}'`);
298
- path = pathTo('.env', searchDir, "up");
302
+ source = mkpath(source);
303
+ debug(`enter loadEnvFrom '${source}'`);
304
+ hSourceInfo = parseSource(source);
305
+ debug('hSourceInfo', hSourceInfo);
306
+ path = pathTo('.env', source, "up");
299
307
  if (path == null) {
300
308
  debug("return from loadEnvFrom() - no .env file found");
301
309
  return;