@jdeighan/coffee-utils 4.0.14 → 4.0.18
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 +1 -1
- package/src/fs_utils.coffee +68 -44
- package/src/fs_utils.js +74 -53
package/package.json
CHANGED
package/src/fs_utils.coffee
CHANGED
@@ -13,47 +13,31 @@ import {debug} from '@jdeighan/coffee-utils/debug'
|
|
13
13
|
|
14
14
|
# ---------------------------------------------------------------------------
|
15
15
|
|
16
|
-
export
|
17
|
-
# --- returns {
|
18
|
-
# dir
|
19
|
-
# filename # only this is guaranteed to be set
|
20
|
-
# stub
|
21
|
-
# ext
|
22
|
-
# }
|
16
|
+
export isFile = (fullpath) ->
|
23
17
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
stub: hInfo.name
|
48
|
-
ext: hInfo.ext
|
49
|
-
}
|
50
|
-
catch err
|
51
|
-
debug "return '#{err.message} from parseSource()"
|
52
|
-
return {
|
53
|
-
filename: source
|
54
|
-
stub: source
|
55
|
-
error: err.message
|
56
|
-
}
|
18
|
+
return fs.lstatSync(fullpath).isFile()
|
19
|
+
|
20
|
+
# ---------------------------------------------------------------------------
|
21
|
+
|
22
|
+
export isDir = (fullpath) ->
|
23
|
+
|
24
|
+
return fs.lstatSync(fullpath).isDirectory()
|
25
|
+
|
26
|
+
# ---------------------------------------------------------------------------
|
27
|
+
|
28
|
+
export isSimpleFileName = (path) ->
|
29
|
+
|
30
|
+
h = pathlib.parse(path)
|
31
|
+
return ! h.root && ! h.dir && h.base
|
32
|
+
|
33
|
+
# ---------------------------------------------------------------------------
|
34
|
+
|
35
|
+
export fileExt = (path) ->
|
36
|
+
|
37
|
+
if lMatches = path.match(/\.[A-Za-z0-9_]+$/)
|
38
|
+
return lMatches[0]
|
39
|
+
else
|
40
|
+
return ''
|
57
41
|
|
58
42
|
# ---------------------------------------------------------------------------
|
59
43
|
# mydir() - pass argument `import.meta.url` and it will return
|
@@ -119,10 +103,7 @@ export barf = (filepath, contents) ->
|
|
119
103
|
|
120
104
|
debug "enter barf('#{filepath}')", contents
|
121
105
|
contents = rtrim(contents) + "\n"
|
122
|
-
|
123
|
-
fs.writeFileSync(filepath, contents, {encoding: 'utf8'})
|
124
|
-
catch err
|
125
|
-
log "barf(): write failed: #{err.message}"
|
106
|
+
fs.writeFileSync(filepath, contents, {encoding: 'utf8'})
|
126
107
|
debug "return from barf()"
|
127
108
|
return
|
128
109
|
|
@@ -272,3 +253,46 @@ export shortenPath = (path) ->
|
|
272
253
|
else
|
273
254
|
return str
|
274
255
|
|
256
|
+
# ---------------------------------------------------------------------------
|
257
|
+
|
258
|
+
export parseSource = (source) ->
|
259
|
+
# --- returns {
|
260
|
+
# dir
|
261
|
+
# filename # only this is guaranteed to be set
|
262
|
+
# stub
|
263
|
+
# ext
|
264
|
+
# }
|
265
|
+
|
266
|
+
debug "enter parseSource()"
|
267
|
+
if source == 'unit test'
|
268
|
+
debug "return 'unit test' from parseSource()"
|
269
|
+
return {
|
270
|
+
filename: 'unit test'
|
271
|
+
stub: 'unit test'
|
272
|
+
}
|
273
|
+
try
|
274
|
+
hInfo = pathlib.parse(source)
|
275
|
+
debug "return from parseSource()", hInfo
|
276
|
+
if hInfo.root
|
277
|
+
dir = mkpath(hInfo.dir) # change \ to /
|
278
|
+
return {
|
279
|
+
dir: dir
|
280
|
+
fullpath: mkpath(dir, hInfo.base)
|
281
|
+
filename: hInfo.base
|
282
|
+
stub: hInfo.name
|
283
|
+
ext: hInfo.ext
|
284
|
+
}
|
285
|
+
else
|
286
|
+
return {
|
287
|
+
dir: mkpath(hInfo.dir) # change \ to /
|
288
|
+
filename: hInfo.base
|
289
|
+
stub: hInfo.name
|
290
|
+
ext: hInfo.ext
|
291
|
+
}
|
292
|
+
catch err
|
293
|
+
debug "return '#{err.message} from parseSource()"
|
294
|
+
return {
|
295
|
+
filename: source
|
296
|
+
stub: source
|
297
|
+
error: err.message
|
298
|
+
}
|
package/src/fs_utils.js
CHANGED
@@ -29,50 +29,29 @@ import {
|
|
29
29
|
} from '@jdeighan/coffee-utils/debug';
|
30
30
|
|
31
31
|
// ---------------------------------------------------------------------------
|
32
|
-
export var
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
fullpath: mkpath(dir, hInfo.base),
|
56
|
-
filename: hInfo.base,
|
57
|
-
stub: hInfo.name,
|
58
|
-
ext: hInfo.ext
|
59
|
-
};
|
60
|
-
} else {
|
61
|
-
return {
|
62
|
-
dir: mkpath(hInfo.dir), // change \ to /
|
63
|
-
filename: hInfo.base,
|
64
|
-
stub: hInfo.name,
|
65
|
-
ext: hInfo.ext
|
66
|
-
};
|
67
|
-
}
|
68
|
-
} catch (error1) {
|
69
|
-
err = error1;
|
70
|
-
debug(`return '${err.message} from parseSource()`);
|
71
|
-
return {
|
72
|
-
filename: source,
|
73
|
-
stub: source,
|
74
|
-
error: err.message
|
75
|
-
};
|
32
|
+
export var isFile = function(fullpath) {
|
33
|
+
return fs.lstatSync(fullpath).isFile();
|
34
|
+
};
|
35
|
+
|
36
|
+
// ---------------------------------------------------------------------------
|
37
|
+
export var isDir = function(fullpath) {
|
38
|
+
return fs.lstatSync(fullpath).isDirectory();
|
39
|
+
};
|
40
|
+
|
41
|
+
// ---------------------------------------------------------------------------
|
42
|
+
export var isSimpleFileName = function(path) {
|
43
|
+
var h;
|
44
|
+
h = pathlib.parse(path);
|
45
|
+
return !h.root && !h.dir && h.base;
|
46
|
+
};
|
47
|
+
|
48
|
+
// ---------------------------------------------------------------------------
|
49
|
+
export var fileExt = function(path) {
|
50
|
+
var lMatches;
|
51
|
+
if (lMatches = path.match(/\.[A-Za-z0-9_]+$/)) {
|
52
|
+
return lMatches[0];
|
53
|
+
} else {
|
54
|
+
return '';
|
76
55
|
}
|
77
56
|
};
|
78
57
|
|
@@ -138,17 +117,11 @@ export var slurp = function(filepath) {
|
|
138
117
|
// ---------------------------------------------------------------------------
|
139
118
|
// barf - write a string to a file
|
140
119
|
export var barf = function(filepath, contents) {
|
141
|
-
var err;
|
142
120
|
debug(`enter barf('${filepath}')`, contents);
|
143
121
|
contents = rtrim(contents) + "\n";
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
});
|
148
|
-
} catch (error1) {
|
149
|
-
err = error1;
|
150
|
-
log(`barf(): write failed: ${err.message}`);
|
151
|
-
}
|
122
|
+
fs.writeFileSync(filepath, contents, {
|
123
|
+
encoding: 'utf8'
|
124
|
+
});
|
152
125
|
debug("return from barf()");
|
153
126
|
};
|
154
127
|
|
@@ -323,3 +296,51 @@ export var shortenPath = function(path) {
|
|
323
296
|
return str;
|
324
297
|
}
|
325
298
|
};
|
299
|
+
|
300
|
+
// ---------------------------------------------------------------------------
|
301
|
+
export var parseSource = function(source) {
|
302
|
+
var dir, err, hInfo;
|
303
|
+
// --- returns {
|
304
|
+
// dir
|
305
|
+
// filename # only this is guaranteed to be set
|
306
|
+
// stub
|
307
|
+
// ext
|
308
|
+
// }
|
309
|
+
debug("enter parseSource()");
|
310
|
+
if (source === 'unit test') {
|
311
|
+
debug("return 'unit test' from parseSource()");
|
312
|
+
return {
|
313
|
+
filename: 'unit test',
|
314
|
+
stub: 'unit test'
|
315
|
+
};
|
316
|
+
}
|
317
|
+
try {
|
318
|
+
hInfo = pathlib.parse(source);
|
319
|
+
debug("return from parseSource()", hInfo);
|
320
|
+
if (hInfo.root) {
|
321
|
+
dir = mkpath(hInfo.dir); // change \ to /
|
322
|
+
return {
|
323
|
+
dir: dir,
|
324
|
+
fullpath: mkpath(dir, hInfo.base),
|
325
|
+
filename: hInfo.base,
|
326
|
+
stub: hInfo.name,
|
327
|
+
ext: hInfo.ext
|
328
|
+
};
|
329
|
+
} else {
|
330
|
+
return {
|
331
|
+
dir: mkpath(hInfo.dir), // change \ to /
|
332
|
+
filename: hInfo.base,
|
333
|
+
stub: hInfo.name,
|
334
|
+
ext: hInfo.ext
|
335
|
+
};
|
336
|
+
}
|
337
|
+
} catch (error1) {
|
338
|
+
err = error1;
|
339
|
+
debug(`return '${err.message} from parseSource()`);
|
340
|
+
return {
|
341
|
+
filename: source,
|
342
|
+
stub: source,
|
343
|
+
error: err.message
|
344
|
+
};
|
345
|
+
}
|
346
|
+
};
|