@jdeighan/coffee-utils 7.0.18 → 7.0.19
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 +56 -28
- package/src/fs_utils.js +60 -31
package/package.json
CHANGED
package/src/fs_utils.coffee
CHANGED
@@ -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
|
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,
|
256
|
+
export pathTo = (fname, searchDir, direction="down") ->
|
253
257
|
|
254
|
-
debug "enter pathTo('#{fname}','#{
|
255
|
-
|
256
|
-
|
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(
|
264
|
-
dirpath = mkpath(
|
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(
|
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
|
@@ -374,3 +382,23 @@ export parseSource = (source) ->
|
|
374
382
|
}
|
375
383
|
debug "return '#{err.message} from parseSource()", hSourceInfo
|
376
384
|
return hSourceInfo
|
385
|
+
|
386
|
+
# ---------------------------------------------------------------------------
|
387
|
+
# backup - back up a file
|
388
|
+
|
389
|
+
# --- If report is true, missing source files are not an error
|
390
|
+
# but both missing source files and successful copies
|
391
|
+
# are reported via LOG
|
392
|
+
|
393
|
+
export backup = (file, from, to, report=false) ->
|
394
|
+
src = mkpath(from, file)
|
395
|
+
dest = mkpath(to, file)
|
396
|
+
|
397
|
+
if report
|
398
|
+
if fs.existsSync(src)
|
399
|
+
LOG "OK #{file}"
|
400
|
+
fs.copyFileSync(src, dest)
|
401
|
+
else
|
402
|
+
LOG "MISSING #{src}"
|
403
|
+
else
|
404
|
+
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
|
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,
|
307
|
+
export var pathTo = function(fname, searchDir, direction = "down") {
|
306
308
|
var dirpath, filepath, fpath, i, len, ref, subdir;
|
307
|
-
debug(`enter pathTo('${fname}','${
|
308
|
-
|
309
|
-
|
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(
|
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(
|
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(
|
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
|
-
|
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
|
@@ -441,3 +448,25 @@ export var parseSource = function(source) {
|
|
441
448
|
return hSourceInfo;
|
442
449
|
}
|
443
450
|
};
|
451
|
+
|
452
|
+
// ---------------------------------------------------------------------------
|
453
|
+
// backup - back up a file
|
454
|
+
|
455
|
+
// --- If report is true, missing source files are not an error
|
456
|
+
// but both missing source files and successful copies
|
457
|
+
// are reported via LOG
|
458
|
+
export var backup = function(file, from, to, report = false) {
|
459
|
+
var dest, src;
|
460
|
+
src = mkpath(from, file);
|
461
|
+
dest = mkpath(to, file);
|
462
|
+
if (report) {
|
463
|
+
if (fs.existsSync(src)) {
|
464
|
+
LOG(`OK ${file}`);
|
465
|
+
return fs.copyFileSync(src, dest);
|
466
|
+
} else {
|
467
|
+
return LOG(`MISSING ${src}`);
|
468
|
+
}
|
469
|
+
} else {
|
470
|
+
return fs.copyFileSync(src, dest);
|
471
|
+
}
|
472
|
+
};
|