@jdeighan/coffee-utils 7.0.8 → 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.8",
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()"
package/src/fs_utils.js CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  error,
19
19
  nonEmpty,
20
20
  isString,
21
+ isArray,
21
22
  isRegExp,
22
23
  isFunction,
23
24
  croak
@@ -32,6 +33,21 @@ import {
32
33
  debug
33
34
  } from '@jdeighan/coffee-utils/debug';
34
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
+
35
51
  // ---------------------------------------------------------------------------
36
52
  export var isFile = function(fullpath) {
37
53
  return fs.lstatSync(fullpath).isFile();
@@ -60,21 +76,6 @@ export var fileExt = function(path) {
60
76
  }
61
77
  };
62
78
 
63
- // ---------------------------------------------------------------------------
64
- // mydir() - pass argument `import.meta.url` and it will return
65
- // the directory your file is in
66
- export var mydir = function(url) {
67
- var dir, final, path;
68
- debug(`url = ${url}`);
69
- path = urllib.fileURLToPath(url);
70
- debug(`path = ${path}`);
71
- dir = pathlib.dirname(path);
72
- debug(`dir = ${dir}`);
73
- final = mkpath(dir);
74
- debug(`final = ${final}`);
75
- return final;
76
- };
77
-
78
79
  // ---------------------------------------------------------------------------
79
80
  export var mkpath = function(...lParts) {
80
81
  var _, drive, i, lMatches, lNewParts, len, newPath, part, rest;
@@ -165,6 +166,15 @@ export var slurp = function(filepath, maxLines = undef) {
165
166
  // barf - write a string to a file
166
167
  export var barf = function(filepath, contents) {
167
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
+ }
168
178
  contents = rtrim(contents) + "\n";
169
179
  fs.writeFileSync(filepath, contents, {
170
180
  encoding: 'utf8'