@jdeighan/coffee-utils 7.0.73 → 8.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/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.73",
4
+ "version": "8.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -4,7 +4,7 @@ import fs from 'fs'
4
4
  import readline from 'readline'
5
5
 
6
6
  import {
7
- assert, defined, isEmpty, isString, nonEmpty, error, rtrim,
7
+ undef, assert, defined, isEmpty, isString, isArray, nonEmpty, error, rtrim,
8
8
  } from '@jdeighan/coffee-utils'
9
9
 
10
10
  # ---------------------------------------------------------------------------
@@ -29,6 +29,9 @@ export blockToArray = (block) ->
29
29
 
30
30
  export arrayToBlock = (lLines) ->
31
31
 
32
+ if (lLines == undef)
33
+ return ''
34
+ assert isArray(lLines), "lLines is not an array"
32
35
  lLines = lLines.filter((line) => defined(line));
33
36
  if lLines.length == 0
34
37
  return ''
@@ -5,10 +5,12 @@ import fs from 'fs';
5
5
  import readline from 'readline';
6
6
 
7
7
  import {
8
+ undef,
8
9
  assert,
9
10
  defined,
10
11
  isEmpty,
11
12
  isString,
13
+ isArray,
12
14
  nonEmpty,
13
15
  error,
14
16
  rtrim
@@ -35,6 +37,10 @@ export var blockToArray = function(block) {
35
37
  // ---------------------------------------------------------------------------
36
38
  // arrayToBlock - block will have no trailing whitespace
37
39
  export var arrayToBlock = function(lLines) {
40
+ if (lLines === undef) {
41
+ return '';
42
+ }
43
+ assert(isArray(lLines), "lLines is not an array");
38
44
  lLines = lLines.filter((line) => {
39
45
  return defined(line);
40
46
  });
@@ -7,7 +7,7 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
9
  assert, undef, pass, defined, rtrim, error, isEmpty, nonEmpty,
10
- isString, isArray, isRegExp, isFunction, croak, OL,
10
+ isString, isArray, isHash, isRegExp, isFunction, croak, OL,
11
11
  } from '@jdeighan/coffee-utils'
12
12
  import {log, LOG} from '@jdeighan/coffee-utils/log'
13
13
  import {debug} from '@jdeighan/coffee-utils/debug'
@@ -268,7 +268,13 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
268
268
 
269
269
  # ---------------------------------------------------------------------------
270
270
 
271
- export pathTo = (fname, searchDir, direction="down") ->
271
+ export pathTo = (fname, searchDir, hOptions={}) ->
272
+
273
+ {direction, relative} = hOptions
274
+ if isEmpty(direction)
275
+ direction = 'down'
276
+ if isEmpty(relative)
277
+ relative = false
272
278
 
273
279
  debug "enter pathTo()", fname, searchDir, direction
274
280
  if ! searchDir
@@ -276,6 +282,8 @@ export pathTo = (fname, searchDir, direction="down") ->
276
282
  assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
277
283
  filepath = mkpath(searchDir, fname)
278
284
  if fs.existsSync(filepath)
285
+ if relative
286
+ filepath = "./#{fname}"
279
287
  debug "return from pathTo() - file exists", filepath
280
288
  return filepath
281
289
 
@@ -285,17 +293,26 @@ export pathTo = (fname, searchDir, direction="down") ->
285
293
  for subdir in getSubDirs(searchDir)
286
294
  dirpath = mkpath(searchDir, subdir)
287
295
  debug "check #{subdir}"
288
- if defined(fpath = pathTo(fname, dirpath))
296
+ if defined(fpath = pathTo(fname, dirpath, hOptions))
297
+ if relative
298
+ fpath = fpath.replace('./', "./#{subdir}/")
289
299
  debug "return from pathTo()", fpath
290
300
  return fpath
291
301
  else if (direction == 'up')
302
+ nLevels = 0
292
303
  while defined(dirPath = getParentDir(searchDir))
304
+ nLevels += 1
293
305
  debug "check #{dirPath}"
294
- filepath = mkpath(dirPath, fname)
295
- debug "check for #{filepath}"
296
- if fs.existsSync(filepath)
297
- debug "return from pathTo()", filepath
298
- return filepath
306
+ fpath = mkpath(dirPath, fname)
307
+ debug "check for #{fpath}"
308
+ if fs.existsSync(fpath)
309
+ if relative
310
+ fpath = "../".repeat(nLevels) + fname
311
+ debug "return from pathTo()", fpath
312
+ return fpath
313
+ else
314
+ debug "return from pathTo()", fpath
315
+ return fpath
299
316
  searchDir = dirPath
300
317
  else
301
318
  error "pathTo(): Invalid direction '#{direction}'"
@@ -309,12 +326,12 @@ export allPathsTo = (fname, searchDir) ->
309
326
 
310
327
  if ! searchDir
311
328
  searchDir = process.cwd()
312
- path = pathTo(fname, searchDir, "up")
329
+ path = pathTo(fname, searchDir, {direction: "up"})
313
330
  if path?
314
331
  lPaths = [path] # --- build an array of paths
315
332
  # --- search upward for files, but return ordered top down
316
333
  while (h = pathlib.parse(path)) \
317
- && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), "up"))
334
+ && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {direction: "up"}))
318
335
  lPaths.unshift path
