-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.js
319 lines (278 loc) · 11 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// ------------------------------------
// REQUIRES GIT TO BE LOCALLY INSTALLED
// ------------------------------------
// Supported argument:
// selection -> bypass menu
// Check node version
const nodeversion = process.versions.node;
const minversion = 16;
if (nodeversion.split('.')[0] < minversion) {
console.log();
console.log('-----------------------------------------------------------------------------------');
console.log(`The installed Node.js version is ${nodeversion}, but version ${minversion} or above is required`)
console.log(`Please install a recent version of Node.js and then start the installer again`)
console.log('-----------------------------------------------------------------------------------');
console.log();
process.exit(1);
}
const spawn = require('child_process').spawn;
const execSync = require('child_process').execSync;
const fse = require('fs-extra');
const prompt = require('prompt-sync')();
const installpath = './bb/';
const installerpath = './installer/';
const basepath = './';
const bbfile = 'bb.js';
const installfile = 'install.js';
const packageFileInstaller = 'package-installer.json';
const packageFile = 'package.json';
const installerVersion = require(basepath + packageFileInstaller).version;
const existsInstallation = fse.pathExistsSync(installpath);
const existsConfiguration = fse.pathExistsSync(installpath + 'config/');
let full = false;
let staging = false;
let repository;
let message = 'Enter selection: ';
let selection = process.argv[2] || null;
start();
function start() {
if (!selection) {
console.clear();
console.log('');
console.log('---------------------------------------------');
console.log('');
console.log(' Balance Bot Installer');
console.log(` v${installerVersion}`);
console.log('');
console.log('---------------------------------------------');
console.log('');
console.log(' 1 - Install Balance Bot');
console.log(' 2 - Update Balance Bot');
console.log(' 3 - Re-install Balance Bot');
console.log('');
console.log(' u - Update this installer');
console.log('');
console.log(' 0 - Exit');
console.log('');
console.log('---------------------------------------------');
console.log('');
selection = prompt(message);
console.log('');
}
switch (selection) {
case '1':
install();
break;
case '2':
install();
break;
case '3':
full = true;
install();
break;
case '1s':
staging = true;
install();
break;
case '2s':
staging = true;
install();
break;
case '3s':
full = true;
staging = true;
install();
break;
case 'u':
full = true;
update();
break;
case '0':
break;
default:
message = 'Invalid selection, try again: ';
start();
break;
}
}
function install() {
repository = staging ? "https://github.com/hodlerhacks/balance-bot-v2-staging.git" : "https://github.com/hodlerhacks/balance-bot-v2.git";
console.log('---------------------------------------------');
console.log('Downloading Balance Bot...');
if (full && existsConfiguration) {
try {
if (fse.existsSync(installpath + 'config/')) fse.copySync(installpath + 'config/', basepath + 'temp/config/', { overwrite: true, preserveTimestamps: true });
if (fse.existsSync(installpath + 'logs/')) fse.copySync(installpath + 'logs/', basepath + 'temp/logs/', { overwrite: true, preserveTimestamps: true });
} catch (err) {
console.log('Error occured while backing up config and log files: ', err);
return process.exit(1);
}
}
if (full) {
try {
fse.removeSync(installpath);
} catch (err) {
if (err) {
console.log('Error occured while cleaning up install folder: ', err);
return process.exit(1);
}
}
}
download(installpath, (err) => {
if (err) {
console.log('Error occured while downloading Balance Bot: ', err);
return process.exit(1);
} else {
console.log('Balance Bot downloaded');
console.log('---------------------------------------------');
console.log('Installing Packages...');
try {
if (fse.pathExistsSync(installpath + packageFileInstaller)) fse.moveSync(installpath + packageFileInstaller, basepath + packageFileInstaller, { overwrite: true });
const installerDependencies = require(basepath + packageFileInstaller).dependencies;
const bbDependencies = require(installpath + packageFile).dependencies;
const mergedDependencies = merge(installerDependencies, bbDependencies);
const pckgFile = require(installpath + packageFile);
pckgFile.dependencies = mergedDependencies;
fse.writeJsonSync(basepath + packageFile, pckgFile);
execSync('npm install', { stdio: 'ignore', cwd: basepath });
} catch (err) {
console.log('Error occured during package installation: ', err);
return process.exit(1);
}
console.log('Packages installed');
console.log('---------------------------------------------');
console.log('Installing Balance Bot...');
try {
if (fse.pathExistsSync(installpath + bbfile)) fse.moveSync(installpath + bbfile, basepath + bbfile, { overwrite: true });
if (fse.pathExistsSync(installpath + installfile)) fse.moveSync(installpath + installfile, basepath + installfile, { overwrite: true });
} catch (err) {
console.log('Error occured during Balance Bot installation: ', err);
return process.exit(1);
}
if (full && existsConfiguration) {
try {
if (fse.existsSync(basepath + 'temp/config/')) fse.copySync(basepath + 'temp/config/', installpath + 'config/', { overwrite: true, preserveTimestamps: true });
if (fse.existsSync(basepath + 'temp/logs/')) fse.copySync(basepath + 'temp/logs/', installpath + 'logs/', { overwrite: true, preserveTimestamps: true });
} catch (err) {
console.log('Error occured while restoring config and log files: ', err);
return process.exit(1);
}
try {
fse.removeSync(basepath + 'temp/');
} catch (err) {
if (err) {
console.log('Error occured while cleaning up temp folder: ', err);
return process.exit(1);
}
}
}
const pckg = require(installpath + '/package.json');
console.log(`Balance Bot version ${pckg.version} installed`);
console.log('---------------------------------------------');
if (pckg.description != '') {
console.log(pckg.description);
console.log('---------------------------------------------');
}
console.log('');
}
});
}
async function update() {
repository = "https://github.com/hodlerhacks/balance.git";
console.log('---------------------------------------------');
console.log('Downloading Balance Bot Installer...');
download(installerpath, (err) => {
if (err) {
console.log('Error occured while downloading Balance Bot Installer: ', err);
return process.exit(1);
} else {
console.log('Balance Bot Installer downloaded');
console.log('---------------------------------------------');
console.log('Installing Balance Bot Installer...');
try {
if (existsInstallation) {
const installerDependencies = require(installerpath + packageFileInstaller).dependencies;
const bbDependencies = require(installpath + packageFile).dependencies;
const mergedDependencies = merge(installerDependencies, bbDependencies);
const pckgFile = require(installpath + packageFile);
pckgFile.dependencies = mergedDependencies;
fse.writeJsonSync(installerpath + packageFile, pckgFile);
}
fse.copySync(installerpath, basepath, { overwrite: true });
fse.removeSync(installerpath);
} catch (err) {
console.log('Error occured during package installation: ', err);
return process.exit(1);
}
console.log('Installer installed');
console.log('---------------------------------------------');
console.log('Installing packages...');
execSync('npm install', { stdio: 'ignore', cwd: basepath });
console.log(`Installation completed`);
console.log('---------------------------------------------');
console.log('');
}
});
}
function merge(dep1, dep2) {
let merged = dep1;
const deps = Object.entries(dep2);
for (const dep of deps) {
const key = dep[0];
const value = dep[1];
if (!dep1.hasOwnProperty(key)) {
merged = {
...merged,
[key]: value,
}
}
}
const sorted = Object.keys(merged).sort().reduce(
(obj, key) => {
obj[key] = merged[key];
return obj;
},
{}
);
return sorted;
}
function download(path, cb) {
if (!existsInstallation || full) gitClone(path, (err) => {
if (err) {
console.log('Error occured during git clone: ', err);
return process.exit(1);
}
else cb(null);
});
else gitPull(path, (err) => {
if (err) {
console.log('Error occured during git pull: ', err);
return process.exit(1);
}
else cb(null);
});
}
function gitClone(path, cb) {
const args = ['clone', repository, path];
spawncb('git', args, {}, function (err) {
if (err) cb(err);
else cb(null);
});
}
function gitPull(path, cb) {
const args = ['pull', '--ff-only'];
spawncb('git', args, { cwd: path }, function (err) {
if (err) cb(err);
else cb(null);
});
}
function spawncb(command, args, opts, cb) {
opts.stdio = 'inherit';
const child = spawn(command, args, opts);
child.on('error', cb);
child.on('close', function (code) {
if (code !== 0) return cb(new Error('exit code ' + code));
cb(null);
})
return child;
}