@jdeighan/coffee-utils 11.0.21 → 11.0.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. package/package.json +4 -4
  2. package/src/fs.coffee +46 -22
  3. package/src/fs.js +47 -27
  4. package/temp.js +0 -26
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "11.0.21",
4
+ "version": "11.0.23",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -32,7 +32,7 @@
32
32
  "verbose": false
33
33
  },
34
34
  "scripts": {
35
- "build": "cls && rm -f ./src/*.js && coffee -c ./src",
35
+ "build": "cls && rm -f ./*.js && coffee -c .",
36
36
  "pretest": "cls && cielo -qfc ./test && coffee -c .",
37
37
  "test": "npm run build && ava ./test/*.test.js && git status"
38
38
  },
@@ -50,7 +50,7 @@
50
50
  },
51
51
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
52
52
  "dependencies": {
53
- "@jdeighan/exceptions": "^1.0.18",
53
+ "@jdeighan/exceptions": "^1.0.19",
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.42"
61
+ "@jdeighan/unit-tester": "^2.0.43"
62
62
  }
63
63
  }
package/src/fs.coffee CHANGED
@@ -7,7 +7,7 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {assert, croak, LOG, fromTAML} from '@jdeighan/exceptions'
9
9
  import {
10
- undef, pass, defined, rtrim, isEmpty, nonEmpty,
10
+ undef, pass, defined, rtrim, isEmpty, nonEmpty, getOptions,
11
11
  isString, isArray, isHash, isRegExp, isFunction, OL,
12
12
  } from '@jdeighan/coffee-utils'
13
13
  import {arrayToBlock} from '@jdeighan/coffee-utils/block'
@@ -23,6 +23,15 @@ export mydir = (url) ->
23
23
  final = mkpath(dir)
24
24
  return final
25
25
 
26
+ # ---------------------------------------------------------------------------
27
+
28
+ export projRoot = (url) ->
29
+
30
+ dir = mydir(url)
31
+ rootDir = pathTo('package.json', dir, 'direction=up directory')
32
+ assert defined(rootDir), "No project root directory found"
33
+ return rootDir
34
+
26
35
  # ---------------------------------------------------------------------------
27
36
  # myfile() - pass argument import.meta.url and it will return
28
37
  # the name of your file
@@ -44,18 +53,25 @@ export myfullpath = (url) ->
44
53
 
45
54
  # ---------------------------------------------------------------------------
46
55
 
56
+ export getStats = (fullpath) ->
57
+
58
+ return fs.lstatSync(fullpath)
59
+
60
+ # ---------------------------------------------------------------------------
61
+
47
62
  export isFile = (fullpath) ->
48
63
 
49
- return fs.lstatSync(fullpath).isFile()
64
+ try
65
+ return getStats(fullpath).isFile()
66
+ catch
67
+ return false
50
68
 
51
69
  # ---------------------------------------------------------------------------
52
70
 
53
71
  export isDir = (fullpath) ->
54
72
 
55
73
  try
56
- obj = fs.lstatSync(fullpath)
57
- if !obj? then return false
58
- return obj.isDirectory()
74
+ return getStats(fullpath).isDirectory()
59
75
  catch
60
76
  return false
61
77
 
@@ -253,41 +269,49 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
253
269
 
254
270
  # ---------------------------------------------------------------------------
255
271
 
256
- export pathTo = (fname, searchDir, hOptions={}) ->
272
+ export pathTo = (fname, searchDir, options=undef) ->
257
273
 
258
- {direction, relative} = hOptions
259
- if isEmpty(direction)
260
- direction = 'down'
261
- if isEmpty(relative)
262
- relative = false
274
+ {direction, relative, directory} = getOptions(options, {
275
+ direction: 'down'
276
+ relative: false
277
+ directory: false # return only the directory the file is in
278
+ })
263
279
 
280
+ assert !(relative && directory), "relative & directory are incompatible"
264
281
  if ! searchDir
