@jdeighan/coffee-utils 14.0.37 → 15.0.0

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 +1 -1
  2. package/src/fs.coffee +79 -30
  3. package/src/fs.js +93 -29
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "14.0.37",
4
+ "version": "15.0.0",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
package/src/fs.coffee CHANGED
@@ -1,10 +1,11 @@
1
1
  # fs.coffee
2
2
 
3
+ import os from 'os'
3
4
  import pathlib from 'path'
4
5
  import urllib from 'url'
5
6
  import fs from 'fs'
6
7
  import {
7
- readFile, writeFile, rm,
8
+ readFile, writeFile, rm, rmdir, # rmSync, rmdirSync,
8
9
  } from 'node:fs/promises'
9
10
  import {execSync} from 'node:child_process'
10
11
  import readline from 'readline'
@@ -20,6 +21,15 @@ import {LOG, LOGVALUE} from '@jdeighan/base-utils/log'
20
21
  import {dbg, dbgEnter, dbgReturn} from '@jdeighan/base-utils/debug'
21
22
  import {fromTAML} from '@jdeighan/base-utils/taml'
22
23
 
24
+ fix = true
25
+
26
+ # ---------------------------------------------------------------------------
27
+
28
+ export doFixOutput = (flag=true) =>
29
+
30
+ fix = flag
31
+ return
32
+
23
33
  # ---------------------------------------------------------------------------
24
34
 
25
35
  export mkpath = (lParts...) =>
@@ -30,7 +40,7 @@ export mkpath = (lParts...) =>
30
40
  if nonEmpty(part)
31
41
  lNewParts.push part
32
42
 
33
- newPath = lNewParts.join('/').replace(/\\/g, '/')
43
+ newPath = lNewParts.join('/').replaceAll('\\', '/')
34
44
  if lMatches = newPath.match(/^([A-Z])\:(.*)$/)
35
45
  [_, drive, rest] = lMatches
36
46
  return "#{drive.toLowerCase()}:#{rest}"
@@ -39,7 +49,7 @@ export mkpath = (lParts...) =>
39
49
 
40
50
  # ---------------------------------------------------------------------------
41
51
 
42
- export mkdir = (dirpath) =>
52
+ export mkdirSync = (dirpath) =>
43
53
 
44
54
  try
45
55
  fs.mkdirSync dirpath
@@ -53,41 +63,88 @@ export mkdir = (dirpath) =>
53
63
 
54
64
  # ---------------------------------------------------------------------------
55
65
 
56
- export mkfile = (filepath, contents='') =>
66
+ export rmDir = (dirpath) =>
57
67
 
58
- await writeFile filepath, contents
68
+ await rmdir dirpath, {recursive: true}
59
69
  return
60
70
 
61
71
  # ---------------------------------------------------------------------------
62
72
 
63
- export rmdir = (dirpath) =>
73
+ export rmDirSync = (dirpath) =>
64
74
 
65
- await rm dirpath, {recursive: true}
75
+ fs.rmdirSync dirpath, {recursive: true}
66
76
  return
67
77
 
68
78
  # ---------------------------------------------------------------------------
69
79
 
70
- export rmfile = (filepath) =>
80
+ export rmFile = (filepath) =>
71
81
 
72
82
  await rm filepath
73
83
  return
74
84
 
75
85
  # ---------------------------------------------------------------------------
76
86
 
77
- export cdTo = (dirpath) =>
87
+ export rmFileSync = (filepath) =>
78
88
 
79
- process.chdir dirpath
80
- return process.cwd()
89
+ fs.rmSync filepath
90
+ return
81
91
 
82
- # ---------------------------------------------------------------------------
92
+ # --------------------------------------------------------------------------
93
+
94
+ export fixOutput = (contents) =>
95
+
96
+ if fix
97
+ return rtrim(contents) + "\n"
98
+ else
99
+ return contents
100
+
101
+ # --------------------------------------------------------------------------
102
+
103
+ export fixFile = (filepath, func) =>
104
+
105
+ contents = await readFile(filepath, {encoding: 'utf8'})
106
+ output = func(contents) # returns modified contents
107
+ output = fixOutput(output)
108
+ await writeFile(filepath, output, {encoding: 'utf8'})
109
+ return
110
+
111
+ # --------------------------------------------------------------------------
112
+
113
+ export fixJson = (filepath, func) =>
114
+
115
+ contents = await readFile(filepath, {encoding: 'utf8'})
116
+ hJson = JSON.parse(contents)
117
+ func(hJson) # modify in place
118
+ output = JSON.stringify(hJson, null, 3)
119
+ output = fixOutput(output)
120
+ await writeFile(filepath, output, {encoding: 'utf8'})
121
+ return
83
122
 
84
- export cdToUserDir = (lSubDirs...) =>
123
+ # --------------------------------------------------------------------------
124
+
125
+ export fixFileSync = (filepath, func) =>
85
126
 
