@jdeighan/coffee-utils 11.0.22 → 11.0.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/package.json +3 -3
  2. package/src/fs.coffee +14 -4
  3. package/src/fs.js +17 -9
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "11.0.22",
4
+ "version": "11.0.24",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -50,7 +50,7 @@
50
50
  },
51
51
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
52
52
  "dependencies": {
53
- "@jdeighan/exceptions": "^1.0.19",
53
+ "@jdeighan/exceptions": "^1.0.20",
54
54
  "cross-env": "^7.0.3",
55
55
  "js-yaml": "^4.1.0",
56
56
  "n-readlines": "^1.0.1",
@@ -58,6 +58,6 @@
58
58
  "svelte": "^3.52.0"
59
59
  },
60
60
  "devDependencies": {
61
- "@jdeighan/unit-tester": "^2.0.43"
61
+ "@jdeighan/unit-tester": "^2.0.44"
62
62
  }
63
63
  }
package/src/fs.coffee CHANGED
@@ -28,7 +28,9 @@ export mydir = (url) ->
28
28
  export projRoot = (url) ->
29
29
 
30
30
  dir = mydir(url)
31
- pathToPkg = pathTo('package.json', dir, {direction: 'up'})
31
+ rootDir = pathTo('package.json', dir, 'direction=up directory')
32
+ assert defined(rootDir), "No project root directory found"
33
+ return rootDir
32
34
 
33
35
  # ---------------------------------------------------------------------------
34
36
  # myfile() - pass argument import.meta.url and it will return
@@ -269,11 +271,13 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
269
271
 
270
272
  export pathTo = (fname, searchDir, options=undef) ->
271
273
 
272
- {direction, relative} = getOptions(options, {
274
+ {direction, relative, directory} = getOptions(options, {
273
275
  direction: 'down'
274
276
  relative: false
277
+ directory: false # return only the directory the file is in
275
278
  })
276
279
 
280
+ assert !(relative && directory), "relative & directory are incompatible"
277
281
  if ! searchDir
278
282
  searchDir = process.cwd()
279
283
  assert isDir(searchDir), "Not a directory: #{OL(searchDir)}"
@@ -281,6 +285,8 @@ export pathTo = (fname, searchDir, options=undef) ->
281
285
  if isFile(filepath)
282
286
  if relative
283
287
  return "./#{fname}"
288
+ else if directory
289
+ return searchDir
284
290
  else
285
291
  return filepath
286
292
 
@@ -288,10 +294,12 @@ export pathTo = (fname, searchDir, options=undef) ->
288
294
  # --- Search all directories in this directory
289
295
  # getSubDirs() returns dirs sorted alphabetically
290
296
  for subdir in getSubDirs(searchDir)
291
- dirpath = mkpath(searchDir, subdir)
292
- if defined(fpath = pathTo(fname, dirpath, options))
297
+ dirPath = mkpath(searchDir, subdir)
298
+ if defined(fpath = pathTo(fname, dirPath, options))
293
299
  if relative
294
300
  return fpath.replace('./', "./#{subdir}/")
301
+ else if directory
302
+ return dirPath
295
303
  else
296
304
  return fpath
297
305
  else if (direction == 'up')
@@ -302,6 +310,8 @@ export pathTo = (fname, searchDir, options=undef) ->
302
310
  if isFile(fpath)
303
311
  if relative
304
312
  return "../".repeat(nLevels) + fname
313
+ else if directory
314
+ return dirPath
305
315
  else
306
316
  return fpath
307
317
  searchDir = dirPath
package/src/fs.js CHANGED
@@ -50,11 +50,11 @@ export var mydir = function(url) {
50
50
 
51
51
  // ---------------------------------------------------------------------------
52
52
  export var projRoot = function(url) {
53
- var dir, pathToPkg;
53
+ var dir, rootDir;
54
54
  dir = mydir(url);
55
- return pathToPkg = pathTo('package.json', dir, {
56
- direction: 'up'
57
- });
55
+ rootDir = pathTo('package.json', dir, 'direction=up directory');
56
+ assert(defined(rootDir), "No project root directory found");
57
+ return rootDir;
58
58
  };
59
59
 
60
60
  // ---------------------------------------------------------------------------
@@ -323,11 +323,13 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
323
323
 
324
324
  // ---------------------------------------------------------------------------
325
325
  export var pathTo = function(fname, searchDir, options = undef) {
326
- var dirPath, direction, dirpath, filepath, fpath, i, len, nLevels, ref, relative, subdir;
327
- ({direction, relative} = getOptions(options, {
326
+ var dirPath, direction, directory, filepath, fpath, i, len, nLevels, ref, relative, subdir;
327
+ ({direction, relative, directory} = getOptions(options, {
328
328
  direction: 'down',
329
- relative: false
329
+ relative: false,
330
+ directory: false // return only the directory the file is in
330
331
  }));
332
+ assert(!(relative && directory), "relative & directory are incompatible");
331
333
  if (!searchDir) {
332
334
  searchDir = process.cwd();
333
335
  }
@@ -336,6 +338,8 @@ export var pathTo = function(fname, searchDir, options = undef) {
336
338
  if (isFile(filepath)) {
337
339
  if (relative) {
338
340
  return `./${fname}`;
341
+ } else if (directory) {
342
+ return searchDir;
339
343
  } else {
340
344
  return filepath;
341
345
  }
@@ -346,10 +350,12 @@ export var pathTo = function(fname, searchDir, options = undef) {
346
350
  // getSubDirs() returns dirs sorted alphabetically
347
351
  for (i = 0, len = ref.length; i < len; i++) {
348
352
  subdir = ref[i];
349
- dirpath = mkpath(searchDir, subdir);
350
- if (defined(fpath = pathTo(fname, dirpath, options))) {
353
+ dirPath = mkpath(searchDir, subdir);
354
+ if (defined(fpath = pathTo(fname, dirPath, options))) {
351
355
  if (relative) {
352
356
  return fpath.replace('./', `./${subdir}/`);
357
+ } else if (directory) {
358
+ return dirPath;
353
359
  } else {
354
360
  return fpath;
355
361
  }
@@ -363,6 +369,8 @@ export var pathTo = function(fname, searchDir, options = undef) {
363
369
  if (isFile(fpath)) {
364
370
  if (relative) {
365
371
  return "../".repeat(nLevels) + fname;
372
+ } else if (directory) {
373
+ return dirPath;
366
374
  } else {
367
375
  return fpath;
368
376
  }