forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
49 lines (45 loc) · 1.07 KB
/
utility.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
const fs = require('fs');
const path = require('path');
/**
* get an array of the files under the give path
*/
function iterateFiles(source) {
let fileList = [];
return new Promise((res, rej) => {
fs.readdir(source, function(err, files) {
if (err) {
console.error('Could not list the directory.', err);
return rej(err);
}
Promise.all(
files.map(file => {
const filePath = path.join(source, file);
return new Promise((res, rej) => {
fs.stat(filePath, (error, stat) => {
if (error) {
console.error('Error stating file.', error);
return rej(error);
}
if (stat.isFile()) {
fileList.push(filePath);
return res();
} else if (stat.isDirectory()) {
iterateFiles(filePath).then(list => {
fileList = fileList.concat(list);
return res();
});
} else {
return res();
}
});
});
})
).then(() => {
return res(fileList);
});
});
});
}
const utility = {};
utility.iterateFiles = iterateFiles;
module.exports = utility;