319
336
  return lPaths
320
337
  else
package/src/fs_utils.js CHANGED
@@ -21,6 +21,7 @@ import {
21
21
  nonEmpty,
22
22
  isString,
23
23
  isArray,
24
+ isHash,
24
25
  isRegExp,
25
26
  isFunction,
26
27
  croak,
@@ -326,8 +327,15 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
326
327
  };
327
328
 
328
329
  // ---------------------------------------------------------------------------
329
- export var pathTo = function(fname, searchDir, direction = "down") {
330
- var dirPath, dirpath, filepath, fpath, i, len, ref, subdir;
330
+ export var pathTo = function(fname, searchDir, hOptions = {}) {
331
+ var dirPath, direction, dirpath, filepath, fpath, i, len, nLevels, ref, relative, subdir;
332
+ ({direction, relative} = hOptions);
333
+ if (isEmpty(direction)) {
334
+ direction = 'down';
335
+ }
336
+ if (isEmpty(relative)) {
337
+ relative = false;
338
+ }
331
339
  debug("enter pathTo()", fname, searchDir, direction);
332
340
  if (!searchDir) {
333
341
  searchDir = process.cwd();
@@ -335,6 +343,9 @@ export var pathTo = function(fname, searchDir, direction = "down") {
335
343
  assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
336
344
  filepath = mkpath(searchDir, fname);
337
345
  if (fs.existsSync(filepath)) {
346
+ if (relative) {
347
+ filepath = `./${fname}`;
348
+ }
338
349
  debug("return from pathTo() - file exists", filepath);
339
350
  return filepath;
340
351
  }
@@ -346,19 +357,30 @@ export var pathTo = function(fname, searchDir, direction = "down") {
346
357
  subdir = ref[i];
347
358
  dirpath = mkpath(searchDir, subdir);
348
359
  debug(`check ${subdir}`);
349
- if (defined(fpath = pathTo(fname, dirpath))) {
360
+ if (defined(fpath = pathTo(fname, dirpath, hOptions))) {
361
+ if (relative) {
362
+ fpath = fpath.replace('./', `./${subdir}/`);
363
+ }
350
364
  debug("return from pathTo()", fpath);
351
365
  return fpath;
352
366
  }
353
367
  }
354
368
  } else if (direction === 'up') {
369
+ nLevels = 0;
355
370
  while (defined(dirPath = getParentDir(searchDir))) {
371
+ nLevels += 1;
356
372
  debug(`check ${dirPath}`);
357
- filepath = mkpath(dirPath, fname);
358
- debug(`check for ${filepath}`);
359
- if (fs.existsSync(filepath)) {
360
- debug("return from pathTo()", filepath);
361
- return filepath;
373
+ fpath = mkpath(dirPath, fname);
374
+ debug(`check for ${fpath}`);
375
+ if (fs.existsSync(fpath)) {
376
+ if (relative) {
377
+ fpath = "../".repeat(nLevels) + fname;
378
+ debug("return from pathTo()", fpath);
379
+ return fpath;
380
+ } else {
381
+ debug("return from pathTo()", fpath);
382
+ return fpath;
383
+ }
362
384
  }
363
385
  searchDir = dirPath;
364
386
  }
@@ -375,11 +397,15 @@ export var allPathsTo = function(fname, searchDir) {
375
397
  if (!searchDir) {
376
398
  searchDir = process.cwd();
377
399
  }
378
- path = pathTo(fname, searchDir, "up");
400
+ path = pathTo(fname, searchDir, {
401
+ direction: "up"
402
+ });
379
403
  if (path != null) {
380
404
  lPaths = [path]; // --- build an array of paths
381
405
  // --- search upward for files, but return ordered top down
382
- while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), "up"))) {
406
+ while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {
407
+ direction: "up"
408
+ }))) {
383
409
  lPaths.unshift(path);
384
410
  }
385
411
  return lPaths;