-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreadFile.js
executable file
·54 lines (47 loc) · 1.46 KB
/
readFile.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
var fs = require('fs');
var readdir = promisify(fs.readdir);
var stat = promisify(fs.stat);
var readFile = promisify(fs.readFile);
var path=require('path')
// 简单实现一个promisify
function promisify(fn) {
return function() {
var args = arguments;
return new Promise(function(resolve, reject) {
[].push.call(args, function(err, result) {
if(err) {
console.log(err)
reject(err);
}else {
resolve(result);
}
});
fn.apply(null, args);
});
}
}
function readDirRecur(file, callback) {
return readdir(file).then((files) => {
files = files.map((item) => {
var fullPath = file + '/' + item;
return stat(fullPath).then((stats) => {
if (stats.isDirectory()) {
return readDirRecur(fullPath, callback);
} else {
/*not use ignore files*/
if(item[0] == '.'){
//console.log(item + ' is a hide file.');
} else {
callback && callback(fullPath)
}
}
})
});
return Promise.all(files);
});
}
var fileList = []
var filePath = path.resolve('static/sx')
module.exports = {
readDirRecur: readDirRecur
};