@jdeighan/coffee-utils 14.0.36 → 14.0.37
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.
- package/package.json +4 -4
- package/src/fs.coffee +126 -30
- package/src/fs.js +142 -40
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdeighan/coffee-utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "14.0.
|
|
4
|
+
"version": "14.0.37",
|
|
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.
|
|
48
|
+
"@jdeighan/base-utils": "^4.0.26",
|
|
49
49
|
"cross-env": "^7.0.3",
|
|
50
50
|
"n-readlines": "^1.0.1",
|
|
51
51
|
"readline-sync": "^1.4.10",
|
|
52
|
-
"svelte": "^3.
|
|
52
|
+
"svelte": "^3.58.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@jdeighan/unit-tester": "^3.0.
|
|
55
|
+
"@jdeighan/unit-tester": "^3.0.44",
|
|
56
56
|
"ava": "^5.2.0"
|
|
57
57
|
}
|
|
58
58
|
}
|
package/src/fs.coffee
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
3
3
|
import pathlib from 'path'
|
|
4
4
|
import urllib from 'url'
|
|
5
5
|
import fs from 'fs'
|
|
6
|
+
import {
|
|
7
|
+
readFile, writeFile, rm,
|
|
8
|
+
} from 'node:fs/promises'
|
|
9
|
+
import {execSync} from 'node:child_process'
|
|
6
10
|
import readline from 'readline'
|
|
7
11
|
import NReadLines from 'n-readlines'
|
|
8
12
|
|
|
@@ -16,6 +20,102 @@ import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
|
|
|
16
20
|
import {dbg, dbgEnter, dbgReturn} from '@jdeighan/base-utils/debug'
|
|
17
21
|
import {fromTAML} from '@jdeighan/base-utils/taml'
|
|
18
22
|
|
|
23
|
+
# ---------------------------------------------------------------------------
|
|
24
|
+
|
|
25
|
+
export mkpath = (lParts...) =>
|
|
26
|
+
|
|
27
|
+
# --- Ignore empty parts
|
|
28
|
+
lNewParts = []
|
|
29
|
+
for part in lParts
|
|
30
|
+
if nonEmpty(part)
|
|
31
|
+
lNewParts.push part
|
|
32
|
+
|
|
33
|
+
newPath = lNewParts.join('/').replace(/\\/g, '/')
|
|
34
|
+
if lMatches = newPath.match(/^([A-Z])\:(.*)$/)
|
|
35
|
+
[_, drive, rest] = lMatches
|
|
36
|
+
return "#{drive.toLowerCase()}:#{rest}"
|
|
37
|
+
else
|
|
38
|
+
return newPath
|
|
39
|
+
|
|
40
|
+
# ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
export mkdir = (dirpath) =>
|
|
43
|
+
|
|
44
|
+
try
|
|
45
|
+
fs.mkdirSync dirpath
|
|
46
|
+
catch err
|
|
47
|
+
if (err.code == 'EEXIST')
|
|
48
|
+
console.log 'Directory exists. Please choose another name'
|
|
49
|
+
else
|
|
50
|
+
console.log err
|
|
51
|
+
process.exit 1
|
|
52
|
+
return
|
|
53
|
+
|
|
54
|
+
# ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
export mkfile = (filepath, contents='') =>
|
|
57
|
+
|
|
58
|
+
await writeFile filepath, contents
|
|
59
|
+
return
|
|
60
|
+
|
|
61
|
+
# ---------------------------------------------------------------------------
|
|
62
|
+
|
|
63
|
+
export rmdir = (dirpath) =>
|
|
64
|
+
|
|
65
|
+
await rm dirpath, {recursive: true}
|
|
66
|
+
return
|
|
67
|
+
|
|
68
|
+
# ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
export rmfile = (filepath) =>
|
|
71
|
+
|
|
72
|
+
await rm filepath
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
# ---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
export cdTo = (dirpath) =>
|
|
78
|
+
|
|
79
|
+
process.chdir dirpath
|
|
80
|
+
return process.cwd()
|
|
81
|
+
|
|
82
|
+
# ---------------------------------------------------------------------------
|
|
83
|
+
|
|
84
|
+
export cdToUserDir = (lSubDirs...) =>
|
|
85
|
+
|
|
86
|
+
return cd(mkpath('~', lSubDirs...))
|
|
87
|
+
|
|
88
|
+
# --------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
export execCmd = (cmdLine) =>
|
|
91
|
+
|
|
92
|
+
execSync cmdLine, {}, (error, stdout, stderr) =>
|
|
93
|
+
if (error)
|
|
94
|
+
LOG "ERROR in #{cmdLine}: #{error.code}"
|
|
95
|
+
process.exit 1
|
|
96
|
+
return stdout
|
|
97
|
+
|
|
98
|
+
# ---------------------------------------------------------------------------
|
|
99
|
+
|
|
100
|
+
export cloneRepo = (user, repo, dir) =>
|
|
101
|
+
|
|
102
|
+
git_repo = "https://github.com/#{user}/#{repo}.git"
|
|
103
|
+
return execCmd "git clone #{git_repo} #{dir}"
|
|
104
|
+
|
|
105
|
+
# ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
export getPkgJson = (filepath) =>
|
|
108
|
+
|
|
109
|
+
jsonTxt = fs.readFileSync(filepath, {encoding: 'utf8'})
|
|
110
|
+
hJson = JSON.parse jsonTxt
|
|
111
|
+
return
|
|
112
|
+
|
|
113
|
+
# ---------------------------------------------------------------------------
|
|
114
|
+
|
|
115
|
+
export putPkgJson = (filepath, hJson) =>
|
|
116
|
+
|
|
117
|
+
return
|
|
118
|
+
|
|
19
119
|
# ---------------------------------------------------------------------------
|
|
20
120
|
# mydir() - pass argument import.meta.url and it will return
|
|
21
121
|
# the directory your file is in
|
|
@@ -108,23 +208,6 @@ export fileExt = (path) =>
|
|
|
108
208
|
|
|
109
209
|
# ---------------------------------------------------------------------------
|
|
110
210
|
|
|
111
|
-
export mkpath = (lParts...) =>
|
|
112
|
-
|
|
113
|
-
# --- Ignore empty parts
|
|
114
|
-
lNewParts = []
|
|
115
|
-
for part in lParts
|
|
116
|
-
if nonEmpty(part)
|
|
117
|
-
lNewParts.push part
|
|
118
|
-
|
|
119
|
-
newPath = lNewParts.join('/').replace(/\\/g, '/')
|
|
120
|
-
if lMatches = newPath.match(/^([A-Z])\:(.*)$/)
|
|
121
|
-
[_, drive, rest] = lMatches
|
|
122
|
-
return "#{drive.toLowerCase()}:#{rest}"
|
|
123
|
-
else
|
|
124
|
-
return newPath
|
|
125
|
-
|
|
126
|
-
# ---------------------------------------------------------------------------
|
|
127
|
-
|
|
128
211
|
export getFullPath = (filepath) =>
|
|
129
212
|
|
|
130
213
|
return mkpath(pathlib.resolve(filepath))
|
|
@@ -230,10 +313,11 @@ export forEachSetOfBlocks = (filepath, func,
|
|
|
230
313
|
return
|
|
231
314
|
|
|
232
315
|
# ---------------------------------------------------------------------------
|
|
233
|
-
# slurp - read
|
|
316
|
+
# slurp - read a file into a string
|
|
234
317
|
|
|
235
|
-
export slurp = (filepath,
|
|
318
|
+
export slurp = (filepath, hOptions={}) =>
|
|
236
319
|
|
|
320
|
+
{maxLines, format} = getOptions(hOptions)
|
|
237
321
|
if defined(maxLines)
|
|
238
322
|
lLines = []
|
|
239
323
|
forEachLineInFile filepath, (line, nLines) ->
|
|
@@ -243,20 +327,33 @@ export slurp = (filepath, maxLines=undef) =>
|
|
|
243
327
|
else
|
|
244
328
|
filepath = filepath.replace(/\//g, "\\")
|
|
245
329
|
contents = fs.readFileSync(filepath, 'utf8').toString()
|
|
246
|
-
|
|
330
|
+
switch format
|
|
331
|
+
when 'TAML'
|
|
332
|
+
return fromTAML(contents)
|
|
333
|
+
when 'JSON'
|
|
334
|
+
return JSON.parse(contents)
|
|
335
|
+
else
|
|
336
|
+
assert notdefined(format), "Unknown format: #{format}"
|
|
337
|
+
return contents
|
|
247
338
|
|
|
248
339
|
# ---------------------------------------------------------------------------
|
|
249
340
|
# barf - write a string to a file
|
|
250
341
|
|
|
251
|
-
export barf = (filepath, contents) =>
|
|
342
|
+
export barf = (filepath, contents='', hOptions={}) =>
|
|
252
343
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
344
|
+
{format} = getOptions(hOptions)
|
|
345
|
+
switch format
|
|
346
|
+
when 'TAML'
|
|
347
|
+
contents = toTAML(contents)
|
|
348
|
+
when 'JSON'
|
|
349
|
+
contents = JSON.stringify(contents, null, 3)
|
|
350
|
+
else
|
|
351
|
+
assert notdefined(format), "Unknown format: #{format}"
|
|
352
|
+
if isArray(contents)
|
|
353
|
+
contents = toBlock(contents)
|
|
354
|
+
else if ! isString(contents)
|
|
355
|
+
croak "barf(): Invalid contents"
|
|
356
|
+
contents = rtrim(contents) + "\n"
|
|
260
357
|
fs.writeFileSync(filepath, contents, {encoding: 'utf8'})
|
|
261
358
|
return
|
|
262
359
|
|
|
@@ -499,5 +596,4 @@ export parseSource = (source) =>
|
|
|
499
596
|
|
|
500
597
|
export slurpTAML = (filepath) =>
|
|
501
598
|
|
|
502
|
-
|
|
503
|
-
return fromTAML(contents)
|
|
599
|
+
return slurp(filepath, {format: 'TAML'})
|
package/src/fs.js
CHANGED
|
@@ -8,6 +8,16 @@ import urllib from 'url';
|
|
|
8
8
|
|
|
9
9
|
import fs from 'fs';
|
|
10
10
|
|
|
11
|
+
import {
|
|
12
|
+
readFile,
|
|
13
|
+
writeFile,
|
|
14
|
+
rm
|
|
15
|
+
} from 'node:fs/promises';
|
|
16
|
+
|
|
17
|
+
import {
|
|
18
|
+
execSync
|
|
19
|
+
} from 'node:child_process';
|
|
20
|
+
|
|
11
21
|
import readline from 'readline';
|
|
12
22
|
|
|
13
23
|
import NReadLines from 'n-readlines';
|
|
@@ -51,6 +61,100 @@ import {
|
|
|
51
61
|
fromTAML
|
|
52
62
|
} from '@jdeighan/base-utils/taml';
|
|
53
63
|
|
|
64
|
+
// ---------------------------------------------------------------------------
|
|
65
|
+
export var mkpath = (...lParts) => {
|
|
66
|
+
var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
|
|
67
|
+
// --- Ignore empty parts
|
|
68
|
+
lNewParts = [];
|
|
69
|
+
for (i = 0, len = lParts.length; i < len; i++) {
|
|
70
|
+
part = lParts[i];
|
|
71
|
+
if (nonEmpty(part)) {
|
|
72
|
+
lNewParts.push(part);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
newPath = lNewParts.join('/').replace(/\\/g, '/');
|
|
76
|
+
if (lMatches = newPath.match(/^([A-Z])\:(.*)$/)) {
|
|
77
|
+
[_, drive, rest] = lMatches;
|
|
78
|
+
return `${drive.toLowerCase()}:${rest}`;
|
|
79
|
+
} else {
|
|
80
|
+
return newPath;
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// ---------------------------------------------------------------------------
|
|
85
|
+
export var mkdir = (dirpath) => {
|
|
86
|
+
var err;
|
|
87
|
+
try {
|
|
88
|
+
fs.mkdirSync(dirpath);
|
|
89
|
+
} catch (error1) {
|
|
90
|
+
err = error1;
|
|
91
|
+
if (err.code === 'EEXIST') {
|
|
92
|
+
console.log('Directory exists. Please choose another name');
|
|
93
|
+
} else {
|
|
94
|
+
console.log(err);
|
|
95
|
+
}
|
|
96
|
+
process.exit(1);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
export var mkfile = async(filepath, contents = '') => {
|
|
102
|
+
await writeFile(filepath, contents);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
export var rmdir = async(dirpath) => {
|
|
107
|
+
await rm(dirpath, {
|
|
108
|
+
recursive: true
|
|
109
|
+
});
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// ---------------------------------------------------------------------------
|
|
113
|
+
export var rmfile = async(filepath) => {
|
|
114
|
+
await rm(filepath);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
export var cdTo = (dirpath) => {
|
|
119
|
+
process.chdir(dirpath);
|
|
120
|
+
return process.cwd();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
export var cdToUserDir = (...lSubDirs) => {
|
|
125
|
+
return cd(mkpath('~', ...lSubDirs));
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// --------------------------------------------------------------------------
|
|
129
|
+
export var execCmd = (cmdLine) => {
|
|
130
|
+
execSync(cmdLine, {}, (error, stdout, stderr) => {
|
|
131
|
+
if (error) {
|
|
132
|
+
LOG(`ERROR in ${cmdLine}: ${error.code}`);
|
|
133
|
+
return process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
return stdout;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
export var cloneRepo = (user, repo, dir) => {
|
|
141
|
+
var git_repo;
|
|
142
|
+
git_repo = `https://github.com/${user}/${repo}.git`;
|
|
143
|
+
return execCmd(`git clone ${git_repo} ${dir}`);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
// ---------------------------------------------------------------------------
|
|
147
|
+
export var getPkgJson = (filepath) => {
|
|
148
|
+
var hJson, jsonTxt;
|
|
149
|
+
jsonTxt = fs.readFileSync(filepath, {
|
|
150
|
+
encoding: 'utf8'
|
|
151
|
+
});
|
|
152
|
+
hJson = JSON.parse(jsonTxt);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// ---------------------------------------------------------------------------
|
|
156
|
+
export var putPkgJson = (filepath, hJson) => {};
|
|
157
|
+
|
|
54
158
|
// ---------------------------------------------------------------------------
|
|
55
159
|
// mydir() - pass argument import.meta.url and it will return
|
|
56
160
|
// the directory your file is in
|
|
@@ -99,7 +203,7 @@ export var getStats = (fullpath) => {
|
|
|
99
203
|
export var isFile = (fullpath) => {
|
|
100
204
|
try {
|
|
101
205
|
return getStats(fullpath).isFile();
|
|
102
|
-
} catch (
|
|
206
|
+
} catch (error1) {
|
|
103
207
|
return false;
|
|
104
208
|
}
|
|
105
209
|
};
|
|
@@ -108,7 +212,7 @@ export var isFile = (fullpath) => {
|
|
|
108
212
|
export var isDir = (fullpath) => {
|
|
109
213
|
try {
|
|
110
214
|
return getStats(fullpath).isDirectory();
|
|
111
|
-
} catch (
|
|
215
|
+
} catch (error1) {
|
|
112
216
|
return false;
|
|
113
217
|
}
|
|
114
218
|
};
|
|
@@ -142,26 +246,6 @@ export var fileExt = (path) => {
|
|
|
142
246
|
}
|
|
143
247
|
};
|
|
144
248
|
|
|
145
|
-
// ---------------------------------------------------------------------------
|
|
146
|
-
export var mkpath = (...lParts) => {
|
|
147
|
-
var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
|
|
148
|
-
// --- Ignore empty parts
|
|
149
|
-
lNewParts = [];
|
|
150
|
-
for (i = 0, len = lParts.length; i < len; i++) {
|
|
151
|
-
part = lParts[i];
|
|
152
|
-
if (nonEmpty(part)) {
|
|
153
|
-
lNewParts.push(part);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
newPath = lNewParts.join('/').replace(/\\/g, '/');
|
|
157
|
-
if (lMatches = newPath.match(/^([A-Z])\:(.*)$/)) {
|
|
158
|
-
[_, drive, rest] = lMatches;
|
|
159
|
-
return `${drive.toLowerCase()}:${rest}`;
|
|
160
|
-
} else {
|
|
161
|
-
return newPath;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
|
|
165
249
|
// ---------------------------------------------------------------------------
|
|
166
250
|
export var getFullPath = (filepath) => {
|
|
167
251
|
return mkpath(pathlib.resolve(filepath));
|
|
@@ -273,9 +357,10 @@ export var forEachSetOfBlocks = (filepath, func, block_regexp = /^-{16,}$/, set_
|
|
|
273
357
|
};
|
|
274
358
|
|
|
275
359
|
// ---------------------------------------------------------------------------
|
|
276
|
-
// slurp - read
|
|
277
|
-
export var slurp = (filepath,
|
|
278
|
-
var contents, lLines;
|
|
360
|
+
// slurp - read a file into a string
|
|
361
|
+
export var slurp = (filepath, hOptions = {}) => {
|
|
362
|
+
var contents, format, lLines, maxLines;
|
|
363
|
+
({maxLines, format} = getOptions(hOptions));
|
|
279
364
|
if (defined(maxLines)) {
|
|
280
365
|
lLines = [];
|
|
281
366
|
forEachLineInFile(filepath, function(line, nLines) {
|
|
@@ -287,21 +372,38 @@ export var slurp = (filepath, maxLines = undef) => {
|
|
|
287
372
|
filepath = filepath.replace(/\//g, "\\");
|
|
288
373
|
contents = fs.readFileSync(filepath, 'utf8').toString();
|
|
289
374
|
}
|
|
290
|
-
|
|
375
|
+
switch (format) {
|
|
376
|
+
case 'TAML':
|
|
377
|
+
return fromTAML(contents);
|
|
378
|
+
case 'JSON':
|
|
379
|
+
return JSON.parse(contents);
|
|
380
|
+
default:
|
|
381
|
+
assert(notdefined(format), `Unknown format: ${format}`);
|
|
382
|
+
return contents;
|
|
383
|
+
}
|
|
291
384
|
};
|
|
292
385
|
|
|
293
386
|
// ---------------------------------------------------------------------------
|
|
294
387
|
// barf - write a string to a file
|
|
295
|
-
export var barf = (filepath, contents) => {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
388
|
+
export var barf = (filepath, contents = '', hOptions = {}) => {
|
|
389
|
+
var format;
|
|
390
|
+
({format} = getOptions(hOptions));
|
|
391
|
+
switch (format) {
|
|
392
|
+
case 'TAML':
|
|
393
|
+
contents = toTAML(contents);
|
|
394
|
+
break;
|
|
395
|
+
case 'JSON':
|
|
396
|
+
contents = JSON.stringify(contents, null, 3);
|
|
397
|
+
break;
|
|
398
|
+
default:
|
|
399
|
+
assert(notdefined(format), `Unknown format: ${format}`);
|
|
400
|
+
if (isArray(contents)) {
|
|
401
|
+
contents = toBlock(contents);
|
|
402
|
+
} else if (!isString(contents)) {
|
|
403
|
+
croak("barf(): Invalid contents");
|
|
404
|
+
}
|
|
405
|
+
contents = rtrim(contents) + "\n";
|
|
303
406
|
}
|
|
304
|
-
contents = rtrim(contents) + "\n";
|
|
305
407
|
fs.writeFileSync(filepath, contents, {
|
|
306
408
|
encoding: 'utf8'
|
|
307
409
|
});
|
|
@@ -333,8 +435,8 @@ export var removeFileWithExt = (path, newExt, hOptions = {}) => {
|
|
|
333
435
|
LOG(` unlink ${filename}`);
|
|
334
436
|
}
|
|
335
437
|
success = true;
|
|
336
|
-
} catch (
|
|
337
|
-
err =
|
|
438
|
+
} catch (error1) {
|
|
439
|
+
err = error1;
|
|
338
440
|
LOG(` UNLINK FAILED: ${err.message}`);
|
|
339
441
|
success = false;
|
|
340
442
|
}
|
|
@@ -576,7 +678,7 @@ export var parseSource = (source) => {
|
|
|
576
678
|
// ---------------------------------------------------------------------------
|
|
577
679
|
// slurpTAML - read TAML from a file
|
|
578
680
|
export var slurpTAML = (filepath) => {
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
681
|
+
return slurp(filepath, {
|
|
682
|
+
format: 'TAML'
|
|
683
|
+
});
|
|
582
684
|
};
|