265
282
  searchDir = process.cwd()
266
- assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
283
+ assert isDir(searchDir), "Not a directory: #{OL(searchDir)}"
267
284
  filepath = mkpath(searchDir, fname)
268
- if fs.existsSync(filepath)
285
+ if isFile(filepath)
269
286
  if relative
270
- filepath = "./#{fname}"
271
- return filepath
287
+ return "./#{fname}"
288
+ else if directory
289
+ return searchDir
290
+ else
291
+ return filepath
272
292
 
273
293
  if (direction == 'down')
274
294
  # --- Search all directories in this directory
275
295
  # getSubDirs() returns dirs sorted alphabetically
276
296
  for subdir in getSubDirs(searchDir)
277
- dirpath = mkpath(searchDir, subdir)
278
- if defined(fpath = pathTo(fname, dirpath, hOptions))
297
+ dirPath = mkpath(searchDir, subdir)
298
+ if defined(fpath = pathTo(fname, dirPath, options))
279
299
  if relative
280
- fpath = fpath.replace('./', "./#{subdir}/")
281
- return fpath
300
+ return fpath.replace('./', "./#{subdir}/")
301
+ else if directory
302
+ return dirPath
303
+ else
304
+ return fpath
282
305
  else if (direction == 'up')
283
306
  nLevels = 0
284
307
  while defined(dirPath = getParentDir(searchDir))
285
308
  nLevels += 1
286
309
  fpath = mkpath(dirPath, fname)
287
- if fs.existsSync(fpath)
310
+ if isFile(fpath)
288
311
  if relative
289
- fpath = "../".repeat(nLevels) + fname
290
- return fpath
312
+ return "../".repeat(nLevels) + fname
313
+ else if directory
314
+ return dirPath
291
315
  else
292
316
  return fpath
293
317
  searchDir = dirPath
