-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
76 lines (68 loc) · 1.73 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
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
const fs = require("fs");
const chalk = require("chalk");
const timeout = (time) => new Promise((res) => setTimeout(res, time));
const updatePauseTime = (time, current, total) => {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(`… pausing for ${time}ms (${(current / total) * 100}%)`);
};
const pause = (callback, time) => {
const fragmentCount = 10;
const timeFragment = time / fragmentCount;
let current = 0;
updatePauseTime(time, current, fragmentCount);
const interval = setInterval(() => {
current += 1;
updatePauseTime(time, current, fragmentCount);
if (current >= fragmentCount) {
clearInterval(interval);
process.stdout.write("\n");
callback();
}
}, timeFragment);
};
createFolder = (filePath) => {
return new Promise((res, rej) => {
fs.mkdir(filePath, { recursive: true }, (err) => {
if (err) return rej(err);
else res();
});
});
};
createJSONFile = (filePath, data = {}) => {
return new Promise((res, rej) => {
fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
if (err) return rej(err);
else res();
});
});
};
const readFile = (path, callback) => {
fs.readFile(path, "utf-8", (err, data) => {
if (err) throw err;
callback(JSON.parse(data));
});
};
class Log {
pending(copy) {
console.log(chalk.yellow("…"), copy);
}
success(copy) {
console.log(chalk.green("✔"), copy);
}
failure(copy) {
console.log(chalk.red("𐄂"), copy);
}
title(copy) {
console.log(chalk.black.bgWhite(copy));
}
timestamp() {
console.log(chalk.gray(`⏰ ${new Date().toLocaleTimeString()}`));
}
}
module.exports = {
pause,
createJSONFile,
Log: new Log(),
readFile,
};