@jdeighan/coffee-utils 7.0.7 → 7.0.10

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.7",
4
+ "version": "7.0.10",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -6,12 +6,27 @@ import fs from 'fs'
6
6
  import NReadLines from 'n-readlines'
7
7
 
8
8
  import {
9
- assert, undef, pass, rtrim, error, nonEmpty,
10
- isString, isRegExp, isFunction, croak,
9
+ assert, undef, pass, rtrim, error, isEmpty, nonEmpty,
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.name != '$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';
@@ -14,8 +16,10 @@ import {
14
16
  pass,
15
17
  rtrim,
16
18
  error,
19
+ isEmpty,
17
20
  nonEmpty,
18
21
  isString,
22
+ isArray,
19
23
  isRegExp,
20
24
  isFunction,
21
25
  croak
@@ -30,6 +34,21 @@ import {
30
34
  debug
31
35
  } from '@jdeighan/coffee-utils/debug';
32
36
 
37
+ // ---------------------------------------------------------------------------
38
+ // mydir() - pass argument `import.meta.url` and it will return
39
+ // the directory your file is in
40
+ export var mydir = function(url) {
41
+ var dir, final, path;
42
+ debug(`url = ${url}`);
43
+ path = urllib.fileURLToPath(url);
44
+ debug(`path = ${path}`);
45
+ dir = pathlib.dirname(path);
46
+ debug(`dir = ${dir}`);
47
+ final = mkpath(dir);
48
+ debug(`final = ${final}`);
49
+ return final;
50
+ };
51
+
33
52
  // ---------------------------------------------------------------------------
34
53
  export var isFile = function(fullpath) {
35
54
  return fs.lstatSync(fullpath).isFile();
@@ -58,21 +77,6 @@ export var fileExt = function(path) {
58
77
  }
59
78
  };
60
79
 
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
80
  // ---------------------------------------------------------------------------
77
81
  export var mkpath = function(...lParts) {
78
82
  var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
@@ -163,6 +167,15 @@ export var slurp = function(filepath, maxLines = undef) {
163
167
  // barf - write a string to a file
164
168
  export var barf = function(filepath, contents) {
165
169
  debug(`enter barf('${filepath}')`, contents);
170
+ if (isEmpty(contents)) {
171
+ debug("return from barf(): empty contents");
172
+ return;
173
+ }
174
+ if (isArray(contents)) {
175
+ contents = arrayToBlock(contents);
176
+ } else if (!isString(contents)) {
177
+ croak("barf(): Invalid contents");
178
+ }
166
179
  contents = rtrim(contents) + "\n";
167
180
  fs.writeFileSync(filepath, contents, {
168
181
  encoding: 'utf8'
@@ -217,13 +230,18 @@ export var withUnderScore = function(path) {
217
230
  return mkpath(dir, `_${base}`);
218
231
  };
219
232
 
233
+ // ---------------------------------------------------------------------------
234
+ isSystemDir = function(dir) {
235
+ return dir === '$Recycle.Bin' || dir === '$WinREAgent';
236
+ };
237
+
220
238
  // ---------------------------------------------------------------------------
221
239
  // Get all subdirectories of a directory
222
240
  export var getSubDirs = function(dir) {
223
241
  return fs.readdirSync(dir, {
224
242
  withFileTypes: true
225
243
  }).filter(function(d) {
226
- return d.isDirectory() && (d.name !== '$Recycle.Bin');
244
+ return d.isDirectory() && !isSystemDir(d.name);
227
245
  }).map(function(d) {
228
246
  return mkpath(d.name);
229
247
  }).sort();