86
- return cd(mkpath('~', lSubDirs...))
127
+ contents = fs.readFileSync(filepath, {encoding: 'utf8'})
128
+ output = func(contents) # returns modified contents
129
+ output = fixOutput(output)
130
+ fs.writeFileSync(filepath, output, {encoding: 'utf8'})
131
+ return
87
132
 
88
133
  # --------------------------------------------------------------------------
89
134
 
90
- export execCmd = (cmdLine) =>
135
+ export fixJsonSync = (filepath, func) =>
136
+
137
+ contents = fs.readFileSync(filepath, {encoding: 'utf8'})
138
+ hJson = JSON.parse(contents)
139
+ func(hJson) # modify in place
140
+ output = JSON.stringify(hJson, null, 3)
141
+ output = fixOutput(output)
142
+ fs.writeFileSync(filepath, output, {encoding: 'utf8'})
143
+ return
144
+
145
+ # --------------------------------------------------------------------------
146
+
147
+ export execCmdSync = (cmdLine) =>
91
148
 
92
149
  execSync cmdLine, {}, (error, stdout, stderr) =>
93
150
  if (error)
@@ -102,20 +159,6 @@ export cloneRepo = (user, repo, dir) =>
102
159
  git_repo = "https://github.com/#{user}/#{repo}.git"
103
160
  return execCmd "git clone #{git_repo} #{dir}"
104
161
 
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
-
119
162
  # ---------------------------------------------------------------------------
120
163
  # mydir() - pass argument import.meta.url and it will return
121
164
  # the directory your file is in
@@ -129,6 +172,12 @@ export mydir = (url) =>
129
172
 
130
173
  # ---------------------------------------------------------------------------
131
174
 
175
+ export homeDir = () =>
176
+
177
+ return mkpath(os.homedir())
178
+
179
+ # ---------------------------------------------------------------------------
180
+
132
181
  export projRoot = (url) =>
133
182
 
134
183
  dir = mydir(url)
@@ -353,7 +402,7 @@ export barf = (filepath, contents='', hOptions={}) =>
353
402
  contents = toBlock(contents)
354
403
  else if ! isString(contents)
355
404
  croak "barf(): Invalid contents"
356
- contents = rtrim(contents) + "\n"
405
+ contents = fixOutput(contents)
357
406
  fs.writeFileSync(filepath, contents, {encoding: 'utf8'})
358
407
  return
359
408
 
package/src/fs.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // fs.coffee
3
- var isSystemDir;
3
+ var fix, isSystemDir;
4
+
5
+ import os from 'os';
4
6
 
5
7
  import pathlib from 'path';
6
8
 
@@ -11,7 +13,8 @@ import fs from 'fs';
11
13
  import {
12
14
  readFile,
13
15
  writeFile,
14
- rm
16
+ rm,
17
+ rmdir // rmSync, rmdirSync,
15
18
  } from 'node:fs/promises';
16
19
 
