-
Notifications
You must be signed in to change notification settings - Fork 1
/
install.js
270 lines (230 loc) · 8.58 KB
/
install.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'use strict';
const fs = require('fs');
const path = require('path');
const https = require('https');
/**
* Source: https://medium.com/@bargord11/write-your-first-node-js-terminal-progress-bar-5bd5edb8a563
*/
class ProgressBar {
constructor() {
this.activate = typeof process.stdout.clearLine == "function";
this.total;
this.current;
this.bar_length = process.stdout.columns - 30;
}
init(total) {
this.total = total;
this.current = 0;
this.update(this.current);
}
update(current) {
if (!this.activate) return;
this.current = current;
const current_progress = this.current / this.total;
this.draw(current_progress);
}
draw(current_progress) {
if (!this.activate) return;
const filled_bar_length = (current_progress * this.bar_length).toFixed(0);
const empty_bar_length = this.bar_length - filled_bar_length;
const filled_bar = this.get_bar(filled_bar_length, "█", a => `\x1b[37m${a}\x1b[0m`);
const empty_bar = this.get_bar(empty_bar_length, "░", a => `\x1b[37m${a}\x1b[0m`);
const percentage_progress = (current_progress * 100).toFixed(2);
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(
`\x1b[35mProgress:\x1b[0m |${filled_bar}${empty_bar}| ${percentage_progress}%`
);
}
get_bar(length, char, color = a => a) {
let str = "";
for (let i = 0; i < length; i++) {
str += char;
}
return color(str);
}
};
/**
* Download a file
*
* @param {string} url the url of the file to download
* @param {string} filePath the output path of the file
* @param {fs.WriteStream} file the file stream
*/
async function download(url, filePath, file = null) {
return new Promise((resolve, reject) => {
let fileInfo = null, closeFile = true;
if (file == null) file = fs.createWriteStream(filePath);
else closeFile = false;
const request = https.get(url, response => {
if (response.statusCode >= 300 && response.statusCode < 400 && response.headers.location != undefined) {
if (response.headers.location.startsWith("https://")) {
url = response.headers.location;
} else {
const addr_regex = /^https:\/\/(www\.)?[a-zA-Z0-9\.]+\.[a-z]{1,5}/;
url = url.match(addr_regex)[0] + response.headers.location;
}
console.log(`\x1b[90mRedirecting to: ${url}\x1b[0m`);
return download(url, filePath, file).then(resolve, reject);
} else if (response.statusCode !== 200) {
reject(new Error(`Failed to get '${url}' (${response.statusCode})`));
return;
}
fileInfo = {
mime: response.headers['content-type'],
size: parseInt(response.headers['content-length'], 10),
};
const bar = new ProgressBar();
bar.init(fileInfo.size);
let current = 0;
response.on('data', chunk => {
current += chunk.length;
bar.update(current);
file.write(chunk);
if (current >= fileInfo.size) {
console.log();
file.end();
}
});
});
if (closeFile) {
// The destination stream is ended by the time it's called
file.on('finish', () => {
resolve(fileInfo);
});
file.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
}
request.on('error', err => {
fs.unlink(filePath, () => reject(err));
});
request.end();
});
}
// Source: https://stackoverflow.com/a/32197381
const deleteFolderRecursive = function (p) {
if (fs.existsSync(p)) {
fs.readdirSync(p).forEach((file) => {
const curPath = path.join(p, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(p);
}
};
function deleteIfExists(p) {
if (fs.existsSync(p)) {
if (fs.lstatSync(p).isDirectory()) {
console.log(`Directory ${p} exists, deleting it`);
deleteFolderRecursive(p);
} else {
console.log(`${p} exists, deleting it`);
fs.unlinkSync(p);
}
}
}
function cleanUp() {
const dist_dir = path.join(__dirname, "dist");
const bin_dir = path.join(__dirname, "autobetlib", "bin");
const out_dir = path.join(__dirname, 'out');
deleteIfExists(dist_dir);
deleteIfExists(bin_dir);
deleteIfExists(out_dir);
}
function full_clean() {
cleanUp();
const model_yml = path.join(__dirname, "resources", "data", "model.yml");
const node_modules = path.join(__dirname, "node_modules");
const lib_node_modules = path.join(__dirname, "autobetlib", "node_modules");
const autobet_conf = path.join(__dirname, "autobet.conf");
const autobet_log = path.join(__dirname, "autobet.log");
const autobet_debug_log = path.join(__dirname, "autobet_debug.log");
const winnings_dat = path.join(__dirname, "winnings.dat");
const conanbuildinfo_cmake = path.join(__dirname, "autobetlib", "conanbuildinfo.cmake");
const conanbuildinfo_txt = path.join(__dirname, "autobetlib", "conanbuildinfo.txt");
const conaninfo_txt = path.join(__dirname, "autobetlib", "conaninfo.txt");
const graph_info_json = path.join(__dirname, "autobetlib", "graph_info.json");
deleteIfExists(conanbuildinfo_cmake);
deleteIfExists(conanbuildinfo_txt);
deleteIfExists(conaninfo_txt);
deleteIfExists(graph_info_json);
deleteIfExists(model_yml);
deleteIfExists(autobet_conf);
deleteIfExists(autobet_log);
deleteIfExists(autobet_debug_log);
deleteIfExists(winnings_dat);
deleteIfExists(node_modules);
deleteIfExists(lib_node_modules);
}
async function downloadModel() {
const dl_addr = "https://www.dropbox.com/s/6g0gdas5oall71w/model.yml?dl=1";
const modelPath = path.join(__dirname, 'resources', 'data', 'model.yml');
if (fs.existsSync(modelPath)) {
console.log("The model file already exists, deleting it");
fs.unlinkSync(modelPath);
console.log(modelPath + " successfully deleted");
}
console.log("Downloading model.yml");
await download(dl_addr, modelPath);
console.log("\x1b[92mSuccessfully downloaded the model\x1b[0m");
}
function postbuild() {
// Source: https://stackoverflow.com/a/26038979
function copyFileSync(source, target) {
var targetFile = target;
// If target is a directory, a new file with the same name will be created
if (fs.existsSync(target)) {
if (fs.lstatSync(target).isDirectory()) {
targetFile = path.join(target, path.basename(source));
}
}
fs.writeFileSync(targetFile, fs.readFileSync(source));
}
function copyFolderRecursiveSync(source, target) {
var files = [];
// Check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(source));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
// Copy
if (fs.lstatSync(source).isDirectory()) {
files = fs.readdirSync(source);
files.forEach(function (file) {
var curSource = path.join(source, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursiveSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
}
const qrcode_src = path.join(__dirname, 'src', 'main', 'qrcode');
const dest = path.join(__dirname, 'out', 'main');
copyFolderRecursiveSync(qrcode_src, dest);
}
if (process.argv.length !== 3) {
throw new Error("install.js called with invalid amount of arguments");
} else {
switch (process.argv[2]) {
case "--downloadModel":
downloadModel();
break;
case "--postbuild":
postbuild();
break;
case "--clean":
cleanUp();
break;
case "--clean_all":
full_clean();
break;
default:
throw new Error(`Unknown argument: '${process.argv[2]}'`);
}
}