-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
82 lines (69 loc) · 2.12 KB
/
helper.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
75
76
77
78
79
80
81
82
const fs = require('fs');
const os = require('os');
const path = require('path');
const https = require('https');
const clc = require("cli-color");
let logStream = null;
/**
* Find all files inside a dir, recursively.
* @function getAllFiles
* @param {string} dir Dir path string.
* @return {string[]} Array with all file names that are inside the directory.
*/
const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
}, []);
const getAllFolders = dir => {
let folders = [];
fs.readdirSync(dir).forEach((file) => {
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
if (!isDirectory) {
return;
}
folders.push(name, ...getAllFolders(name));
});
return folders;
}
const downloadFile = (url, destination) => {
return new Promise((resolve, reject) => {
var file = fs.createWriteStream(destination);
var request = https.get(url, (res) => {
res.pipe(file);
res.on('end', resolve);
});
});
};
const removeInvalidPathCharacters = (str) => {
return str.replace(/[<>:"/\\|?*]/ig, '').trim();
}
const getLogPath = () => {
return path.join(os.tmpdir() , "media_identifier_output.log");
}
const log = (color, message) => {
if (logStream === null) {
logStream = fs.createWriteStream(getLogPath(), {flags: 'w'});
}
let d = new Date();
message = "[" + d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "] " + message;
if (color === "blue") { // i cant see the f** blue in black screens
color = "cyan";
}
if (color !== "normal") {
message = clc[color](message);
}
console.log(message);
logStream.write(message + os.EOL);
// stream.end();
}
module.exports = {
log,
getLogPath,
getAllFiles,
getAllFolders,
removeInvalidPathCharacters,
downloadFile
}