1 | 1 | var fs = require('fs'); |
|
2 | 1 | var path = require('path'); |
|
3 | 1 | var glob = require('glob'); |
|
4 | 1 | var globAll = require('glob-all'); |
|
5 | |||
6 | /** |
||
7 | * recursivly read a list of files from a given directory |
||
8 | * according to a given file-path pattern |
||
9 | * |
||
10 | * scans relative to project root |
||
11 | * |
||
12 | * @param string|[string] dir |
||
13 | * @param string pattern |
||
14 | * @return [string] |
||
15 | */ |
||
16 | function _readdir ( dir, pattern ) { |
||
17 | |||
18 | 132 | var files = []; |
|
19 | |||
20 | 132 | dir = !Array.isArray(dir) ? [dir] : dir; |
|
21 | |||
22 | 132 | for (var i in dir) { |
|
0 ignored issues
–
show
|
|||
23 | 132 | if ( fs.existsSync( dir[i] ) ) { |
|
24 | 27 | files = files.concat( |
|
25 | globAll.sync( path.join( dir[i], pattern ) ) |
||
26 | ); |
||
27 | } else { |
||
28 | 105 | files = files.concat( |
|
29 | globAll.sync( path.join( process.cwd(), dir[i], pattern ) ) |
||
30 | ); |
||
31 | } |
||
32 | } |
||
33 | |||
34 | 132 | return files; |
|
35 | |||
36 | } |
||
37 | |||
38 | module.exports = _readdir; |
||
39 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: