-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
98 lines (88 loc) · 1.99 KB
/
util.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const readline = require("readline");
const fs = require('fs');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/**
*
* @param {string} msg
* @returns {string}
*/
async function input(msg) {
return new Promise((resolve, reject) => {
rl.question(msg, function(answer) {
resolve(answer)
})
})
}
/**
*
* @param {string} msg Message to print
* @param {boolean} flag false if printing own message otherwise true
*/
function renderMessage(msg, flag = true) {
if (flag == false) {
process.stdout.moveCursor(0, -1);
}
process.stdout.cursorTo(0);
const dy = Math.ceil(msg.length / process.stdout.columns);
process.stdout.moveCursor(0, -1 * dy);
process.stdout.write(msg);
process.stdout.clearScreenDown();
process.stdout.moveCursor(0, 1);
process.stdout.cursorTo(0);
// process.stdout.clearLine(1);
process.stdout.write("\n");
// process.stdout.moveCursor(0,1);
if (flag) {
process.stdout.write("> ");
}
}
function pad(text, len, char='0') {
const s = Math.max(len - text.length, 0);
return char.repeat(s) + text;
}
function debug(msg, cb=console.log, ...args) {
if(process.env.DEBUG) {
cb(msg, ...args)
}
}
const config = {};
function init() {
if(fs.existsSync('config.json')) {
const content = fs.readFileSync('config.json');
const data = JSON.parse(content);
for(const key in data) {
config[key] = data[key];
}
debug(config);
}
else {
fs.writeFileSync('config.json', Buffer.from("{}"));
}
}
function setConfig(key, value) {
config[key] = value;
fs.writeFileSync(
'config.json',
Buffer.from(JSON.stringify(config))
);
}
function getConfig(key) {
if(key) {
return config[key];
}
else {
return config;
}
}
module.exports = {
input,
renderMessage,
pad,
debug,
init,
setConfig,
getConfig
}