@jdeighan/coffee-utils 16.0.4 → 16.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +3 -3
  2. package/src/fs.coffee +5 -107
  3. package/src/fs.js +16 -117
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "16.0.4",
4
+ "version": "16.0.6",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -45,14 +45,14 @@
45
45
  },
46
46
  "homepage": "https://github.com/johndeighan/coffee-utils#readme",
47
47
  "dependencies": {
48
- "@jdeighan/base-utils": "^4.0.29",
48
+ "@jdeighan/base-utils": "^6.0.0",
49
49
  "cross-env": "^7.0.3",
50
50
  "n-readlines": "^1.0.1",
51
51
  "readline-sync": "^1.4.10",
52
52
  "svelte": "^3.59.1"
53
53
  },
54
54
  "devDependencies": {
55
- "@jdeighan/unit-tester": "^3.0.46",
55
+ "@jdeighan/unit-tester": "^3.0.52",
56
56
  "ava": "^5.3.0"
57
57
  }
58
58
  }
package/src/fs.coffee CHANGED
@@ -16,11 +16,16 @@ import {
16
16
  isString, isArray, isHash, isRegExp, isFunction, isBoolean,
17
17
  OL, toBlock, getOptions, isArrayOfStrings,
18
18
  } from '@jdeighan/base-utils'
19
+ import {
20
+ mkpath, isFile, isDir, rmFileSync, mkdirSync,
21
+ } from '@jdeighan/base-utils/fs'
19
22
  import {assert, croak} from '@jdeighan/base-utils/exceptions'
20
23
  import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
21
24
  import {dbg, dbgEnter, dbgReturn} from '@jdeighan/base-utils/debug'
22
25
  import {fromTAML} from '@jdeighan/base-utils/taml'
23
26
 
27
+ export {mkpath, isFile, isDir, rmFileSync, mkdirSync}
28
+
24
29
  fix = true
25
30
 
26
31
  # ---------------------------------------------------------------------------
@@ -32,37 +37,6 @@ export doFixOutput = (flag=true) =>
32
37
 
33
38
  # ---------------------------------------------------------------------------
34
39
 
35
- export mkpath = (lParts...) =>
36
-
37
- # --- Ignore empty parts
38
- lNewParts = []
39
- for part in lParts
40
- if nonEmpty(part)
41
- lNewParts.push part
42
-
43
- newPath = lNewParts.join('/').replaceAll('\\', '/')
44
- if lMatches = newPath.match(/^([A-Z])\:(.*)$/)
45
- [_, drive, rest] = lMatches
46
- return "#{drive.toLowerCase()}:#{rest}"
47
- else
48
- return newPath
49
-
50
- # ---------------------------------------------------------------------------
51
-
52
- export mkdirSync = (dirpath) =>
53
-
54
- try
55
- fs.mkdirSync dirpath
56
- catch err
57
- if (err.code == 'EEXIST')
58
- console.log 'Directory exists. Please choose another name'
59
- else
60
- console.log err
61
- process.exit 1
62
- return
63
-
64
- # ---------------------------------------------------------------------------
65
-
66
40
  export rmDir = (dirpath) =>
67
41
 
68
42
  await rmdir dirpath, {recursive: true}
@@ -82,13 +56,6 @@ export rmFile = (filepath) =>
82
56
  await rm filepath
83
57
  return
84
58
 
85
- # ---------------------------------------------------------------------------
86
-
87
- export rmFileSync = (filepath) =>
88
-
89
- fs.rmSync filepath
90
- return
91
-
92
59
  # --------------------------------------------------------------------------
93
60
 
94
61
  export fixOutput = (contents) =>
@@ -212,24 +179,6 @@ export getStats = (fullpath) =>
212
179
 
213
180
  # ---------------------------------------------------------------------------
214
181
 
215
- export isFile = (fullpath) =>
216
-
217
- try
218
- return getStats(fullpath).isFile()
219
- catch
220
- return false
221
-
222
- # ---------------------------------------------------------------------------
223
-
224
- export isDir = (fullpath) =>
225
-
226
- try
227
- return getStats(fullpath).isDirectory()
228
- catch
229
- return false
230
-
231
- # ---------------------------------------------------------------------------
232
-
233
182
  export isSimpleFileName = (path) =>