17
20
  import {
@@ -61,6 +64,13 @@ import {
61
64
  fromTAML
62
65
  } from '@jdeighan/base-utils/taml';
63
66
 
67
+ fix = true;
68
+
69
+ // ---------------------------------------------------------------------------
70
+ export var doFixOutput = (flag = true) => {
71
+ fix = flag;
72
+ };
73
+
64
74
  // ---------------------------------------------------------------------------
65
75
  export var mkpath = (...lParts) => {
66
76
  var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
@@ -72,7 +82,7 @@ export var mkpath = (...lParts) => {
72
82
  lNewParts.push(part);
73
83
  }
74
84
  }
75
- newPath = lNewParts.join('/').replace(/\\/g, '/');
85
+ newPath = lNewParts.join('/').replaceAll('\\', '/');
76
86
  if (lMatches = newPath.match(/^([A-Z])\:(.*)$/)) {
77
87
  [_, drive, rest] = lMatches;
78
88
  return `${drive.toLowerCase()}:${rest}`;
@@ -82,7 +92,7 @@ export var mkpath = (...lParts) => {
82
92
  };
83
93
 
84
94
  // ---------------------------------------------------------------------------
85
- export var mkdir = (dirpath) => {
95
+ export var mkdirSync = (dirpath) => {
86
96
  var err;
87
97
  try {
88
98
  fs.mkdirSync(dirpath);
@@ -98,35 +108,96 @@ export var mkdir = (dirpath) => {
98
108
  };
99
109
 
100
110
  // ---------------------------------------------------------------------------
101
- export var mkfile = async(filepath, contents = '') => {
102
- await writeFile(filepath, contents);
111
+ export var rmDir = async(dirpath) => {
112
+ await rmdir(dirpath, {
113
+ recursive: true
114
+ });
103
115
  };
104
116
 
105
117
  // ---------------------------------------------------------------------------
106
- export var rmdir = async(dirpath) => {
107
- await rm(dirpath, {
118
+ export var rmDirSync = (dirpath) => {
119
+ fs.rmdirSync(dirpath, {
108
120
  recursive: true
109
121
  });
110
122
  };
111
123
 
112
124
  // ---------------------------------------------------------------------------
113
- export var rmfile = async(filepath) => {
125
+ export var rmFile = async(filepath) => {
114
126
  await rm(filepath);
115
127
  };
116
128
 
117
129
  // ---------------------------------------------------------------------------
118
- export var cdTo = (dirpath) => {
119
- process.chdir(dirpath);
120
- return process.cwd();
130
+ export var rmFileSync = (filepath) => {
131
+ fs.rmSync(filepath);
121
132
  };
122
133
 
123
- // ---------------------------------------------------------------------------
124
- export var cdToUserDir = (...lSubDirs) => {
125
- return cd(mkpath('~', ...lSubDirs));
134
+ // --------------------------------------------------------------------------
135
+ export var fixOutput = (contents) => {
136
+ if (fix) {
137
+ return rtrim(contents) + "\n";
138
+ } else {
139
+ return contents;
140
+ }
141
+ };
142
+
143
+ // --------------------------------------------------------------------------
144
+ export var fixFile = async(filepath, func) => {
145
+ var contents, output;
146
+ contents = (await readFile(filepath, {
147
+ encoding: 'utf8'
148
+ }));
149
+ output = func(contents); // returns modified contents
150
+ output = fixOutput(output);
151
+ await writeFile(filepath, output, {
152
+ encoding: 'utf8'
153
+ });
154
+ };
155
+
156
+ // --------------------------------------------------------------------------
157
+ export var fixJson = async(filepath, func) => {
158
+ var contents, hJson, output;
159
+ contents = (await readFile(filepath, {
160
+ encoding: 'utf8'
161
+ }));
162
+ hJson = JSON.parse(contents);
163
+ func(hJson); // modify in place
164
+ output = JSON.stringify(hJson, null, 3);
165
+ output = fixOutput(output);
166
+ await writeFile(filepath, output, {
167
+ encoding: 'utf8'
168
+ });
126
169
  };
127
170
 
128
171
  // --------------------------------------------------------------------------
129
- export var execCmd = (cmdLine) => {
172
+ export var fixFileSync = (filepath, func) => {
173
+ var contents, output;
174
+ contents = fs.readFileSync(filepath, {
175
+ encoding: 'utf8'
176
+ });
177
+ output = func(contents); // returns modified contents
178
+ output = fixOutput(output);
179
+ fs.writeFileSync(filepath, output, {
180
+ encoding: 'utf8'
181
+ });
182
+ };
183
+
184
+ // --------------------------------------------------------------------------
185
+ export var fixJsonSync = (filepath, func) => {
186
+ var contents, hJson, output;
187
+ contents = fs.readFileSync(filepath, {
188
+ encoding: 'utf8'
189
+ });
190
+ hJson = JSON.parse(contents);
191
+ func(hJson); // modify in place
192
+ output = JSON.stringify(hJson, null, 3);
193
+ output = fixOutput(output);
194
+ fs.writeFileSync(filepath, output, {
195
+ encoding: 'utf8'
196
+ });
197
+ };
198
+
199
+ // --------------------------------------------------------------------------
200
+ export var execCmdSync = (cmdLine) => {
130
201
  execSync(cmdLine, {}, (error, stdout, stderr) => {
131
202
  if (error) {
132
203
  LOG(`ERROR in ${cmdLine}: ${error.code}`);
@@ -143,18 +214,6 @@ export var cloneRepo = (user, repo, dir) => {
143
214
  return execCmd(`git clone ${git_repo} ${dir}`);
144
215
  };
145
216
 
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
-
158
217
  // ---------------------------------------------------------------------------
159
218
  // mydir() - pass argument import.meta.url and it will return
160
219
  // the directory your file is in
@@ -166,6 +225,11 @@ export var mydir = (url) => {
166
225
  return final;
167
226
  };
168
227
 
228
+ // ---------------------------------------------------------------------------
229
+ export var homeDir = () => {
230
+ return mkpath(os.homedir());
231
+ };
232
+
169
233
  // ---------------------------------------------------------------------------
170
234
  export var projRoot = (url) => {
171
235
  var dir, rootDir;
@@ -402,7 +466,7 @@ export var barf = (filepath, contents = '', hOptions = {}) => {
402
466
  } else if (!isString(contents)) {
403
467
  croak("barf(): Invalid contents");
404
468
  }
405
- contents = rtrim(contents) + "\n";
469
+ contents = fixOutput(contents);
406
470
  }
407
471
  fs.writeFileSync(filepath, contents, {
408
472
  encoding: 'utf8'