-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetfiles.js
47 lines (43 loc) · 1.4 KB
/
getfiles.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
/* This file will scan the file system and retreive the hierarchy of the file subset and associated files.
It will also get details of if the it is a file or directory as well as the file or directory size for later use. */
const testFolder = './';
const fs = require('fs');
const path = require('path');
const getFiles = (workingDir) => {
return new Promise((resolve, reject) => {
const walk = (dir, done) => {
var results = [];
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
list.forEach(function(file) {
file = path.resolve(dir, file);
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
results.push(file);
if (!--pending) done(null, results);
}
});
});
});
resolve(results);
};
});
}
// Write to JSON file
getFiles(testFolder)
.then((results) => {
console.log(results);
fs.writeFile("src/sources/data.json", JSON.stringify(results), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved to disc");
});
});