@jdeighan/coffee-utils 7.0.17 → 7.0.20

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.17",
4
+ "version": "7.0.20",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -150,7 +150,7 @@ adjustStack = (str) ->
150
150
  export debug = (lArgs...) ->
151
151
 
152
152
  # --- We want to allow item to be undef. Therefore, we need to
153
- # distinguish between 1 arg sent vs. 2+ args sent
153
+ # distinguish between 1 arg sent vs. 2 args sent
154
154
  nArgs = lArgs.length
155
155
  assert (nArgs==1) || (nArgs==2), "debug(): #{nArgs} args"
156
156
  [label, item] = lArgs
@@ -172,6 +172,7 @@ export debug = (lArgs...) ->
172
172
  prefix: mainPre
173
173
  itemPrefix: auxPre
174
174
  }
175
+
175
176
  switch type
176
177
  when 'enter'
177
178
  log label, hOptions
@@ -184,7 +185,7 @@ export debug = (lArgs...) ->
184
185
  # --- don't repeat the label
185
186
  logItem undef, item, hOptions
186
187
  when 'string'
187
- if item
188
+ if (nArgs==2)
188
189
  logItem label, item, hOptions
189
190
  else
190
191
  log label, hOptions
@@ -163,7 +163,7 @@ adjustStack = function(str) {
163
163
  export var debug = function(...lArgs) {
164
164
  var auxPre, hEnv, hOptions, item, label, mainPre, nArgs, orgDebugging, trans, type;
165
165
  // --- We want to allow item to be undef. Therefore, we need to
166
- // distinguish between 1 arg sent vs. 2+ args sent
166
+ // distinguish between 1 arg sent vs. 2 args sent
167
167
  nArgs = lArgs.length;
168
168
  assert((nArgs === 1) || (nArgs === 2), `debug(): ${nArgs} args`);
169
169
  [label, item] = lArgs;
@@ -199,7 +199,7 @@ export var debug = function(...lArgs) {
199
199
  }
200
200
  break;
201
201
  case 'string':
202
- if (item) {
202
+ if (nArgs === 2) {
203
203
  logItem(label, item, hOptions);
204
204
  } else {
205
205
  log(label, hOptions);
@@ -14,7 +14,7 @@ import {debug} from '@jdeighan/coffee-utils/debug'
14
14
  import {arrayToBlock} from '@jdeighan/coffee-utils/block'
15
15
 
16
16
  # ---------------------------------------------------------------------------
17
- # mydir() - pass argument `import.meta.url` and it will return
17
+ # mydir() - pass argument import.meta.url and it will return
18
18
  # the directory your file is in
19
19
 
20
20
  export mydir = (url) ->
@@ -28,6 +28,30 @@ export mydir = (url) ->
28
28
  debug "final = #{final}"
29
29
  return final
30
30
 
31
+ # ---------------------------------------------------------------------------
32
+ # myfile() - pass argument import.meta.url and it will return
33
+ # the name of your file
34
+
35
+ export myfile = (url) ->
36
+
37
+ debug "url = #{url}"
38
+ path = urllib.fileURLToPath(url)
39
+ debug "path = #{path}"
40
+ filename = pathlib.parse(path).base
41
+ debug "filename = #{filename}"
42
+ return filename
43
+
44
+ # ---------------------------------------------------------------------------
45
+ # myfullpath() - pass argument import.meta.url and it will return
46
+ # the full path to your file
47
+
48
+ export myfullpath = (url) ->
49
+
50
+ debug "url = #{url}"
51
+ path = urllib.fileURLToPath(url)
52
+ debug "path = #{path}"
53
+ return mkpath(path)
54
+
31
55
  # ---------------------------------------------------------------------------
32
56
 
33
57
  export isFile = (fullpath) ->
@@ -80,26 +104,6 @@ export getFullPath = (filepath) ->
80
104
 
81
105
  return mkpath(pathlib.resolve(filepath))
82
106
 
83
- # ---------------------------------------------------------------------------
84
- # backup - back up a file
85
-
86
- # --- If report is true, missing source files are not an error
87
- # but both missing source files and successful copies
88
- # are reported via LOG
89
-
90
- export backup = (file, from, to, report=false) ->
91
- src = mkpath(from, file)
92
- dest = mkpath(to, file)
93
-
94
- if report
95
- if fs.existsSync(src)
96
- LOG "OK #{file}"
97
- fs.copyFileSync(src, dest)
98
- else
99
- LOG "MISSING #{src}"
100
- else
101
- fs.copyFileSync(src, dest)
102
-
103
107
  # ---------------------------------------------------------------------------
104
108
 
105
109
  export forEachLineInFile = (filepath, func) ->
@@ -249,25 +253,27 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
249
253
 
250
254
  # ---------------------------------------------------------------------------
251
255
 
252
- export pathTo = (fname, dir, direction="down") ->
256
+ export pathTo = (fname, searchDir, direction="down") ->
253
257
 
254
- debug "enter pathTo('#{fname}','#{dir}','#{direction}')"
255
- assert fs.existsSync(dir), "Directory #{dir} does not exist"
256
- filepath = mkpath(dir, fname)
258
+ debug "enter pathTo('#{fname}','#{searchDir}','#{direction}')"
259
+ if ! searchDir
260
+ searchDir = process.cwd()
261
+ assert fs.existsSync(searchDir), "Directory #{searchDir} does not exist"
262
+ filepath = mkpath(searchDir, fname)
257
263
  if fs.existsSync(filepath)
258
264
  debug "return from pathTo: #{filepath} - file exists"
259
265
  return filepath
260
266
  else if (direction == 'down')
261
267
  # --- Search all directories in this directory
262
268
  # getSubDirs() returns dirs sorted alphabetically
263
- for subdir in getSubDirs(dir)
264
- dirpath = mkpath(dir, subdir)
269
+ for subdir in getSubDirs(searchDir)
270
+ dirpath = mkpath(searchDir, subdir)
265
271
  debug "check #{dirpath}"
266
272
  if fpath = pathTo(fname, dirpath)
267
273
  debug "return from pathTo: #{fpath}"
268
274
  return fpath
269
275
  else if (direction == 'up')
270
- while dirpath = getParentDir(dir)
276
+ while dirpath = getParentDir(searchDir)
271
277
  debug "check #{dirpath}"
272
278
  filepath = mkpath(dirpath, fname)
273
279
  if fs.existsSync(filepath)
@@ -283,6 +289,8 @@ export pathTo = (fname, dir, direction="down") ->
283
289
  export allPathsTo = (fname, searchDir) ->
284
290
  # --- Only searches upward
285
291
 
292
+ if ! searchDir
293
+ searchDir = process.cwd()
286
294
  path = pathTo(fname, searchDir, "up")
287
295
  if path?
288
296
  lPaths = [path] # --- build an array of paths
@@ -334,43 +342,54 @@ export shortenPath = (path) ->
334
342
  export parseSource = (source) ->
335
343
  # --- returns {
336
344
  # dir
337
- # filename # only this is guaranteed to be set
345
+ # filename
338
346
  # stub
339
347
  # ext
340
348
  # }
349
+ # --- NOTE: source may be a file URL, e.g. import.meta.url
341
350
 
342
351
  debug "enter parseSource()"
352
+ assert isString(source), "parseSource(): source not a string"
343
353
  if source == 'unit test'
354
+ croak "A source of 'unit test' is deprecated"
355
+ if source.match(/^file\:\/\//)
356
+ source = urllib.fileURLToPath(source)
357
+
358
+ hInfo = pathlib.parse(source)
359
+ if hInfo.dir
360
+ dir = mkpath(hInfo.dir) # change \ to /
344
361
  hSourceInfo = {
345
- filename: 'unit test'
346
- stub: 'unit test'
362
+ dir
363
+ fullpath: mkpath(dir, hInfo.base)
364
+ filename: hInfo.base
365
+ stub: hInfo.name
366
+ ext: hInfo.ext
347
367
  }
348
- debug "return from parseSource()", hSourceInfo
349
- return hSourceInfo
350
- try
351
- hInfo = pathlib.parse(source)
352
- if hInfo.dir
353
- dir = mkpath(hInfo.dir) # change \ to /
354
- hSourceInfo = {
355
- dir
356
- fullpath: mkpath(dir, hInfo.base)
357
- filename: hInfo.base
358
- stub: hInfo.name
359
- ext: hInfo.ext
360
- }
361
- else
362
- hSourceInfo = {
363
- filename: hInfo.base
364
- stub: hInfo.name
365
- ext: hInfo.ext
366
- }
367
- debug "return from parseSource()", hSourceInfo
368
- return hSourceInfo
369
- catch err
368
+ else
370
369
  hSourceInfo = {
371
- filename: source
372
- stub: source
373
- error: err.message
370
+ filename: hInfo.base
371
+ stub: hInfo.name
372
+ ext: hInfo.ext
374
373
  }
375
- debug "return '#{err.message} from parseSource()", hSourceInfo
376
- return hSourceInfo
374
+ debug "return from parseSource()", hSourceInfo
375
+ return hSourceInfo
376
+
377
+ # ---------------------------------------------------------------------------
378
+ # backup - back up a file
379
+
380
+ # --- If report is true, missing source files are not an error
381
+ # but both missing source files and successful copies
382
+ # are reported via LOG
383
+
384
+ export backup = (file, from, to, report=false) ->
385
+ src = mkpath(from, file)
386
+ dest = mkpath(to, file)
387
+
388
+ if report
389
+ if fs.existsSync(src)
390
+ LOG "OK #{file}"
391
+ fs.copyFileSync(src, dest)
392
+ else
393
+ LOG "MISSING #{src}"
394
+ else
395
+ fs.copyFileSync(src, dest)
package/src/fs_utils.js CHANGED
@@ -39,7 +39,7 @@ import {
39
39
  } from '@jdeighan/coffee-utils/block';
40
40
 
41
41
  // ---------------------------------------------------------------------------
42
- // mydir() - pass argument `import.meta.url` and it will return
42
+ // mydir() - pass argument import.meta.url and it will return
43
43
  // the directory your file is in
44
44
  export var mydir = function(url) {
45
45
  var dir, final, path;
@@ -53,6 +53,30 @@ export var mydir = function(url) {
53
53
  return final;
54
54
  };
55
55
 
56
+ // ---------------------------------------------------------------------------
57
+ // myfile() - pass argument import.meta.url and it will return
58
+ // the name of your file
59
+ export var myfile = function(url) {
60
+ var filename, path;
61
+ debug(`url = ${url}`);
62
+ path = urllib.fileURLToPath(url);
63
+ debug(`path = ${path}`);
64
+ filename = pathlib.parse(path).base;
65
+ debug(`filename = ${filename}`);
66
+ return filename;
67
+ };
68
+
69
+ // ---------------------------------------------------------------------------
70
+ // myfullpath() - pass argument import.meta.url and it will return
71
+ // the full path to your file
72
+ export var myfullpath = function(url) {
73
+ var path;
74
+ debug(`url = ${url}`);
75
+ path = urllib.fileURLToPath(url);
76
+ debug(`path = ${path}`);
77
+ return mkpath(path);
78
+ };
79
+
56
80
  // ---------------------------------------------------------------------------
57
81
  export var isFile = function(fullpath) {
58
82
  return fs.lstatSync(fullpath).isFile();
@@ -106,28 +130,6 @@ export var getFullPath = function(filepath) {
106
130
  return mkpath(pathlib.resolve(filepath));
107
131
  };
108
132
 
109
- // ---------------------------------------------------------------------------
110
- // backup - back up a file
111
-
112
- // --- If report is true, missing source files are not an error
113
- // but both missing source files and successful copies
114
- // are reported via LOG
115
- export var backup = function(file, from, to, report = false) {
116
- var dest, src;
117
- src = mkpath(from, file);
118
- dest = mkpath(to, file);
119
- if (report) {
120
- if (fs.existsSync(src)) {
121
- LOG(`OK ${file}`);
122
- return fs.copyFileSync(src, dest);
123
- } else {
124
- return LOG(`MISSING ${src}`);
125
- }
126
- } else {
127
- return fs.copyFileSync(src, dest);
128
- }
129
- };
130
-
131
133
  // ---------------------------------------------------------------------------
132
134
  export var forEachLineInFile = function(filepath, func) {
133
135
  var buffer, line, nLines, reader;
@@ -302,21 +304,24 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
302
304
  };
303
305
 
304
306
  // ---------------------------------------------------------------------------
305
- export var pathTo = function(fname, dir, direction = "down") {
307
+ export var pathTo = function(fname, searchDir, direction = "down") {
306
308
  var dirpath, filepath, fpath, i, len, ref, subdir;
307
- debug(`enter pathTo('${fname}','${dir}','${direction}')`);
308
- assert(fs.existsSync(dir), `Directory ${dir} does not exist`);
309
- filepath = mkpath(dir, fname);
309
+ debug(`enter pathTo('${fname}','${searchDir}','${direction}')`);
310
+ if (!searchDir) {
311
+ searchDir = process.cwd();
312
+ }
313
+ assert(fs.existsSync(searchDir), `Directory ${searchDir} does not exist`);
314
+ filepath = mkpath(searchDir, fname);
310
315
  if (fs.existsSync(filepath)) {
311
316
  debug(`return from pathTo: ${filepath} - file exists`);
312
317
  return filepath;
313
318
  } else if (direction === 'down') {
314
- ref = getSubDirs(dir);
319
+ ref = getSubDirs(searchDir);
315
320
  // --- Search all directories in this directory
316
321
  // getSubDirs() returns dirs sorted alphabetically
317
322
  for (i = 0, len = ref.length; i < len; i++) {
318
323
  subdir = ref[i];
319
- dirpath = mkpath(dir, subdir);
324
+ dirpath = mkpath(searchDir, subdir);
320
325
  debug(`check ${dirpath}`);
321
326
  if (fpath = pathTo(fname, dirpath)) {
322
327
  debug(`return from pathTo: ${fpath}`);
@@ -324,7 +329,7 @@ export var pathTo = function(fname, dir, direction = "down") {
324
329
  }
325
330
  }
326
331
  } else if (direction === 'up') {
327
- while (dirpath = getParentDir(dir)) {
332
+ while (dirpath = getParentDir(searchDir)) {
328
333
  debug(`check ${dirpath}`);
329
334
  filepath = mkpath(dirpath, fname);
330
335
  if (fs.existsSync(filepath)) {
@@ -342,7 +347,9 @@ export var pathTo = function(fname, dir, direction = "down") {
342
347
  // ---------------------------------------------------------------------------
343
348
  export var allPathsTo = function(fname, searchDir) {
344
349
  var h, lPaths, path;
345
- // --- Only searches upward
350
+ if (!searchDir) {
351
+ searchDir = process.cwd();
352
+ }
346
353
  path = pathTo(fname, searchDir, "up");
347
354
  if (path != null) {
348
355
  lPaths = [path]; // --- build an array of paths
@@ -394,50 +401,61 @@ export var shortenPath = function(path) {
394
401
 
395
402
  // ---------------------------------------------------------------------------
396
403
  export var parseSource = function(source) {
397
- var dir, err, hInfo, hSourceInfo;
404
+ var dir, hInfo, hSourceInfo;
398
405
  // --- returns {
399
406
  // dir
400
- // filename # only this is guaranteed to be set
407
+ // filename
401
408
  // stub
402
409
  // ext
403
410
  // }
411
+ // --- NOTE: source may be a file URL, e.g. import.meta.url
404
412
  debug("enter parseSource()");
413
+ assert(isString(source), "parseSource(): source not a string");
405
414
  if (source === 'unit test') {
415
+ croak("A source of 'unit test' is deprecated");
416
+ }
417
+ if (source.match(/^file\:\/\//)) {
418
+ source = urllib.fileURLToPath(source);
419
+ }
420
+ hInfo = pathlib.parse(source);
421
+ if (hInfo.dir) {
422
+ dir = mkpath(hInfo.dir); // change \ to /
423
+ hSourceInfo = {
424
+ dir,
425
+ fullpath: mkpath(dir, hInfo.base),
426
+ filename: hInfo.base,
427
+ stub: hInfo.name,
428
+ ext: hInfo.ext
429
+ };
430
+ } else {
406
431
  hSourceInfo = {
407
- filename: 'unit test',
408
- stub: 'unit test'
432
+ filename: hInfo.base,
433
+ stub: hInfo.name,
434
+ ext: hInfo.ext
409
435
  };
410
- debug("return from parseSource()", hSourceInfo);
411
- return hSourceInfo;
412
436
  }
413
- try {
414
- hInfo = pathlib.parse(source);
415
- if (hInfo.dir) {
416
- dir = mkpath(hInfo.dir); // change \ to /
417
- hSourceInfo = {
418
- dir,
419
- fullpath: mkpath(dir, hInfo.base),
420
- filename: hInfo.base,
421
- stub: hInfo.name,
422
- ext: hInfo.ext
423
- };
437
+ debug("return from parseSource()", hSourceInfo);
438
+ return hSourceInfo;
439
+ };
440
+
441
+ // ---------------------------------------------------------------------------
442
+ // backup - back up a file
443
+
444
+ // --- If report is true, missing source files are not an error
445
+ // but both missing source files and successful copies
446
+ // are reported via LOG
447
+ export var backup = function(file, from, to, report = false) {
448
+ var dest, src;
449
+ src = mkpath(from, file);
450
+ dest = mkpath(to, file);
451
+ if (report) {
452
+ if (fs.existsSync(src)) {
453
+ LOG(`OK ${file}`);
454
+ return fs.copyFileSync(src, dest);
424
455
  } else {
425
- hSourceInfo = {
426
- filename: hInfo.base,
427
- stub: hInfo.name,
428
- ext: hInfo.ext
429
- };
456
+ return LOG(`MISSING ${src}`);
430
457
  }
431
- debug("return from parseSource()", hSourceInfo);
432
- return hSourceInfo;
433
- } catch (error1) {
434
- err = error1;
435
- hSourceInfo = {
436
- filename: source,
437
- stub: source,
438
- error: err.message
439
- };
440
- debug(`return '${err.message} from parseSource()`, hSourceInfo);
441
- return hSourceInfo;
458
+ } else {
459
+ return fs.copyFileSync(src, dest);
442
460
  }
443
461
  };