|
1 | | -import yargs from 'yargs'; |
2 | | -import cfonts from 'cfonts'; |
3 | | -import { fileURLToPath } from 'url'; |
4 | | -import { join, dirname } from 'path'; |
5 | | -import { createRequire } from 'module'; |
6 | | -import { createInterface } from 'readline'; |
7 | | -import { setupMaster, fork } from 'cluster'; |
8 | | -import { watchFile, unwatchFile } from 'fs'; |
| 1 | +console.log('🕖 Starting...') |
9 | 2 |
|
10 | | -// Setup console output |
11 | | -const { say } = cfonts; |
12 | | -const rl = createInterface(process.stdin, process.stdout); |
13 | | -const __dirname = dirname(fileURLToPath(import.meta.url)); |
14 | | -const require = createRequire(__dirname); |
15 | | -const { name, author } = require(join(__dirname, './package.json')); |
| 3 | +import { join, dirname } from 'path' |
| 4 | +import { createRequire } from "module" |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | +import { setupMaster, fork } from 'cluster' |
| 7 | +import { watchFile, unwatchFile } from 'fs' |
| 8 | +import cfonts from 'cfonts' |
| 9 | +import { createInterface } from 'readline' |
| 10 | +import yargs from 'yargs' |
| 11 | +import './config.js' |
16 | 12 |
|
17 | | -say('Lightweight\nWhatsApp Bot', { font: 'chrome', align: 'center', gradient: ['red', 'magenta'] }); |
18 | | -say(`'${name}' By @${author.name || author}`, { font: 'console', align: 'center', gradient: ['red', 'magenta'] }); |
| 13 | +const __dirname = dirname(fileURLToPath(import.meta.url)) |
| 14 | +const require = createRequire(__dirname) |
| 15 | +const { say } = cfonts |
| 16 | +const rl = createInterface(process.stdin, process.stdout) |
19 | 17 |
|
20 | | -console.log('🐾 Starting...'); |
| 18 | +say(global.config.namebot, { |
| 19 | + font: 'pallet', |
| 20 | + align: 'center', |
| 21 | + colors: ['white'] |
| 22 | +}) |
| 23 | +say(`⧻ ${global.config.namebot} by ${global.config.author}`, { |
| 24 | + font: 'console', |
| 25 | + align: 'center', |
| 26 | + colors: ['white'] |
| 27 | +}) |
21 | 28 |
|
22 | | -var isRunning = false; |
| 29 | +let isRunning = false |
23 | 30 |
|
24 | | -/** |
25 | | - * Start a js file |
26 | | - * @param {String} file `path/to/file` |
27 | | - */ |
28 | 31 | function start(file) { |
29 | | - if (isRunning) return; |
30 | | - isRunning = true; |
31 | | - |
32 | | - let args = [join(__dirname, file), ...process.argv.slice(2)]; |
33 | | - say([process.argv[0], ...args].join(' '), { font: 'console', align: 'center', gradient: ['red', 'magenta'] }); |
34 | | - |
35 | | - setupMaster({ exec: args[0], args: args.slice(1) }); |
36 | | - let p = fork(); |
37 | | - |
38 | | - p.on('message', data => { |
39 | | - console.log('[✅RECEIVED]', data); |
40 | | - switch (data) { |
41 | | - case 'reset': |
42 | | - p.kill(); // Change here |
43 | | - isRunning = false; |
44 | | - start(file); |
45 | | - break; |
46 | | - case 'uptime': |
47 | | - p.send(process.uptime()); |
48 | | - break; |
49 | | - default: |
50 | | - console.warn('[⚠️ UNRECOGNIZED MESSAGE]', data); |
51 | | - } |
52 | | - }); |
53 | | - |
54 | | - p.on('exit', (_, code) => { |
55 | | - isRunning = false; |
56 | | - console.error('[❗] Exited with code:', code); |
57 | | - if (code !== 0) { |
58 | | - console.log('[🔄 Restarting worker due to non-zero exit code...'); |
59 | | - return start(file); |
60 | | - } |
61 | | - |
62 | | - watchFile(args[0], () => { |
63 | | - unwatchFile(args[0]); |
64 | | - start(file); |
65 | | - }); |
66 | | - }); |
67 | | - |
68 | | - let opts = yargs(process.argv.slice(2)).exitProcess(false).parse(); |
69 | | - |
70 | | - if (!opts['test']) { |
71 | | - if (!rl.listenerCount()) { |
72 | | - rl.on('line', line => { |
73 | | - p.emit('message', line.trim()); |
74 | | - }); |
| 32 | + if (isRunning) return |
| 33 | + isRunning = true |
| 34 | + const args = [join(__dirname, file), ...process.argv.slice(2)] |
| 35 | + say([process.argv[0], ...args].join(' '), { |
| 36 | + font: 'console', |
| 37 | + align: 'center', |
| 38 | + colors: ['magenta'] |
| 39 | + }) |
| 40 | + say('⸙ MEMUAT SOURCE...', { |
| 41 | + font: 'console', |
| 42 | + align: 'center', |
| 43 | + colors: ['blue'] |
| 44 | + }) |
| 45 | + say('⸙ MEMUAT PLUGINS...', { |
| 46 | + font: 'console', |
| 47 | + align: 'center', |
| 48 | + colors: ['blue'] |
| 49 | + }) |
| 50 | + say('✅ DONE !', { |
| 51 | + font: 'console', |
| 52 | + align: 'center', |
| 53 | + colors: ['green'] |
| 54 | + }) |
| 55 | + setupMaster({ |
| 56 | + exec: args[0], |
| 57 | + args: args.slice(1), |
| 58 | + }) |
| 59 | + const p = fork() |
| 60 | + p.on('message', data => { |
| 61 | + console.log('[RECEIVED]', data) |
| 62 | + switch (data) { |
| 63 | + case 'reset': |
| 64 | + p.process.kill() |
| 65 | + isRunning = false |
| 66 | + start.apply(this, arguments) |
| 67 | + break |
| 68 | + case 'uptime': |
| 69 | + p.send(process.uptime()) |
| 70 | + break |
| 71 | + } |
| 72 | + }) |
| 73 | + p.on('exit', (_, code) => { |
| 74 | + isRunning = false |
| 75 | + if (code == 'SIGKILL' || code == 'SIGABRT') return start(file) |
| 76 | + console.error('[❗] Exited with code:', code) |
| 77 | + if (code === 0) return |
| 78 | + watchFile(args[0], () => { |
| 79 | + unwatchFile(args[0]) |
| 80 | + start(file) |
| 81 | + }) |
| 82 | + }) |
| 83 | + const opts = yargs(process.argv.slice(2)).exitProcess(false).parse() |
| 84 | + if (!opts['test'] && !rl.listenerCount()) { |
| 85 | + rl.on('line', line => { |
| 86 | + p.emit('message', line.trim()) |
| 87 | + }) |
75 | 88 | } |
76 | | - } |
77 | 89 | } |
78 | 90 |
|
79 | | -start('main.js'); |
| 91 | +start('main.js') |
0 commit comments