-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.js
38 lines (32 loc) · 1.06 KB
/
files.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
"use strict";
var path = require('path');
var fs = require('fs');
/**
* Find all files recursively in specific folder with specific extension, e.g:
* findFilesInDir('./project/src', '.html') ==> ['./project/src/a.html','./project/src/build/index.html']
* @param {String} startPath Path relative to this file or other file which requires this files
* @param {String} filter Extension name, e.g: '.html'
* @return {Array} Result files with path string in an array
*/
function findFilesInDir(startPath, filter){
var results = [];
var re = new RegExp(`${filter}$`, 'i');
if(!fs.existsSync(startPath)){
return;
}
var files = fs.readdirSync(startPath);
for(var i=0 ; i<files.length; i++){
var filename=path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()){
results = results.concat(findFilesInDir(filename, filter)); //recurse
}
else if (re.test(filename)) {
results.push(filename);
}
}
return results;
}
module.exports = {
findFilesInDir: findFilesInDir
};