-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
365 lines (297 loc) · 8.52 KB
/
index.js
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* Copyright (c) 2014, CKSource - Frederico Knabben. All rights reserved.
* Licensed under the terms of the MIT License (see LICENSE.md).
*/
'use strict';
var Minimatch = require( 'minimatch' ).Minimatch,
fs = require( 'graceful-fs' ),
path = require( 'path' );
/**
* Reads directory recursively
* @param {String|Array.<String>} path Path to a directory to be read or a pattern
* @param {Object} [options] Configuration options
* @param {String|RegExp} [options.ignore] Ignore file pattern, supports globstar matching
* @param {Boolean} [options.stats] Return file objects containing file statistics
* @param {Boolean} [options.stopOnErrors] Stop reading the directory on first error
* @param {Function} callback Function called when done or error occured
*/
function gerard( path, options, callback ) {
// invalid argument count
if ( arguments.length < 2 ) {
return callback( new Error( 'Invalid number of arguments specified.' ) );
}
// only callback function was given
if ( typeof options == 'function' ) {
callback = options;
options = {};
}
// invalid argument type
if ( typeof callback != 'function' ) {
return callback( new Error( 'Invalid argument specified.' ) );
}
// set the stopOnErrors option to default if no value specified
options.stopOnErrors = ( options.stopOnErrors === undefined ) ? true : options.stopOnErrors;
// set the recursive read flag to default (used internally)
options.recursive = ( options.recursive === undefined ) ? true : options.recursive;
// convert the ignore option to an array
options.ignore = Array.isArray( options.ignore ) ?
options.ignore :
options.ignore ? [ options.ignore ] : [];
// convert minimatch patterns to instances
options.ignore = options.ignore.map( function( ignore, i ) {
return typeof ignore == 'string' ? new Minimatch( ignore ) : ignore;
} );
path = Array.isArray( path ) ? path : [ path ];
var count = path.length,
results = [];
function decreaseCounter( err, result ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( !err ) {
results = results.concat( result );
}
count--;
if ( !count ) {
callback( null, sort( uniq( results, options.stats ), options.stats ) );
}
}
path.forEach( function( pth ) {
// the given path is a pattern
if ( isPattern( pth ) ) {
readPattern( pth, options, decreaseCounter );
} else {
readDir( pth, options, decreaseCounter );
}
} );
}
module.exports = gerard;
//
// private stuff
//
function readPattern( dir, options, callback ) {
var parts = path.normalize( dir ).split( path.sep ),
results = [],
paths = [];
function readPart() {
var part = parts.shift();
if ( !part ) {
return callback( null, sort( results, options.stats ) );
}
// cloned options for further modifications
var opt = clone( options );
opt.filter = part;
// add the CWD if the path starts with a pattern
if ( isPattern( part ) && parts.length && !paths.length ) {
paths.push( '.' );
}
// globstar
if ( part === '**' ) {
opt.filter = dir;
readAndFilterFiles( paths, opt, callback );
// pattern in the middle of the path
} else if ( parts.length && ( isPattern( part ) || paths.length ) ) {
opt.dirOnly = true;
readAndFilterPaths( paths, opt, function( err, result ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
paths = result;
readPart();
} );
// in the middle of the path
} else if ( parts.length ) {
paths.push( part );
readPart();
// at the end of the path
} else {
readAndFilterPaths( paths, opt, function( err, result ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
results = result;
readPart();
} );
}
}
readPart();
}
// read given paths and apply filters on the fly
function readAndFilterPaths( paths, options, callback ) {
var count = paths.length,
results = [],
opt = clone( options );
opt.recursive = false;
paths.forEach( function( pth ) {
readDir( pth, opt, decreaseCounter );
} );
function decreaseCounter( err, result ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( !err ) {
results = results.concat( result );
}
count--;
if ( !count ) {
callback( null, sort( results, options.stats ) );
}
}
}
// recursively read given paths and apply filtering on the final file list
function readAndFilterFiles( paths, options, callback ) {
var count = paths.length,
results = [],
opt = clone( options ),
filter = typeof options.filter == 'string' ?
new Minimatch( options.filter ) :
options.filter;
delete opt.filter;
if ( !paths.length ) {
callback( null, [] );
}
paths.forEach( function( dir ) {
readDir( dir, opt, decreaseCounter );
} );
function decreaseCounter( err, result ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( !err ) {
results = results.concat( result );
}
count--;
if ( !count ) {
if ( filter ) {
results = results.filter( function( file ) {
file = typeof file == 'string' ? file : file.path;
return filter instanceof Minimatch ? filter.match( file ) :
filter instanceof RegExp ? filter.test( file ) : true;
} );
}
callback( null, sort( results, options.stats ) );
}
}
}
// recursively read a given directory
function readDir( dir, options, callback ) {
var results = [],
count = 0,
filter = typeof options.filter == 'string' ?
new Minimatch( options.filter ) :
options.filter;
function decreaseCounter() {
count--;
if ( !count ) {
callback( null, sort( results, options.stats ) );
}
}
function isIgnored( file ) {
return options.ignore.some( function( ignore ) {
return ( ignore instanceof Minimatch && ignore.match( file ) ) ||
( ignore instanceof RegExp && ignore.test( file ) ) ||
( typeof ignore == 'function' && ignore( file ) );
} );
}
dir = typeof dir == 'object' ? dir.path : dir;
fs.readdir( dir, function( err, files ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( !files || !( count = files.length ) ) {
return callback( null, sort( results, options.stats ) );
}
files.forEach( function( file ) {
var name = file;
file = path.join( dir, file );
if ( isIgnored( file ) ||
( filter instanceof Minimatch && !filter.match( name ) ) ||
( filter instanceof RegExp && !filter.test( name ) )
) {
return decreaseCounter();
}
fs.stat( file, function( err, stats ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( err ) {
return decreaseCounter();
}
if ( stats.isDirectory() && options.recursive ) {
readDir( file, options, function( err, files ) {
if ( err && options.stopOnErrors ) {
return callback( err );
}
if ( !err ) {
results = results.concat( files );
}
decreaseCounter();
} );
} else if ( !options.dirOnly || stats.isDirectory() ) {
if ( options.stats ) {
results.push( {
name: name,
dir: dir,
path: file,
stats: stats
} );
} else {
results.push( file );
}
decreaseCounter();
} else {
decreaseCounter();
}
} );
} );
} );
}
// check if the given string is a pattern
function isPattern( str ) {
return str.indexOf( '*' ) > -1;
}
// returns a copy of the given argument
function clone( obj ) {
var copy;
if ( Array.isArray( obj ) ) {
copy = obj.map( function( value ) {
return clone( value );
} );
// don't clone instances of Minimatch - just use a reference
} else if ( typeof obj == 'object' && !( obj instanceof Minimatch ) && obj !== null ) {
copy = {};
Object.getOwnPropertyNames( obj ).forEach( function( name ) {
copy[ name ] = clone( obj[ name ] );
} );
} else {
copy = obj;
}
return copy;
}
// sort method for the results array, supports sorting the results that include stats
function sort( array, stats ) {
return stats ?
array.sort( function( a, b ) {
return a.path > b.path;
} ) :
array.sort();
}
// return a new array of unique items of a given array, supports items that include stats
function uniq( array, stats ) {
if ( stats ) {
var unique = {},
results = [];
array.forEach( function( item ) {
if ( unique[ item.path ] ) {
return;
}
unique[ item.path ] = 1;
results.push( item );
} );
return results;
} else {
return array.filter( function( item, index ) {
return array.indexOf( item ) === index;
} );
}
}