@jdeighan/coffee-utils 7.0.6 → 7.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "7.0.6",
4
+ "version": "7.0.9",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -7,11 +7,26 @@ import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
9
  assert, undef, pass, rtrim, error, nonEmpty,
10
- isString, isRegExp, isFunction, croak,
10
+ isString, isArray, isRegExp, isFunction, croak,
11
11
  } from '@jdeighan/coffee-utils'
12
12
  import {log, LOG} from '@jdeighan/coffee-utils/log'
13
13
  import {debug} from '@jdeighan/coffee-utils/debug'
14
14
 
15
+ # ---------------------------------------------------------------------------
16
+ # mydir() - pass argument `import.meta.url` and it will return
17
+ # the directory your file is in
18
+
19
+ export mydir = (url) ->
20
+
21
+ debug "url = #{url}"
22
+ path = urllib.fileURLToPath(url)
23
+ debug "path = #{path}"
24
+ dir = pathlib.dirname(path)
25
+ debug "dir = #{dir}"
26
+ final = mkpath(dir)
27
+ debug "final = #{final}"
28
+ return final
29
+
15
30
  # ---------------------------------------------------------------------------
16
31
 
17
32
  export isFile = (fullpath) ->
@@ -41,21 +56,6 @@ export fileExt = (path) ->
41
56
  else
42
57
  return ''
43
58
 
44
- # ---------------------------------------------------------------------------
45
- # mydir() - pass argument `import.meta.url` and it will return
46
- # the directory your file is in
47
-
48
- export mydir = (url) ->
49
-
50
- debug "url = #{url}"
51
- path = urllib.fileURLToPath(url)
52
- debug "path = #{path}"
53
- dir = pathlib.dirname(path)
54
- debug "dir = #{dir}"
55
- final = mkpath(dir)
56
- debug "final = #{final}"
57
- return final
58
-
59
59
  # ---------------------------------------------------------------------------
60
60
 
61
61
  export mkpath = (lParts...) ->
@@ -138,6 +138,13 @@ export slurp = (filepath, maxLines=undef) ->
138
138
  export barf = (filepath, contents) ->
139
139
 
140
140
  debug "enter barf('#{filepath}')", contents
141
+ if isEmpty(contents)
142
+ debug "return from barf(): empty contents"
143
+ return
144
+ if isArray(contents)
145
+ contents = arrayToBlock(contents)
146
+ else if ! isString(contents)
147
+ croak "barf(): Invalid contents"
141
148
  contents = rtrim(contents) + "\n"
142
149
  fs.writeFileSync(filepath, contents, {encoding: 'utf8'})
143
150
  debug "return from barf()"
@@ -186,13 +193,19 @@ export withUnderScore = (path) ->
186
193
  {dir, base} = pathlib.parse(path)
187
194
  return mkpath(dir, "_#{base}")
188
195
 
196
+ # ---------------------------------------------------------------------------
197
+
198
+ isSystemDir = (dir) ->
199
+
200
+ return dir in ['$Recycle.Bin', '$WinREAgent']
201
+
189
202
  # ---------------------------------------------------------------------------
190
203
  # Get all subdirectories of a directory
191
204
 
192
205
  export getSubDirs = (dir) ->
193
206
 
194
207
  return fs.readdirSync(dir, {withFileTypes: true}) \
195
- .filter((d) -> d.isDirectory() && (d != '$Recycle.Bin')) \
208
+ .filter((d) -> d.isDirectory() && !isSystemDir(d.name)) \
196
209
  .map((d) -> mkpath(d.name)) \
197
210
  .sort()
198
211
 
package/src/fs_utils.js CHANGED
@@ -1,5 +1,7 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
2
  // fs_utils.coffee
3
+ var isSystemDir;
4
+
3
5
  import pathlib from 'path';
4
6
 
5
7
  import urllib from 'url';
@@ -16,6 +18,7 @@ import {
16
18
  error,
17
19
  nonEmpty,
18
20
  isString,
21
+ isArray,
19
22
  isRegExp,
20
23
  isFunction,
21
24
  croak
@@ -30,6 +33,21 @@ import {
30
33
  debug
31
34
  } from '@jdeighan/coffee-utils/debug';
32
35
 
36
+ // ---------------------------------------------------------------------------
37
+ // mydir() - pass argument `import.meta.url` and it will return
38
+ // the directory your file is in
39
+ export var mydir = function(url) {
40
+ var dir, final, path;
41
+ debug(`url = ${url}`);
42
+ path = urllib.fileURLToPath(url);
43
+ debug(`path = ${path}`);
44
+ dir = pathlib.dirname(path);
45
+ debug(`dir = ${dir}`);
46
+ final = mkpath(dir);
47
+ debug(`final = ${final}`);
48
+ return final;
49
+ };
50
+
33
51
  // ---------------------------------------------------------------------------
34
52
  export var isFile = function(fullpath) {
35
53
  return fs.lstatSync(fullpath).isFile();
@@ -58,21 +76,6 @@ export var fileExt = function(path) {
58
76
  }
59
77
  };
60
78
 
61
- // ---------------------------------------------------------------------------
62
- // mydir() - pass argument `import.meta.url` and it will return
63
- // the directory your file is in
64
- export var mydir = function(url) {
65
- var dir, final, path;
66
- debug(`url = ${url}`);
67
- path = urllib.fileURLToPath(url);
68
- debug(`path = ${path}`);
69
- dir = pathlib.dirname(path);
70
- debug(`dir = ${dir}`);
71
- final = mkpath(dir);
72
- debug(`final = ${final}`);
73
- return final;
74
- };
75
-
76
79
  // ---------------------------------------------------------------------------
77
80
  export var mkpath = function(...lParts) {
78
81
  var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
@@ -163,6 +166,15 @@ export var slurp = function(filepath, maxLines = undef) {
163
166
  // barf - write a string to a file
164
167
  export var barf = function(filepath, contents) {
165
168
  debug(`enter barf('${filepath}')`, contents);
169
+ if (isEmpty(contents)) {
170
+ debug("return from barf(): empty contents");
171
+ return;
172
+ }
173
+ if (isArray(contents)) {
174
+ contents = arrayToBlock(contents);
175
+ } else if (!isString(contents)) {
176
+ croak("barf(): Invalid contents");
177
+ }
166
178
  contents = rtrim(contents) + "\n";
167
179
  fs.writeFileSync(filepath, contents, {
168
180
  encoding: 'utf8'
@@ -217,13 +229,18 @@ export var withUnderScore = function(path) {
217
229
  return mkpath(dir, `_${base}`);
218
230
  };
219
231
 
232
+ // ---------------------------------------------------------------------------
233
+ isSystemDir = function(dir) {
234
+ return dir === '$Recycle.Bin' || dir === '$WinREAgent';
235
+ };
236
+
220
237
  // ---------------------------------------------------------------------------
221
238
  // Get all subdirectories of a directory
222
239
  export var getSubDirs = function(dir) {
223
240
  return fs.readdirSync(dir, {
224
241
  withFileTypes: true
225
242
  }).filter(function(d) {
226
- return d.isDirectory() && (d !== '$Recycle.Bin');
243
+ return d.isDirectory() && !isSystemDir(d.name);
227
244
  }).map(function(d) {
228
245
  return mkpath(d.name);
229
246
  }).sort();