package/src/fs.js CHANGED
@@ -24,6 +24,7 @@ import {
24
24
  rtrim,
25
25
  isEmpty,
26
26
  nonEmpty,
27
+ getOptions,
27
28
  isString,
28
29
  isArray,
29
30
  isHash,
@@ -47,6 +48,15 @@ export var mydir = function(url) {
47
48
  return final;
48
49
  };
49
50
 
51
+ // ---------------------------------------------------------------------------
52
+ export var projRoot = function(url) {
53
+ var dir, rootDir;
54
+ dir = mydir(url);
55
+ rootDir = pathTo('package.json', dir, 'direction=up directory');
56
+ assert(defined(rootDir), "No project root directory found");
57
+ return rootDir;
58
+ };
59
+
50
60
  // ---------------------------------------------------------------------------
51
61
  // myfile() - pass argument import.meta.url and it will return
52
62
  // the name of your file
@@ -66,20 +76,24 @@ export var myfullpath = function(url) {
66
76
  return mkpath(path);
67
77
  };
68
78
 
79
+ // ---------------------------------------------------------------------------
80
+ export var getStats = function(fullpath) {
81
+ return fs.lstatSync(fullpath);
82
+ };
83
+
69
84
  // ---------------------------------------------------------------------------
70
85
  export var isFile = function(fullpath) {
71
- return fs.lstatSync(fullpath).isFile();
86
+ try {
87
+ return getStats(fullpath).isFile();
88
+ } catch (error) {
89
+ return false;
90
+ }
72
91
  };
73
92
 
74
93
  // ---------------------------------------------------------------------------
75
94
  export var isDir = function(fullpath) {
76
- var obj;
77
95
  try {
78
- obj = fs.lstatSync(fullpath);
79
- if (obj == null) {
80
- return false;
81
- }
82
- return obj.isDirectory();
96
+ return getStats(fullpath).isDirectory();
83
97
  } catch (error) {
84
98
  return false;
85
99
  }
@@ -308,25 +322,27 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
308
322
  };
309
323
 
310
324
  // ---------------------------------------------------------------------------
311
- export var pathTo = function(fname, searchDir, hOptions = {}) {
312
- var dirPath, direction, dirpath, filepath, fpath, i, len, nLevels, ref, relative, subdir;
313
- ({direction, relative} = hOptions);
314
- if (isEmpty(direction)) {
315
- direction = 'down';
316
- }
317
- if (isEmpty(relative)) {
318
- relative = false;
319
- }
325
+ export var pathTo = function(fname, searchDir, options = undef) {
326
+ var dirPath, direction, directory, filepath, fpath, i, len, nLevels, ref, relative, subdir;
327
+ ({direction, relative, directory} = getOptions(options, {
328
+ direction: 'down',
329
+ relative: false,
330
+ directory: false // return only the directory the file is in
331
+ }));
332
+ assert(!(relative && directory), "relative & directory are incompatible");
320
333
  if (!searchDir) {
321
334
  searchDir = process.cwd();
322
335
  }
323
- assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
336
+ assert(isDir(searchDir), `Not a directory: ${OL(searchDir)}`);
324
337
  filepath = mkpath(searchDir, fname);
325
- if (fs.existsSync(filepath)) {
338
+ if (isFile(filepath)) {
326
339
  if (relative) {
327
- filepath = `./${fname}`;
340
+ return `./${fname}`;
341
+ } else if (directory) {
342
+ return searchDir;
343
+ } else {
344
+ return filepath;
328
345
  }
329
- return filepath;
330
346
  }
331
347
  if (direction === 'down') {
332
348
  ref = getSubDirs(searchDir);
@@ -334,12 +350,15 @@ export var pathTo = function(fname, searchDir, hOptions = {}) {
334
350
  // getSubDirs() returns dirs sorted alphabetically
335
351
  for (i = 0, len = ref.length; i < len; i++) {
336
352
  subdir = ref[i];
337
- dirpath = mkpath(searchDir, subdir);
338
- if (defined(fpath = pathTo(fname, dirpath, hOptions))) {
353
+ dirPath = mkpath(searchDir, subdir);
354
+ if (defined(fpath = pathTo(fname, dirPath, options))) {
339
355
  if (relative) {
340
- fpath = fpath.replace('./', `./${subdir}/`);
356
+ return fpath.replace('./', `./${subdir}/`);
357
+ } else if (directory) {
358
+ return dirPath;
359
+ } else {
360
+ return fpath;
341
361
  }
342
- return fpath;
343
362
  }
344
363
  }
345
364
  } else if (direction === 'up') {
@@ -347,10 +366,11 @@ export var pathTo = function(fname, searchDir, hOptions = {}) {
347
366
  while (defined(dirPath = getParentDir(searchDir))) {
348
367
  nLevels += 1;
349
368
  fpath = mkpath(dirPath, fname);
350
- if (fs.existsSync(fpath)) {
369
+ if (isFile(fpath)) {
351
370
  if (relative) {
352
- fpath = "../".repeat(nLevels) + fname;
353
- return fpath;
371
+ return "../".repeat(nLevels) + fname;
372
+ } else if (directory) {
373
+ return dirPath;
354
374
  } else {
355
375
  return fpath;
356
376
  }
package/temp.js DELETED
@@ -1,26 +0,0 @@
1
- // Generated by CoffeeScript 2.6.1
2
- // temp.coffee
3
- var dir;
4
-
5
- import {
6
- say
7
- } from '@jdeighan/coffee-utils';
8
-
9
- import {
10
- log
11
- } from '@jdeighan/coffee-utils/log';
12
-
13
- import {
14
- setDebugging
15
- } from '@jdeighan/coffee-utils/debug';
16
-
17
- import {
18
- mydir,
19
- mkpath
20
- } from '@jdeighan/coffee-utils/fs';
21
-
22
- setDebugging(true);
23
-
24
- dir = mydir(import.meta.url);
25
-
26
- say(`dir = ${dir}`);