This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
97 lines (85 loc) · 4.38 KB
/
init.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
const logger = require('./logger');
const Table = require('cli-table3');
const { exec } = require('child_process');
const { config } = require('../config.js');
const init = async () => {
logger.info('Set the environment variables.');
switch (config.source.provider) {
case "mysql":
process.env.DATABASE_URL = `${config.source.provider}://${config.source.sql.user}:${config.source.sql.password}@${config.source.sql.host}:${config.source.sql.port}/${config.source.sql.database}`
break;
case "sqlite":
process.env.DATABASE_URL = `file:./${config.source.sqlite}`
break;
}
logger.info('Initializing the database.');
try {
await execAsync('bun prisma db push');
logger.info('Database model initialization successful.');
} catch (error) {
handleError('Error while initializing the database model!', error);
}
try {
await execAsync('bun prisma db seed');
logger.info('Target user fit the requirements.');
} catch (error) {
handleError('Error while preparing target user!', error);
}
const pm2command = `pm2 start ./index.js --watch --deep-monitoring --merge-logs -i 1 --kill-timeout ${config.daemon.timeout} --name ${config.daemon.name}`;
try {
await execAsync(pm2command);
logger.info(`Initialization successful.(${Math.round(performance.now())}ms)`);
displayBanner();
displayCommands();
} catch (error) {
handleError('Error while start server thread!', error);
}
};
const execAsync = (command) => {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
});
});
};
const handleError = (message, error) => {
logger.error(`${message}: ${error}`);
logger.error('EnShii-Daemon will exit now');
process.exit();
};
const displayBanner = () => {
console.log(`\x1B[2m╭──────────────────────────────────────────────────────╮\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓ 〓 〓 \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m ▓▓▓▓▓▓▓▓▓▓▓ ▓▓✚▓▓▓✚ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓▓▓▓▓✚ ▓▓ ▓▓ \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ ▓▓ \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m ▓▓▓▓▓▓▓▓▓▓▓ ▓▓ ▓▓ ▓▓▓▓▓▓▓▓▓▓▓ ▓▓ ▓▓ ▓▓ ▓▓ \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│──────────────────────────────────────────────────────│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m EnShii-Daemon \x1B[2m|\x1B[0m Powered by SPCraftMC & crux_tech. \x1B[2m│\x1B[0m`);
console.log(`\x1B[2m│\x1B[0m Made with \x1B[31m❤\x1B[0m . \x1B[2m│\x1B[0m`)
console.log(`\x1B[2m╰──────────────────────────────────────────────────────╯\x1B[0m`)
};
const displayCommands = () => {
const table = new Table({
head: ['Command', 'Description'],
chars: {
'top': '\x1B[2m─\x1B[0m', 'top-mid': '\x1B[2m┬\x1B[0m', 'top-left': '\x1B[2m╭\x1B[0m', 'top-right': '\x1B[2m╮\x1B[0m'
, 'bottom': '\x1B[2m─\x1B[0m', 'bottom-mid': '\x1B[2m┴\x1B[0m', 'bottom-left': '\x1B[2m╰\x1B[0m', 'bottom-right': '\x1B[2m╯\x1B[0m'
, 'left': '\x1B[2m│\x1B[0m', 'mid': '\x1B[2m─\x1B[0m', 'mid-mid': '\x1B[2m┼\x1B[0m', 'right-mid': '\x1B[2m│\x1B[0m', 'left-mid': '\x1B[2m│\x1B[0m', 'right': '\x1B[2m│\x1B[0m', 'middle': '\x1B[2m│\x1B[0m'
}
});
table.push(
['pm2 ls', 'list all pm2 instance'],
[`pm2 logs ${config.daemon.name}`, 'watch logs'],
['pm2 flush', 'flush logs'],
[`pm2 reload ${config.daemon.name}`, 'reload the server'],
[`pm2 stop ${config.daemon.name}`, 'stop the server'],
['pm2 kill', 'kill all']
);
console.log(table.toString());
}
init();