@jdeighan/coffee-utils 7.0.75 → 8.0.0
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 +27 -10
- package/src/fs_utils.js +36 -10
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, defined, rtrim, error, isEmpty, nonEmpty,
|
10
|
-
isString, isArray, isRegExp, isFunction, croak, OL,
|
10
|
+
isString, isArray, isHash, 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'
|
@@ -268,7 +268,13 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
|
|
268
268
|
|
269
269
|
# ---------------------------------------------------------------------------
|
270
270
|
|
271
|
-
export pathTo = (fname, searchDir,
|
271
|
+
export pathTo = (fname, searchDir, hOptions={}) ->
|
272
|
+
|
273
|
+
{direction, relative} = hOptions
|
274
|
+
if isEmpty(direction)
|
275
|
+
direction = 'down'
|
276
|
+
if isEmpty(relative)
|
277
|
+
relative = false
|
272
278
|
|
273
279
|
debug "enter pathTo()", fname, searchDir, direction
|
274
280
|
if ! searchDir
|
@@ -276,6 +282,8 @@ export pathTo = (fname, searchDir, direction="down") ->
|
|
276
282
|
assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
|
277
283
|
filepath = mkpath(searchDir, fname)
|
278
284
|
if fs.existsSync(filepath)
|
285
|
+
if relative
|
286
|
+
filepath = "./#{fname}"
|
279
287
|
debug "return from pathTo() - file exists", filepath
|
280
288
|
return filepath
|
281
289
|
|
@@ -285,17 +293,26 @@ export pathTo = (fname, searchDir, direction="down") ->
|
|
285
293
|
for subdir in getSubDirs(searchDir)
|
286
294
|
dirpath = mkpath(searchDir, subdir)
|
287
295
|
debug "check #{subdir}"
|
288
|
-
if defined(fpath = pathTo(fname, dirpath))
|
296
|
+
if defined(fpath = pathTo(fname, dirpath, hOptions))
|
297
|
+
if relative
|
298
|
+
fpath = fpath.replace('./', "./#{subdir}/")
|
289
299
|
debug "return from pathTo()", fpath
|
290
300
|
return fpath
|
291
301
|
else if (direction == 'up')
|
302
|
+
nLevels = 0
|
292
303
|
while defined(dirPath = getParentDir(searchDir))
|
304
|
+
nLevels += 1
|
293
305
|
debug "check #{dirPath}"
|
294
|
-
|
295
|
-
debug "check for #{
|
296
|
-
if fs.existsSync(
|
297
|
-
|
298
|
-
|
306
|
+
fpath = mkpath(dirPath, fname)
|
307
|
+
debug "check for #{fpath}"
|
308
|
+
if fs.existsSync(fpath)
|
309
|
+
if relative
|
310
|
+
fpath = "../".repeat(nLevels) + fname
|
311
|
+
debug "return from pathTo()", fpath
|
312
|
+
return fpath
|
313
|
+
else
|
314
|
+
debug "return from pathTo()", fpath
|
315
|
+
return fpath
|
299
316
|
searchDir = dirPath
|
300
317
|
else
|
301
318
|
error "pathTo(): Invalid direction '#{direction}'"
|
@@ -309,12 +326,12 @@ export allPathsTo = (fname, searchDir) ->
|
|
309
326
|
|
310
327
|
if ! searchDir
|
311
328
|
searchDir = process.cwd()
|
312
|
-
path = pathTo(fname, searchDir, "up")
|
329
|
+
path = pathTo(fname, searchDir, {direction: "up"})
|
313
330
|
if path?
|
314
331
|
lPaths = [path] # --- build an array of paths
|
315
332
|
# --- search upward for files, but return ordered top down
|
316
333
|
while (h = pathlib.parse(path)) \
|
317
|
-
&& (path = pathTo(fname, pathlib.resolve(h.dir, '..'), "up"))
|
334
|
+
&& (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {direction: "up"}))
|
318
335
|
lPaths.unshift path
|
319
336
|
return lPaths
|
320
337
|
else
|
package/src/fs_utils.js
CHANGED
@@ -21,6 +21,7 @@ import {
|
|
21
21
|
nonEmpty,
|
22
22
|
isString,
|
23
23
|
isArray,
|
24
|
+
isHash,
|
24
25
|
isRegExp,
|
25
26
|
isFunction,
|
26
27
|
croak,
|
@@ -326,8 +327,15 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
|
|
326
327
|
};
|
327
328
|
|
328
329
|
// ---------------------------------------------------------------------------
|
329
|
-
export var pathTo = function(fname, searchDir,
|
330
|
-
var dirPath, dirpath, filepath, fpath, i, len, ref, subdir;
|
330
|
+
export var pathTo = function(fname, searchDir, hOptions = {}) {
|
331
|
+
var dirPath, direction, dirpath, filepath, fpath, i, len, nLevels, ref, relative, subdir;
|
332
|
+
({direction, relative} = hOptions);
|
333
|
+
if (isEmpty(direction)) {
|
334
|
+
direction = 'down';
|
335
|
+
}
|
336
|
+
if (isEmpty(relative)) {
|
337
|
+
relative = false;
|
338
|
+
}
|
331
339
|
debug("enter pathTo()", fname, searchDir, direction);
|
332
340
|
if (!searchDir) {
|
333
341
|
searchDir = process.cwd();
|
@@ -335,6 +343,9 @@ export var pathTo = function(fname, searchDir, direction = "down") {
|
|
335
343
|
assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
|
336
344
|
filepath = mkpath(searchDir, fname);
|
337
345
|
if (fs.existsSync(filepath)) {
|
346
|
+
if (relative) {
|
347
|
+
filepath = `./${fname}`;
|
348
|
+
}
|
338
349
|
debug("return from pathTo() - file exists", filepath);
|
339
350
|
return filepath;
|
340
351
|
}
|
@@ -346,19 +357,30 @@ export var pathTo = function(fname, searchDir, direction = "down") {
|
|
346
357
|
subdir = ref[i];
|
347
358
|
dirpath = mkpath(searchDir, subdir);
|
348
359
|
debug(`check ${subdir}`);
|
349
|
-
if (defined(fpath = pathTo(fname, dirpath))) {
|
360
|
+
if (defined(fpath = pathTo(fname, dirpath, hOptions))) {
|
361
|
+
if (relative) {
|
362
|
+
fpath = fpath.replace('./', `./${subdir}/`);
|
363
|
+
}
|
350
364
|
debug("return from pathTo()", fpath);
|
351
365
|
return fpath;
|
352
366
|
}
|
353
367
|
}
|
354
368
|
} else if (direction === 'up') {
|
369
|
+
nLevels = 0;
|
355
370
|
while (defined(dirPath = getParentDir(searchDir))) {
|
371
|
+
nLevels += 1;
|
356
372
|
debug(`check ${dirPath}`);
|
357
|
-
|
358
|
-
debug(`check for ${
|
359
|
-
if (fs.existsSync(
|
360
|
-
|
361
|
-
|
373
|
+
fpath = mkpath(dirPath, fname);
|
374
|
+
debug(`check for ${fpath}`);
|
375
|
+
if (fs.existsSync(fpath)) {
|
376
|
+
if (relative) {
|
377
|
+
fpath = "../".repeat(nLevels) + fname;
|
378
|
+
debug("return from pathTo()", fpath);
|
379
|
+
return fpath;
|
380
|
+
} else {
|
381
|
+
debug("return from pathTo()", fpath);
|
382
|
+
return fpath;
|
383
|
+
}
|
362
384
|
}
|
363
385
|
searchDir = dirPath;
|
364
386
|
}
|
@@ -375,11 +397,15 @@ export var allPathsTo = function(fname, searchDir) {
|
|
375
397
|
if (!searchDir) {
|
376
398
|
searchDir = process.cwd();
|
377
399
|
}
|
378
|
-
path = pathTo(fname, searchDir,
|
400
|
+
path = pathTo(fname, searchDir, {
|
401
|
+
direction: "up"
|
402
|
+
});
|
379
403
|
if (path != null) {
|
380
404
|
lPaths = [path]; // --- build an array of paths
|
381
405
|
// --- search upward for files, but return ordered top down
|
382
|
-
while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'),
|
406
|
+
while ((h = pathlib.parse(path)) && (path = pathTo(fname, pathlib.resolve(h.dir, '..'), {
|
407
|
+
direction: "up"
|
408
|
+
}))) {
|
383
409
|
lPaths.unshift(path);
|
384
410
|
}
|
385
411
|
return lPaths;
|