-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
205 lines (173 loc) · 5.33 KB
/
helpers.js
File metadata and controls
205 lines (173 loc) · 5.33 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const fs = require('fs'),
colors = require('colors/safe'),
ReadLine = require('readline'),
GithubContent = require('github-content');
const pJson = require('./package.json');
let GitCUpdate = new GithubContent({
owner: 'cursedseal',
repo: 'vcoinx',
branch: 'master'
});
let checkUpdateTTL = null,
onUpdatesCB = false,
offColors = false;
function formateSCORE(e) {
return (arguments.length > 1 && void 0 !== arguments[1] && arguments[1]) ?
function(e, t, n, a) {
var r, o, c, s, i;
r = parseInt(e = (+e || 0).toFixed(t), 10) + "";
(o = r.length) > 3 ? o %= 3 : o = 0;
i = o ? (r.substr(0, o) + a) : "";
c = r.substr(o).replace(/(\d{3})(?=\d)/g, "$1" + a);
s = t ? n + Math.abs(e - r).toFixed(t).replace(/-/, 0).slice(2) : "";
return i + c + s;
}(e / 1e3, 3, ",", " ") :
(e / 1e3).toFixed(3).toString().replace(".", ",")
}
colors.setTheme({
dateBG: 'white',
dataC: 'yellow',
warnBG: 'bgBlack',
warn: 'yellow',
errorBG: 'bgBlack',
error: 'red'
});
function con(message, color, colorBG) {
if (message === undefined) {
console.log("\n")
return;
}
let temp = (!offColors ? colors.dateBG('[' + dateF() + ']') : dateF()) + ": " + ccon(message, color, colorBG, 1);
console.log(temp);
}
function ccon(message, color, colorBG, ret) {
let temp = "";
if (message === undefined) {
console.log("\n")
return;
}
if (color === true) {
color = "white";
colorBG = "Red";
temp = !offColors ? colors.yellow.bgRed("[ОШИБКА]: ") : "[ОШИБКА]: ";
}
colorBG = "bg" + ((typeof colorBG == "string") ? colorBG : "Black");
color = (typeof color == "string") ? color : "green";
temp += !offColors ? colors[colorBG](colors[color](message)) : message;
!ret && console.log(temp);
return temp;
}
function setColorsM(e) {
offColors = !!e;
}
function dateF(date) {
if (!isNaN(date) && date < 9900000000)
date *= 1000;
date = date !== undefined ? new Date(date) : new Date();
var dYear = date.getFullYear(),
dMonthF = (date.getMonth() + 1),
dMonth = dMonthF > 9 ? dMonthF : "0" + dMonthF,
dDay = date.getDate() > 9 ? date.getDate() : "0" + date.getDate(),
dHour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours(),
dMinutes = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes(),
dSeconds = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds(),
date_format = dDay + '.' + dMonth + '.' + dYear + ' ' + dHour + ':' + dMinutes + ':' + dSeconds;
return date_format;
}
let rl = ReadLine.createInterface(process.stdin, process.stdout);
rl.setPrompt('> ');
rl.prompt();
rl.isQst = false;
rl.questionAsync = (question) => {
return new Promise((resolve) => {
rl.isQst = true;
rl.question(question, _ => {
rl.isQst = false;
resolve(_);
});
});
};
function hashPassCoin(e, t) {
return (e % 2 === 0) ?
(e + t - 15) :
(e + t - 109);
}
function checkUpdates() {
GitCUpdate.files(['package.json'], (err, results) => {
if (err) return;
results.forEach(file => {
let c = file.contents.toString();
if (c[0] === "{") {
let data = JSON.parse(c);
let msg = (data.version > pJson.version) ? "Было выпущено новое обновление! -> github.com/cursedseal/VCoinX \t[" + (data.version + "/" + pJson.version) + "]" :
false;
if (msg) {
if (onUpdatesCB) onUpdatesCB(msg);
else con(msg, "white", "Red");
}
}
});
});
}
checkUpdateTTL = setInterval(checkUpdates, 1e7);
checkUpdates();
function rand(min, max) {
if (max === undefined)
max = min;
min = 0;
return Math.floor(min + Math.random() * (max + 1 - min));
}
let cFile = "./log.txt";
async function infLog(data) {
data = "\n[" + dateF() + "] \t" + data;
let exists = await existsAsync(cFile);
if (!exists) {
let errWrite = await writeFileAsync(cFile, "Log." + data);
if (errWrite) throw errWrite;
} else
await appendFileAsync(cFile, data);
}
function existsFile(f) {
return fs.existsSync(f);
}
function existsAsync(path) {
return new Promise((resolve, reject) => fs.exists(path, exists => resolve(exists)));
}
function writeFileAsync(path, data) {
return new Promise((resolve, reject) => fs.writeFile(path, data, err => resolve(err)));
}
function appendFileAsync(path, data) {
return new Promise((resolve, reject) => fs.appendFile(path, data, err => resolve(err)));
}
function getVersion() {
return pJson.version;
}
function setTerminalTitle(title) {
process.stdout.write(
String.fromCharCode(27) + "]0;" + title + String.fromCharCode(7)
);
}
function beep() {
process.stdout.write('\x07');
}
module.exports = {
rl,
con,
ccon,
setColorsM,
offColors,
formateSCORE,
hashPassCoin,
checkUpdates,
checkUpdateTTL,
onUpdates: cb => (onUpdatesCB = cb, true),
existsFile,
existsAsync,
writeFileAsync,
appendFileAsync,
getVersion,
setTerminalTitle,
infLog,
rand,
beep,
}