forked from fshost/node-dir
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaths.ts
341 lines (296 loc) · 8.82 KB
/
paths.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import * as fs from 'fs'
import * as path from 'path'
interface FileReadOptions {
basePath?: string
recursive?: boolean
excludeHidden?: boolean
sync?: boolean
ignoreType?: string
valuetizer?: (stat, shortName: string, longPath: string, isDir?: boolean) => any
}
export function promiseFiles(
dir,
type,
options?: FileReadOptions,
statOptions?: fs.StatSyncOptions,
){
switch(typeof type){
case 'object':
options = type
type = 'file'
break;
default:type = type || 'file'
}
var processor = function(res,rej){
var cb = function(err,data){
if(err)return rej(err)
res(data)
}
exports.files(dir, type, cb, options, statOptions)
}
return new Promise(processor)
}
/**
* find all files or subdirs (recursive) and pass to callback fn
*
* @param {string} dir directory in which to recurse files or subdirs
* @param {string} type type of dir entry to recurse ('file', 'dir', or 'all', defaults to 'file')
* @param {function(error, <Array.<string>)} callback fn to call when done
* @example
* dir.files(__dirname, function(err, files) {
* if (err) throw err;
* console.log('files:', files);
* });
*/
export function files(
dir, type, callback,
options?: FileReadOptions,
statOptions?: fs.StatSyncOptions,
) {
var ofType = typeof type
if(ofType == 'object'){
options = options || type
type = 'file'
callback = function(){}
}else if (ofType !== 'string') {
//ignoreType = callback;
callback = type;
type = 'file';
}
options = options || {} as FileReadOptions
var pending
var results = {
files: [],
dirs: []
};
var done = function(_list?: any) {
if(type==='combine'){
results = results.files.concat(results.dirs) as any
} else if (!type || options.ignoreType || ['all','combine'].indexOf(type)>=0) {
results = results
} else {
results = results[type + 's']
}
if(options.sync)return;
callback(null, results);
};
/**
@statPath - fullPath
@name - fileName
@statHanOptions - {
valuetizer:function(stat, shortName, longPath){}, - function the handles value assignment
lstatCalled:false - used internally
}
*/
var getStatHandler = function(
statPath,
name,
statHanOptions,
) {
return function(err, stat) {
if (err) {
if (!statHanOptions.lstatCalled) {
var newStatHanOptions = assign(statHanOptions, {lstatCalled:true})
if(options.sync){
var lstat = fs.lstatSync(statPath);
return getStatHandler(statPath, name, newStatHanOptions)(null, lstat)
}else{
return fs.lstat(statPath, getStatHandler(statPath, name, newStatHanOptions));
}
}
return callback(err);
}
var isDir = stat && stat.isDirectory() && stat.mode !== 17115
var pushVal = statHanOptions.valuetizer(stat, name, statPath, isDir)//options.shortName ? name : statPath
if( pushVal==null ){
if (!--pending){
done();
}
return
}
if (isDir) {
if (type !== 'file') {
results.dirs.push(pushVal);
}
if (options.recursive==null || options.recursive) {
var subloop = function(err, res) {
if (err){
return callback(err)
}
if(type === 'combine'){
results.files = results.files.concat(res);
}else if (type === 'all') {
if( res.files ){//dont add things that are not there
results.files = results.files.concat(res.files);
}
results.dirs = results.dirs.concat(res.dirs);
} else if (type === 'file') {
if( res.files ){//dont add things that are not there
results.files = results.files.concat(res.files);
}
} else {
results.dirs = results.dirs.concat(res.dirs);
}
if (!--pending){
done();
}
}
var newOptions = assign({}, options)
newOptions.basePath = options.basePath || dir
newOptions.ignoreType = true
var moreResults = files(statPath, type, subloop, newOptions);
if(options.sync){
subloop(null, moreResults)
}
}else if (!--pending){
done()
}
} else {
var excludeHidden = options.excludeHidden && name.split(path.sep).pop().search(/^\./)==0
if (type!=='dir' && !excludeHidden) {
results.files.push(pushVal);
}
// should be the last statement in statHandler
if (!--pending){
done()
}
}
}
}
const onDirRead = function(
err,
list,
statOptions?: fs.StatSyncOptions
) {
if (err) return callback(err);
pending = list.length;
if (!pending){
done(list);
return list
}
var statHanOptions: FileReadOptions = {}
if( options.valuetizer ){
statHanOptions.valuetizer = options.valuetizer
}else{
statHanOptions.valuetizer = getValuetizerByOptions(options, dir)
}
for (var file, i = 0, l = list.length; i < l; i++) {
var fname = list[i].toString();
file = path.join(dir, fname);
//var buffile = Buffer.concat([bufdir, Buffer.from(path.sep), list[i]]);
if(options.sync){
var res = fs.statSync(file, statOptions);
getStatHandler(file, list[i], statHanOptions)(null, res)
}else{
fs.stat(file, getStatHandler(file, list[i], statHanOptions));
}
}
return results
}
const onStat = function(
err,
stat,
statOptions?: fs.StatSyncOptions
) {
if (err) return callback(err);
if (stat && stat.mode === 17115) return done();
if(options.sync){
const list = fs.readdirSync(dir)
return onDirRead(null, list, statOptions)
}else{
fs.readdir(dir,
(err: NodeJS.ErrnoException, files: string[]) => onDirRead(err, files, statOptions)
)
}
}
if(options.sync){
const stat = fs.statSync(dir);
return onStat(null, stat, statOptions)
}else{
fs.stat(dir, onStat);
}
};
/**
* find all files and subdirs in a directory (recursive) and pass them to callback fn
*
* @param {string} dir directory in which to recurse files or subdirs
* @param {boolean} combine whether to combine both subdirs and filepaths into one array (default false)
* @param {function(error, Object.<<Array.<string>, Array.<string>>)} callback fn to call when done
* @example
* dir.paths(__dirname, function (err, paths) {
* if (err) throw err;
* console.log('files:', paths.files);
* console.log('subdirs:', paths.dirs);
* });
* dir.paths(__dirname, true, function (err, paths) {
* if (err) throw err;
* console.log('paths:', paths);
* });
*/
exports.paths = function paths(dir, combine, callback) {
if (typeof combine === 'function') {
callback = combine;
combine = false;
}
files(dir, 'all', function(err, results) {
if (err) return callback(err);
if (combine) {
callback(null, results.files.concat(results.dirs));
} else {
callback(null, results);
}
});
};
/**
* find all subdirs (recursive) of a directory and pass them to callback fn
*
* @param {string} dir directory in which to find subdirs
* @param {string} type type of dir entry to recurse ('file' or 'dir', defaults to 'file')
* @param {function(error, <Array.<string>)} callback fn to call when done
* @example
* dir.subdirs(__dirname, function (err, paths) {
* if (err) throw err;
* console.log('files:', paths.files);
* console.log('subdirs:', paths.dirs);
* });
*/
exports.subdirs = function subdirs(dir, callback, type, options) {
options = options || {}
const iCallback = function(err, subdirs) {
if (err) return callback(err);
if(type=='combine'){
subdirs = subdirs.files.concat(subdirs.dirs)
}
if(options.sync)return subdirs
callback(null, subdirs);
}
const res = exports.files(dir, 'dir', iCallback, options)
if(options && options.sync){
return iCallback(null,res)
}
}
function assign(c0, c1){
for(var x in c1)c0[x] = c1[x]
return c0
}
function getValuetizerByOptions(options, dir){
if(options.shortName){
if( options.shortName=='relative' ){
var dirBase = (options.basePath||dir)
var startPos = dirBase.length
if(dirBase.substring(dirBase.length-path.sep.length, dirBase.length)!=path.sep){
startPos = startPos + path.sep.length
}
return function(stat, shortName, longPath, isDir){
return longPath.substring(startPos, longPath.length)
}
}else{
return function(stat, shortName, longPath){
return shortName
}
}
}
return function(stat, shortName, longPath){
return longPath
}
}