-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
44 lines (39 loc) · 1.15 KB
/
utils.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
const path = require('path');
const fs = require('fs');
const { MOCK_FILE_PATH } = require('./constants/config');
const promisifyReadDir = (pathValue) => new Promise((resolve, reject) => {
fs.readdir(path.join('./', pathValue), (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
const promisifyReadFile = (filePath) => new Promise((resolve, reject) => {
fs.readFile(path.join('./', filePath), 'utf-8', (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
const getFilePathRecursively = async (directory) => {
const filesInDirectory = await promisifyReadDir(directory);
const promisifiedPathArray = filesInDirectory.map(async (file) => {
const absolute = path.join(directory, file);
if (fs.statSync(path.join('./', absolute)).isDirectory()) {
const dirList = await getFilePathRecursively(absolute);
return dirList;
}
return absolute.replace(MOCK_FILE_PATH, '');
});
const list = await Promise.all(promisifiedPathArray);
return list.flat();
};
module.exports = {
promisifyReadDir,
promisifyReadFile,
getFilePathRecursively,
};