234
183
 
235
184
  h = pathlib.parse(path)
@@ -361,50 +310,6 @@ export forEachSetOfBlocks = (filepath, func,
361
310
  func(lBlocks, firstLineNum)
362
311
  return
363
312
 
364
- # ---------------------------------------------------------------------------
365
- # slurp - read a file into a string
366
-
367
- export slurp = (filepath, hOptions={}) =>
368
-
369
- {maxLines, format} = getOptions(hOptions)
370
- if defined(maxLines)
371
- lLines = []
372
- forEachLineInFile filepath, (line, nLines) ->
373
- lLines.push line
374
- return (nLines >= maxLines)
375
- contents = toBlock(lLines)
376
- else
377
- filepath = filepath.replace(/\//g, "\\")
378
- contents = fs.readFileSync(filepath, 'utf8').toString()
379
- switch format
380
- when 'TAML'
381
- return fromTAML(contents)
382
- when 'JSON'
383
- return JSON.parse(contents)
384
- else
385
- assert notdefined(format), "Unknown format: #{format}"
386
- return contents
387
-
388
- # ---------------------------------------------------------------------------
389
- # barf - write a string to a file
390
-
391
- export barf = (filepath, contents='', hOptions={}) =>
392
-
393
- {format} = getOptions(hOptions)
394
- switch format
395
- when 'TAML'
396
- contents = toTAML(contents)
397
- when 'JSON'
398
- contents = JSON.stringify(contents, null, 3)
399
- else
400
- assert notdefined(format), "Unknown format: #{format}"
401
- if isArrayOfStrings(contents)
402
- contents = fixOutput(toBlock(contents))
403
- else if isString(contents)
404
- contents = fixOutput(contents)
405
- fs.writeFileSync(filepath, contents)
406
- return
407
-
408
313
  # ---------------------------------------------------------------------------
409
314
  # withExt - change file extention in a file name
410
315
 
@@ -638,10 +543,3 @@ export parseSource = (source) =>
638
543
  hSourceInfo.purpose = lMatches[1]
639
544
  dbgReturn "parseSource", hSourceInfo
640
545
  return hSourceInfo
641
-
642
- # ---------------------------------------------------------------------------
643
- # slurpTAML - read TAML from a file
644
-
645
- export slurpTAML = (filepath) =>
646
-
647
- return slurp(filepath, {format: 'TAML'})
package/src/fs.js CHANGED
@@ -45,6 +45,14 @@ import {
45
45
  isArrayOfStrings
46
46
  } from '@jdeighan/base-utils';
47
47
 
48
+ import {
49
+ mkpath,
50
+ isFile,
51
+ isDir,
52
+ rmFileSync,
53
+ mkdirSync
54
+ } from '@jdeighan/base-utils/fs';
55
+
48
56
  import {
49
57
  assert,
50
58
  croak
@@ -65,6 +73,14 @@ import {
65
73
  fromTAML
66
74
  } from '@jdeighan/base-utils/taml';
67
75
 
76
+ export {
77
+ mkpath,
78
+ isFile,
79
+ isDir,
80
+ rmFileSync,
81
+ mkdirSync
82
+ };
83
+
68
84
  fix = true;
69
85
 
70
86
  // ---------------------------------------------------------------------------
@@ -72,42 +88,6 @@ export var doFixOutput = (flag = true) => {
72
88
  fix = flag;
73
89
  };
74
90
 
75
- // ---------------------------------------------------------------------------
76
- export var mkpath = (...lParts) => {
77
- var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
78
- // --- Ignore empty parts
79
- lNewParts = [];
80
- for (i = 0, len = lParts.length; i < len; i++) {
81
- part = lParts[i];
82
- if (nonEmpty(part)) {
83
- lNewParts.push(part);
84
- }
85
- }
86
- newPath = lNewParts.join('/').replaceAll('\\', '/');
87
- if (lMatches = newPath.match(/^([A-Z])\:(.*)$/)) {
88
- [_, drive, rest] = lMatches;
89
- return `${drive.toLowerCase()}:${rest}`;
90
- } else {
91
- return newPath;
92
- }
93
- };
94
-
95
- // ---------------------------------------------------------------------------
96
- export var mkdirSync = (dirpath) => {
97
- var err;
98
- try {
99
- fs.mkdirSync(dirpath);
100
- } catch (error1) {
101
- err = error1;
102
- if (err.code === 'EEXIST') {
103
- console.log('Directory exists. Please choose another name');
104
- } else {
105
- console.log(err);
106
- }
107
- process.exit(1);
108
- }
109
- };
110
-
111
91
  // ---------------------------------------------------------------------------
112
92
  export var rmDir = async(dirpath) => {
113
93
  await rmdir(dirpath, {
@@ -127,11 +107,6 @@ export var rmFile = async(filepath) => {
127
107
  await rm(filepath);
128
108
  };
129
109
 
130
- // ---------------------------------------------------------------------------
131
- export var rmFileSync = (filepath) => {
132
- fs.rmSync(filepath);
133
- };
134
-
135
110
  // --------------------------------------------------------------------------
136
111
  export var fixOutput = (contents) => {
137
112
  if (fix && isString(contents)) {
@@ -264,24 +239,6 @@ export var getStats = (fullpath) => {
264
239
  return fs.lstatSync(fullpath);
265
240
  };
266
241
 
267
- // ---------------------------------------------------------------------------
268
- export var isFile = (fullpath) => {
269
- try {
270
- return getStats(fullpath).isFile();
271
- } catch (error1) {
272
- return false;
273
- }
274
- };
275
-
276
- // ---------------------------------------------------------------------------
277
- export var isDir = (fullpath) => {
278
- try {
279
- return getStats(fullpath).isDirectory();
280
- } catch (error1) {
281
- return false;
282
- }
283
- };
284
-
285
242
  // ---------------------------------------------------------------------------
286
243
  export var isSimpleFileName = (path) => {
287
244
  var h;
@@ -421,56 +378,6 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
421
378
  }
422
379
  };
423
380
 
424
- // ---------------------------------------------------------------------------
425
- // slurp - read a file into a string
426
- export var slurp = (filepath, hOptions = {}) => {
427
- var contents, format, lLines, maxLines;
428
- ({maxLines, format} = getOptions(hOptions));
429
- if (defined(maxLines)) {
430
- lLines = [];
431
- forEachLineInFile(filepath, function(line, nLines) {
432
- lLines.push(line);
433
- return nLines >= maxLines;
434
- });
435
- contents = toBlock(lLines);
436
- } else {
437
- filepath = filepath.replace(/\//g, "\\");
438
- contents = fs.readFileSync(filepath, 'utf8').toString();
439
- }
440
- switch (format) {
441
- case 'TAML':
442
- return fromTAML(contents);
443
- case 'JSON':
444
- return JSON.parse(contents);
445
- default:
446
- assert(notdefined(format), `Unknown format: ${format}`);
447
- return contents;
448
- }
449
- };
450
-
451
- // ---------------------------------------------------------------------------
452
- // barf - write a string to a file
453
- export var barf = (filepath, contents = '', hOptions = {}) => {
454
- var format;
455
- ({format} = getOptions(hOptions));
456
- switch (format) {
457
- case 'TAML':
458
- contents = toTAML(contents);
459
- break;
460
- case 'JSON':
461
- contents = JSON.stringify(contents, null, 3);
462
- break;
463
- default:
464
- assert(notdefined(format), `Unknown format: ${format}`);
465
- if (isArrayOfStrings(contents)) {
466
- contents = fixOutput(toBlock(contents));
467
- } else if (isString(contents)) {
468
- contents = fixOutput(contents);
469
- }
470
- }
471
- fs.writeFileSync(filepath, contents);
472
- };
473
-
474
381
  // ---------------------------------------------------------------------------
475
382
  // withExt - change file extention in a file name
476
383
  export var withExt = (path, newExt) => {
@@ -736,11 +643,3 @@ export var parseSource = (source) => {
736
643
  dbgReturn("parseSource", hSourceInfo);
737
644
  return hSourceInfo;
738
645
  };
739
-
740
- // ---------------------------------------------------------------------------
741
- // slurpTAML - read TAML from a file
742
- export var slurpTAML = (filepath) => {
743
- return slurp(filepath, {
744
- format: 'TAML'
745
- });
746
- };