@jdeighan/coffee-utils 2.1.5 → 2.1.6
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 +1 -1
- package/src/coffee_utils.coffee +6 -0
- package/src/coffee_utils.js +5 -0
- package/src/fs_utils.coffee +29 -1
- package/src/fs_utils.js +44 -1
package/package.json
CHANGED
package/src/coffee_utils.coffee
CHANGED
@@ -188,6 +188,12 @@ export isFunction = (x) ->
|
|
188
188
|
|
189
189
|
# ---------------------------------------------------------------------------
|
190
190
|
|
191
|
+
export isRegExp = (x) ->
|
192
|
+
|
193
|
+
return x instanceof RegExp
|
194
|
+
|
195
|
+
# ---------------------------------------------------------------------------
|
196
|
+
|
191
197
|
export isInteger = (x) ->
|
192
198
|
|
193
199
|
if (typeof x == 'number')
|
package/src/coffee_utils.js
CHANGED
@@ -195,6 +195,11 @@ export var isFunction = function(x) {
|
|
195
195
|
return typeof x === 'function';
|
196
196
|
};
|
197
197
|
|
198
|
+
// ---------------------------------------------------------------------------
|
199
|
+
export var isRegExp = function(x) {
|
200
|
+
return x instanceof RegExp;
|
201
|
+
};
|
202
|
+
|
198
203
|
// ---------------------------------------------------------------------------
|
199
204
|
export var isInteger = function(x) {
|
200
205
|
if (typeof x === 'number') {
|
package/src/fs_utils.coffee
CHANGED
@@ -11,7 +11,8 @@ import {
|
|
11
11
|
} from 'fs'
|
12
12
|
|
13
13
|
import {
|
14
|
-
undef, pass, firstLine, rtrim, error,
|
14
|
+
undef, pass, firstLine, rtrim, error, nonEmpty,
|
15
|
+
isRegExp, isFunction, croak,
|
15
16
|
} from '@jdeighan/coffee-utils'
|
16
17
|
import {log} from '@jdeighan/coffee-utils/log'
|
17
18
|
import {debug} from '@jdeighan/coffee-utils/debug'
|
@@ -117,6 +118,33 @@ export getParentDir = (dir) ->
|
|
117
118
|
|
118
119
|
# ---------------------------------------------------------------------------
|
119
120
|
|
121
|
+
export forEachFile = (dir, cb, filt=undef, level=0) ->
|
122
|
+
# --- filt can be a regular expression or a function that gets:
|
123
|
+
# (filename, dir, level)
|
124
|
+
# callback will get parms (filename, dir, level)
|
125
|
+
|
126
|
+
lSubDirectories = []
|
127
|
+
for ent in readdirSync(dir, {withFileTypes: true})
|
128
|
+
if ent.isDirectory()
|
129
|
+
lSubDirectories.push ent
|
130
|
+
else if ent.isFile()
|
131
|
+
if not filt?
|
132
|
+
cb(ent.name, dir, level)
|
133
|
+
else if isRegExp(filt)
|
134
|
+
if ent.name.match(filt)
|
135
|
+
cb(ent.name, dir, level)
|
136
|
+
else if isFunction(filt)
|
137
|
+
if filt(ent.name, dir, level)
|
138
|
+
cb(ent.name, dir, level)
|
139
|
+
else
|
140
|
+
croak "forEachFile(): bad filter", 'filter', filt
|
141
|
+
if nonEmpty(lSubDirectories)
|
142
|
+
for subdir in lSubDirectories.sort()
|
143
|
+
forEachFile(mkpath(dir, subdir.name), cb, filt, level+1)
|
144
|
+
return
|
145
|
+
|
146
|
+
# ---------------------------------------------------------------------------
|
147
|
+
|
120
148
|
export pathTo = (fname, dir, direction="down") ->
|
121
149
|
|
122
150
|
debug "enter pathTo('#{fname}','#{dir}','#{direction}')"
|
package/src/fs_utils.js
CHANGED
@@ -28,7 +28,11 @@ import {
|
|
28
28
|
pass,
|
29
29
|
firstLine,
|
30
30
|
rtrim,
|
31
|
-
error
|
31
|
+
error,
|
32
|
+
nonEmpty,
|
33
|
+
isRegExp,
|
34
|
+
isFunction,
|
35
|
+
croak
|
32
36
|
} from '@jdeighan/coffee-utils';
|
33
37
|
|
34
38
|
import {
|
@@ -145,6 +149,45 @@ export var getParentDir = function(dir) {
|
|
145
149
|
return mkpath(resolve(dir, '..'));
|
146
150
|
};
|
147
151
|
|
152
|
+
// ---------------------------------------------------------------------------
|
153
|
+
export var forEachFile = function(dir, cb, filt = undef, level = 0) {
|
154
|
+
var ent, i, j, lSubDirectories, len, len1, ref, ref1, subdir;
|
155
|
+
// --- filt can be a regular expression or a function that gets:
|
156
|
+
// (filename, dir, level)
|
157
|
+
// callback will get parms (filename, dir, level)
|
158
|
+
lSubDirectories = [];
|
159
|
+
ref = readdirSync(dir, {
|
160
|
+
withFileTypes: true
|
161
|
+
});
|
162
|
+
for (i = 0, len = ref.length; i < len; i++) {
|
163
|
+
ent = ref[i];
|
164
|
+
if (ent.isDirectory()) {
|
165
|
+
lSubDirectories.push(ent);
|
166
|
+
} else if (ent.isFile()) {
|
167
|
+
if (filt == null) {
|
168
|
+
cb(ent.name, dir, level);
|
169
|
+
} else if (isRegExp(filt)) {
|
170
|
+
if (ent.name.match(filt)) {
|
171
|
+
cb(ent.name, dir, level);
|
172
|
+
} else if (isFunction(filt)) {
|
173
|
+
if (filt(ent.name, dir, level)) {
|
174
|
+
cb(ent.name, dir, level);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
} else {
|
178
|
+
croak("forEachFile(): bad filter", 'filter', filt);
|
179
|
+
}
|
180
|
+
}
|
181
|
+
}
|
182
|
+
if (nonEmpty(lSubDirectories)) {
|
183
|
+
ref1 = lSubDirectories.sort();
|
184
|
+
for (j = 0, len1 = ref1.length; j < len1; j++) {
|
185
|
+
subdir = ref1[j];
|
186
|
+
forEachFile(mkpath(dir, subdir.name), cb, filt, level + 1);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
};
|
190
|
+
|
148
191
|
// ---------------------------------------------------------------------------
|
149
192
|
export var pathTo = function(fname, dir, direction = "down") {
|
150
193
|
var fpath, i, len, ref, subdir;
|