@jdeighan/coffee-utils 7.0.18 → 7.0.21

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.18",
4
+ "version": "7.0.21",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -7,14 +7,14 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
9
  assert, undef, pass, rtrim, error, isEmpty, nonEmpty,
10
- isString, isArray, isRegExp, isFunction, croak,
10
+ isString, isArray, 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'
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,55 @@ 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),\
353
+ "parseSource(): source not a string: #{OL(source)}"
343
354
  if source == 'unit test'
355
+ croak "A source of 'unit test' is deprecated"
356
+ if source.match(/^file\:\/\//)
357
+ source = urllib.fileURLToPath(source)
358
+
359
+ hInfo = pathlib.parse(source)
360
+ if hInfo.dir
361
+ dir = mkpath(hInfo.dir) # change \ to /
344
362
  hSourceInfo = {
345
- filename: 'unit test'
346
- stub: 'unit test'
363
+ dir
364
+ fullpath: mkpath(dir, hInfo.base)
365
+ filename: hInfo.base
366
+ stub: hInfo.name
367
+ ext: hInfo.ext
347
368
  }
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
369
+ else
370
370
  hSourceInfo = {
371
- filename: source
372
- stub: source
373
- error: err.message
371
+ filename: hInfo.base
372
+ stub: hInfo.name
373
+ ext: hInfo.ext
374
374
  }
375
- debug "return '#{err.message} from parseSource()", hSourceInfo
376
- return hSourceInfo
375
+ debug "return from parseSource()", hSourceInfo
376
+ return hSourceInfo
377
+
378
+ # ---------------------------------------------------------------------------
379
+ # backup - back up a file
380
+
381
+ # --- If report is true, missing source files are not an error
382
+ # but both missing source files and successful copies
383
+ # are reported via LOG
384
+
385
+ export backup = (file, from, to, report=false) ->
386
+ src = mkpath(from, file)
387
+ dest = mkpath(to, file)
388
+
389
+ if report
390
+ if fs.existsSync(src)
391
+ LOG "OK #{file}"
392
+ fs.copyFileSync(src, dest)
393
+ else
394
+ LOG "MISSING #{src}"
395
+ else
396
+ fs.copyFileSync(src, dest)
package/src/fs_utils.js CHANGED
@@ -22,7 +22,8 @@ import {
22
22
  isArray,
23
23
  isRegExp,
24
24
  isFunction,
25
- croak
25
+ croak,
26
+ OL
26
27
  } from '@jdeighan/coffee-utils';
27
28
 
28
29
  import {
@@ -39,7 +40,7 @@ import {
39
40
  } from '@jdeighan/coffee-utils/block';
40
41
 
41
42
  // ---------------------------------------------------------------------------
42
- // mydir() - pass argument `import.meta.url` and it will return
43
+ // mydir() - pass argument import.meta.url and it will return
43
44
  // the directory your file is in
44
45
  export var mydir = function(url) {
45
46
  var dir, final, path;
@@ -53,6 +54,30 @@ export var mydir = function(url) {
53
54
  return final;
54
55
  };
55
56
 
57
+ // ---------------------------------------------------------------------------
58
+ // myfile() - pass argument import.meta.url and it will return
59
+ // the name of your file
60
+ export var myfile = function(url) {
61
+ var filename, path;
62
+ debug(`url = ${url}`);
63
+ path = urllib.fileURLToPath(url);
64
+ debug(`path = ${path}`);
65
+ filename = pathlib.parse(path).base;
66
+ debug(`filename = ${filename}`);
67
+ return filename;
68
+ };
69
+
70
+ // ---------------------------------------------------------------------------
71
+ // myfullpath() - pass argument import.meta.url and it will return
72
+ // the full path to your file
73
+ export var myfullpath = function(url) {
74
+ var path;
75
+ debug(`url = ${url}`);
76
+ path = urllib.fileURLToPath(url);
77
+ debug(`path = ${path}`);
78
+ return mkpath(path);
79
+ };
80
+
56
81
  // ---------------------------------------------------------------------------
57
82
  export var isFile = function(fullpath) {
58
83
  return fs.lstatSync(fullpath).isFile();
@@ -106,28 +131,6 @@ export var getFullPath = function(filepath) {
106
131
  return mkpath(pathlib.resolve(filepath));
107
132
  };
108
133
 
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
134
  // ---------------------------------------------------------------------------
132
135
  export var forEachLineInFile = function(filepath, func) {
133
136
  var buffer, line, nLines, reader;
@@ -302,21 +305,24 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
302
305
  };
303
306
 
304
307
  // ---------------------------------------------------------------------------
305
- export var pathTo = function(fname, dir, direction = "down") {
308
+ export var pathTo = function(fname, searchDir, direction = "down") {
306
309
  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);
310
+ debug(`enter pathTo('${fname}','${searchDir}','${direction}')`);
311
+ if (!searchDir) {
312
+ searchDir = process.cwd();
313
+ }
314
+ assert(fs.existsSync(searchDir), `Directory ${searchDir} does not exist`);
315
+ filepath = mkpath(searchDir, fname);
310
316
  if (fs.existsSync(filepath)) {
311
317
  debug(`return from pathTo: ${filepath} - file exists`);
312
318
  return filepath;
313
319
  } else if (direction === 'down') {
314
- ref = getSubDirs(dir);
320
+ ref = getSubDirs(searchDir);
315
321
  // --- Search all directories in this directory
316
322
  // getSubDirs() returns dirs sorted alphabetically
317
323
  for (i = 0, len = ref.length; i < len; i++) {
318
324
  subdir = ref[i];
319
- dirpath = mkpath(dir, subdir);
325
+ dirpath = mkpath(searchDir, subdir);
320
326
  debug(`check ${dirpath}`);
321
327
  if (fpath = pathTo(fname, dirpath)) {
322
328
  debug(`return from pathTo: ${fpath}`);
@@ -324,7 +330,7 @@ export var pathTo = function(fname, dir, direction = "down") {
324
330
  }
325
331
  }
326
332
  } else if (direction === 'up') {
327
- while (dirpath = getParentDir(dir)) {
333
+ while (dirpath = getParentDir(searchDir)) {
328
334
  debug(`check ${dirpath}`);
329
335
  filepath = mkpath(dirpath, fname);
330
336
  if (fs.existsSync(filepath)) {
@@ -342,7 +348,9 @@ export var pathTo = function(fname, dir, direction = "down") {
342
348
  // ---------------------------------------------------------------------------
343
349
  export var allPathsTo = function(fname, searchDir) {
344
350
  var h, lPaths, path;
345
- // --- Only searches upward
351
+ if (!searchDir) {
352
+ searchDir = process.cwd();
353
+ }
346
354
  path = pathTo(fname, searchDir, "up");
347
355
  if (path != null) {
348
356
  lPaths = [path]; // --- build an array of paths
@@ -394,50 +402,61 @@ export var shortenPath = function(path) {
394
402
 
395
403
  // ---------------------------------------------------------------------------
396
404
  export var parseSource = function(source) {
397
- var dir, err, hInfo, hSourceInfo;
405
+ var dir, hInfo, hSourceInfo;
398
406
  // --- returns {
399
407
  // dir
400
- // filename # only this is guaranteed to be set
408
+ // filename
401
409
  // stub
402
410
  // ext
403
411
  // }
412
+ // --- NOTE: source may be a file URL, e.g. import.meta.url
404
413
  debug("enter parseSource()");
414
+ assert(isString(source), `parseSource(): source not a string: ${OL(source)}`);
405
415
  if (source === 'unit test') {
416
+ croak("A source of 'unit test' is deprecated");
417
+ }
418
+ if (source.match(/^file\:\/\//)) {
419
+ source = urllib.fileURLToPath(source);
420
+ }
421
+ hInfo = pathlib.parse(source);
422
+ if (hInfo.dir) {
423
+ dir = mkpath(hInfo.dir); // change \ to /
424
+ hSourceInfo = {
425
+ dir,
426
+ fullpath: mkpath(dir, hInfo.base),
427
+ filename: hInfo.base,
428
+ stub: hInfo.name,
429
+ ext: hInfo.ext
430
+ };
431
+ } else {
406
432
  hSourceInfo = {
407
- filename: 'unit test',
408
- stub: 'unit test'
433
+ filename: hInfo.base,
434
+ stub: hInfo.name,
435
+ ext: hInfo.ext
409
436
  };
410
- debug("return from parseSource()", hSourceInfo);
411
- return hSourceInfo;
412
437
  }
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
- };
438
+ debug("return from parseSource()", hSourceInfo);
439
+ return hSourceInfo;
440
+ };
441
+
442
+ // ---------------------------------------------------------------------------
443
+ // backup - back up a file
444
+
445
+ // --- If report is true, missing source files are not an error
446
+ // but both missing source files and successful copies
447
+ // are reported via LOG
448
+ export var backup = function(file, from, to, report = false) {
449
+ var dest, src;
450
+ src = mkpath(from, file);
451
+ dest = mkpath(to, file);
452
+ if (report) {
453
+ if (fs.existsSync(src)) {
454
+ LOG(`OK ${file}`);
455
+ return fs.copyFileSync(src, dest);
424
456
  } else {
425
- hSourceInfo = {
426
- filename: hInfo.base,
427
- stub: hInfo.name,
428
- ext: hInfo.ext
429
- };
457
+ return LOG(`MISSING ${src}`);
430
458
  }
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;
459
+ } else {
460
+ return fs.copyFileSync(src, dest);
442
461
  }
443
462
  };