@jdeighan/coffee-utils 7.0.19 → 7.0.22
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/fs_utils.coffee +32 -18
- package/src/fs_utils.js +39 -21
package/package.json
CHANGED
package/src/fs_utils.coffee
CHANGED
@@ -7,7 +7,7 @@ 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'
|
@@ -62,7 +62,12 @@ export isFile = (fullpath) ->
|
|
62
62
|
|
63
63
|
export isDir = (fullpath) ->
|
64
64
|
|
65
|
-
|
65
|
+
try
|
66
|
+
obj = fs.lstatSync(fullpath)
|
67
|
+
if !obj? then return false
|
68
|
+
return obj.isDirectory()
|
69
|
+
catch
|
70
|
+
return false
|
66
71
|
|
67
72
|
# ---------------------------------------------------------------------------
|
68
73
|
|
@@ -73,6 +78,16 @@ export isSimpleFileName = (path) ->
|
|
73
78
|
|
74
79
|
# ---------------------------------------------------------------------------
|
75
80
|
|
81
|
+
export fileStub = (path) ->
|
82
|
+
|
83
|
+
assert isString(path), "fileExt(): path not a string"
|
84
|
+
if lMatches = path.match(/^(.*)\.[A-Za-z0-9_]+$/)
|
85
|
+
return lMatches[1]
|
86
|
+
else
|
87
|
+
return ''
|
88
|
+
|
89
|
+
# ---------------------------------------------------------------------------
|
90
|
+
|
76
91
|
export fileExt = (path) ->
|
77
92
|
|
78
93
|
assert isString(path), "fileExt(): path not a string"
|
@@ -342,20 +357,27 @@ export shortenPath = (path) ->
|
|
342
357
|
export parseSource = (source) ->
|
343
358
|
# --- returns {
|
344
359
|
# dir
|
345
|
-
# filename
|
360
|
+
# filename
|
361
|
+
# fullpath
|
346
362
|
# stub
|
347
363
|
# ext
|
348
364
|
# }
|
365
|
+
# --- NOTE: source may be a file URL, e.g. import.meta.url
|
349
366
|
|
350
367
|
debug "enter parseSource()"
|
368
|
+
assert isString(source),\
|
369
|
+
"parseSource(): source not a string: #{OL(source)}"
|
351
370
|
if source == 'unit test'
|
371
|
+
croak "A source of 'unit test' is deprecated"
|
372
|
+
if source.match(/^file\:\/\//)
|
373
|
+
source = urllib.fileURLToPath(source)
|
374
|
+
|
375
|
+
if isDir(source)
|
352
376
|
hSourceInfo = {
|
353
|
-
|
354
|
-
|
377
|
+
dir: source
|
378
|
+
fullpath: source
|
355
379
|
}
|
356
|
-
|
357
|
-
return hSourceInfo
|
358
|
-
try
|
380
|
+
else
|
359
381
|
hInfo = pathlib.parse(source)
|
360
382
|
if hInfo.dir
|
361
383
|
dir = mkpath(hInfo.dir) # change \ to /
|
@@ -372,16 +394,8 @@ export parseSource = (source) ->
|
|
372
394
|
stub: hInfo.name
|
373
395
|
ext: hInfo.ext
|
374
396
|
}
|
375
|
-
|
376
|
-
|
377
|
-
catch err
|
378
|
-
hSourceInfo = {
|
379
|
-
filename: source
|
380
|
-
stub: source
|
381
|
-
error: err.message
|
382
|
-
}
|
383
|
-
debug "return '#{err.message} from parseSource()", hSourceInfo
|
384
|
-
return hSourceInfo
|
397
|
+
debug "return from parseSource()", hSourceInfo
|
398
|
+
return hSourceInfo
|
385
399
|
|
386
400
|
# ---------------------------------------------------------------------------
|
387
401
|
# backup - back up a file
|
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 {
|
@@ -84,7 +85,16 @@ export var isFile = function(fullpath) {
|
|
84
85
|
|
85
86
|
// ---------------------------------------------------------------------------
|
86
87
|
export var isDir = function(fullpath) {
|
87
|
-
|
88
|
+
var obj;
|
89
|
+
try {
|
90
|
+
obj = fs.lstatSync(fullpath);
|
91
|
+
if (obj == null) {
|
92
|
+
return false;
|
93
|
+
}
|
94
|
+
return obj.isDirectory();
|
95
|
+
} catch (error1) {
|
96
|
+
return false;
|
97
|
+
}
|
88
98
|
};
|
89
99
|
|
90
100
|
// ---------------------------------------------------------------------------
|
@@ -94,6 +104,17 @@ export var isSimpleFileName = function(path) {
|
|
94
104
|
return !h.root && !h.dir && h.base;
|
95
105
|
};
|
96
106
|
|
107
|
+
// ---------------------------------------------------------------------------
|
108
|
+
export var fileStub = function(path) {
|
109
|
+
var lMatches;
|
110
|
+
assert(isString(path), "fileExt(): path not a string");
|
111
|
+
if (lMatches = path.match(/^(.*)\.[A-Za-z0-9_]+$/)) {
|
112
|
+
return lMatches[1];
|
113
|
+
} else {
|
114
|
+
return '';
|
115
|
+
}
|
116
|
+
};
|
117
|
+
|
97
118
|
// ---------------------------------------------------------------------------
|
98
119
|
export var fileExt = function(path) {
|
99
120
|
var lMatches;
|
@@ -401,23 +422,29 @@ export var shortenPath = function(path) {
|
|
401
422
|
|
402
423
|
// ---------------------------------------------------------------------------
|
403
424
|
export var parseSource = function(source) {
|
404
|
-
var dir,
|
425
|
+
var dir, hInfo, hSourceInfo;
|
405
426
|
// --- returns {
|
406
427
|
// dir
|
407
|
-
// filename
|
428
|
+
// filename
|
429
|
+
// fullpath
|
408
430
|
// stub
|
409
431
|
// ext
|
410
432
|
// }
|
433
|
+
// --- NOTE: source may be a file URL, e.g. import.meta.url
|
411
434
|
debug("enter parseSource()");
|
435
|
+
assert(isString(source), `parseSource(): source not a string: ${OL(source)}`);
|
412
436
|
if (source === 'unit test') {
|
437
|
+
croak("A source of 'unit test' is deprecated");
|
438
|
+
}
|
439
|
+
if (source.match(/^file\:\/\//)) {
|
440
|
+
source = urllib.fileURLToPath(source);
|
441
|
+
}
|
442
|
+
if (isDir(source)) {
|
413
443
|
hSourceInfo = {
|
414
|
-
|
415
|
-
|
444
|
+
dir: source,
|
445
|
+
fullpath: source
|
416
446
|
};
|
417
|
-
|
418
|
-
return hSourceInfo;
|
419
|
-
}
|
420
|
-
try {
|
447
|
+
} else {
|
421
448
|
hInfo = pathlib.parse(source);
|
422
449
|
if (hInfo.dir) {
|
423
450
|
dir = mkpath(hInfo.dir); // change \ to /
|
@@ -435,18 +462,9 @@ export var parseSource = function(source) {
|
|
435
462
|
ext: hInfo.ext
|
436
463
|
};
|
437
464
|
}
|
438
|
-
debug("return from parseSource()", hSourceInfo);
|
439
|
-
return hSourceInfo;
|
440
|
-
} catch (error1) {
|
441
|
-
err = error1;
|
442
|
-
hSourceInfo = {
|
443
|
-
filename: source,
|
444
|
-
stub: source,
|
445
|
-
error: err.message
|
446
|
-
};
|
447
|
-
debug(`return '${err.message} from parseSource()`, hSourceInfo);
|
448
|
-
return hSourceInfo;
|
449
465
|
}
|
466
|
+
debug("return from parseSource()", hSourceInfo);
|
467
|
+
return hSourceInfo;
|
450
468
|
};
|
451
469
|
|
452
470
|
// ---------------------------------------------------------------------------
|