This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
files_manager.js
74 lines (71 loc) · 2.89 KB
/
files_manager.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('fs'),
fse = require('fs-extra'),
path = require('path');
function isIncluded(exts, ext) {
if (!fs.statSync(ext).isFile())
return false;
var isValid = false;
exts.forEach((ex) => {
if (ext.substr(-(ex.length + 1)) == `.${ex}`)
isValid = true;
});
return isValid;
}
const TempFile = require('./definitions/TempFile');
module.exports = function(Console, dir, output, ext, shouldEmptyOutputDir, callback) {
var copy = function() {
fse.copy(dir, output, { overwrite: true }, function(err) {
if (err) {
Console.write("~red~Error:~~s~ Something went wrong while trying to copy the files, here are the error details:~", true);
console.error(err);
} else {
var filesList = [],
lastLength = 0,
isStillSearching = true,
waitForLoop = function() {
setTimeout(function() {
if (filesList.length == lastLength) {
if (!isStillSearching)
callback(filesList);
else
waitForLoop();
} else {
waitForLoop();
}
lastLength = filesList.length;
}, 10)
};
var checkLoop = function(dir) {
isStillSearching = true;
fs.readdir(dir, function(err, files) {
let _ = 0,
__ = 0;
files.filter(file => isIncluded(ext, path.join(dir, file))).forEach(function(file) {
filesList.push(new TempFile(path.join(dir, file).replace(/\\/g, "/")));
_++;
});
files.filter(file => fs.statSync(path.join(dir, file)).isDirectory()).forEach(function(file) {
checkLoop(path.join(dir, file));
__++;
});
if (_ == 0 || __ == 0)
isStillSearching = false;
delete _, __;
});
};
checkLoop(output);
waitForLoop();
}
});
};
if (shouldEmptyOutputDir) {
fse.emptyDir(output, function(err) {
if (err) {
Console.write("~red~Error:~~s~ Something went wrong while trying to empty the output directory, here are the error details:~", true);
console.error(err);
} else
copy();
});
} else
copy();
};