@jdeighan/coffee-utils 11.0.21 → 11.0.22

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 +35 -21
  3. package/src/fs.js +37 -25
  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.22",
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,13 @@ 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
+ pathToPkg = pathTo('package.json', dir, {direction: 'up'})
32
+
26
33
  # ---------------------------------------------------------------------------
27
34
  # myfile() - pass argument import.meta.url and it will return
28
35
  # the name of your file
@@ -44,18 +51,25 @@ export myfullpath = (url) ->
44
51
 
45
52
  # ---------------------------------------------------------------------------
46
53
 
54
+ export getStats = (fullpath) ->
55
+
56
+ return fs.lstatSync(fullpath)
57
+
58
+ # ---------------------------------------------------------------------------
59
+
47
60
  export isFile = (fullpath) ->
48
61
 
49
- return fs.lstatSync(fullpath).isFile()
62
+ try
63
+ return getStats(fullpath).isFile()
64
+ catch
65
+ return false
50
66
 
51
67
  # ---------------------------------------------------------------------------
52
68
 
53
69
  export isDir = (fullpath) ->
54
70
 
55
71
  try
56
- obj = fs.lstatSync(fullpath)
57
- if !obj? then return false
58
- return obj.isDirectory()
72
+ return getStats(fullpath).isDirectory()
59
73
  catch
60
74
  return false
61
75
 
@@ -253,41 +267,41 @@ export forEachFile = (dir, cb, filt=undef, level=0) ->
253
267
 
254
268
  # ---------------------------------------------------------------------------
255
269
 
256
- export pathTo = (fname, searchDir, hOptions={}) ->
270
+ export pathTo = (fname, searchDir, options=undef) ->
257
271
 
258
- {direction, relative} = hOptions
259
- if isEmpty(direction)
260
- direction = 'down'
261
- if isEmpty(relative)
262
- relative = false
272
+ {direction, relative} = getOptions(options, {
273
+ direction: 'down'
274
+ relative: false
275
+ })
263
276
 
264
277
  if ! searchDir
265
278
  searchDir = process.cwd()
266
- assert fs.existsSync(searchDir), "Dir #{searchDir} does not exist"
279
+ assert isDir(searchDir), "Not a directory: #{OL(searchDir)}"
267
280
  filepath = mkpath(searchDir, fname)
268
- if fs.existsSync(filepath)
281
+ if isFile(filepath)
269
282
  if relative
270
- filepath = "./#{fname}"
271
- return filepath
283
+ return "./#{fname}"
284
+ else
285
+ return filepath
272
286
 
273
287
  if (direction == 'down')
274
288
  # --- Search all directories in this directory
275
289
  # getSubDirs() returns dirs sorted alphabetically
276
290
  for subdir in getSubDirs(searchDir)
277
291
  dirpath = mkpath(searchDir, subdir)
278
- if defined(fpath = pathTo(fname, dirpath, hOptions))
292
+ if defined(fpath = pathTo(fname, dirpath, options))
279
293
  if relative
280
- fpath = fpath.replace('./', "./#{subdir}/")
281
- return fpath
294
+ return fpath.replace('./', "./#{subdir}/")
295
+ else
296
+ return fpath
282
297
  else if (direction == 'up')
283
298
  nLevels = 0
284
299
  while defined(dirPath = getParentDir(searchDir))
285
300
  nLevels += 1
286
301
  fpath = mkpath(dirPath, fname)
287
- if fs.existsSync(fpath)
302
+ if isFile(fpath)
288
303
  if relative
289
- fpath = "../".repeat(nLevels) + fname
290
- return fpath
304
+ return "../".repeat(nLevels) + fname
291
305
  else
292
306
  return fpath
293
307
  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, pathToPkg;
54
+ dir = mydir(url);
55
+ return pathToPkg = pathTo('package.json', dir, {
56
+ direction: 'up'
57
+ });
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,23 @@ export var forEachFile = function(dir, cb, filt = undef, level = 0) {
308
322
  };
309
323
 
310
324
  // ---------------------------------------------------------------------------
311
- export var pathTo = function(fname, searchDir, hOptions = {}) {
325
+ export var pathTo = function(fname, searchDir, options = undef) {
312
326
  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
- }
327
+ ({direction, relative} = getOptions(options, {
328
+ direction: 'down',
329
+ relative: false
330
+ }));
320
331
  if (!searchDir) {
321
332
  searchDir = process.cwd();
322
333
  }
323
- assert(fs.existsSync(searchDir), `Dir ${searchDir} does not exist`);
334
+ assert(isDir(searchDir), `Not a directory: ${OL(searchDir)}`);
324
335
  filepath = mkpath(searchDir, fname);
325
- if (fs.existsSync(filepath)) {
336
+ if (isFile(filepath)) {
326
337
  if (relative) {
327
- filepath = `./${fname}`;
338
+ return `./${fname}`;
339
+ } else {
340
+ return filepath;
328
341
  }
329
- return filepath;
330
342
  }
331
343
  if (direction === 'down') {
332
344
  ref = getSubDirs(searchDir);
@@ -335,11 +347,12 @@ export var pathTo = function(fname, searchDir, hOptions = {}) {
335
347
  for (i = 0, len = ref.length; i < len; i++) {
336
348
  subdir = ref[i];
337
349
  dirpath = mkpath(searchDir, subdir);
338
- if (defined(fpath = pathTo(fname, dirpath, hOptions))) {
350
+ if (defined(fpath = pathTo(fname, dirpath, options))) {
339
351
  if (relative) {
340
- fpath = fpath.replace('./', `./${subdir}/`);
352
+ return fpath.replace('./', `./${subdir}/`);
353
+ } else {
354
+ return fpath;
341
355
  }
342
- return fpath;
343
356
  }
344
357
  }
345
358
  } else if (direction === 'up') {
@@ -347,10 +360,9 @@ export var pathTo = function(fname, searchDir, hOptions = {}) {
347
360
  while (defined(dirPath = getParentDir(searchDir))) {
348
361
  nLevels += 1;
349
362
  fpath = mkpath(dirPath, fname);
350
- if (fs.existsSync(fpath)) {
363
+ if (isFile(fpath)) {
351
364
  if (relative) {
352
- fpath = "../".repeat(nLevels) + fname;
353
- return fpath;
365
+ return "../".repeat(nLevels) + fname;
354
366
  } else {
355
367
  return fpath;
356
368
  }
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}`);