|
1 | | -console.log('🐾 Starting...') |
| 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'; |
2 | 9 |
|
3 | | -import yargs from 'yargs' |
4 | | -import cfonts from 'cfonts' |
5 | | -import { fileURLToPath } from 'url' |
6 | | -import { join, dirname } from 'path' |
7 | | -import { createRequire } from 'module' |
8 | | -import { createInterface } from 'readline' |
9 | | -import { setupMaster, fork } from 'cluster' |
10 | | -import { watchFile, unwatchFile } from 'fs' |
| 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')); |
11 | 16 |
|
12 | | -// https://stackoverflow.com/a/50052194 |
13 | | -const { say } = cfonts |
14 | | -const rl = createInterface(process.stdin, process.stdout) |
15 | | -const __dirname = dirname(fileURLToPath(import.meta.url)) |
16 | | -const require = createRequire(__dirname) // Bring in the ability to create the 'require' method |
17 | | -const { name, author } = require(join(__dirname, './package.json')) // https://www.stefanjudis.com/snippets/how-to-import-json-files-in-es-modules-node-js/ |
| 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'] }); |
18 | 19 |
|
19 | | -say('Lightweight\nWhatsApp Bot', { font: 'chrome', align: 'center', gradient: ['red', 'magenta'] }) |
20 | | -say(`'${name}' By @${author.name || author}`, { font: 'console', align: 'center', gradient: ['red', 'magenta'] }) |
| 20 | +console.log('🐾 Starting...'); |
| 21 | + |
| 22 | +var isRunning = false; |
21 | 23 |
|
22 | | -var isRunning = false |
23 | 24 | /** |
24 | 25 | * Start a js file |
25 | 26 | * @param {String} file `path/to/file` |
26 | 27 | */ |
27 | 28 | function start(file) { |
28 | | - if (isRunning) return |
29 | | - isRunning = true |
30 | | - let args = [join(__dirname, file), ...process.argv.slice(2)] |
31 | | - say([process.argv[0], ...args].join(' '), { font: 'console', align: 'center', gradient: ['red', 'magenta'] }) |
32 | | - setupMaster({ exec: args[0], args: args.slice(1) }) |
33 | | - let p = fork() |
| 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 | + |
34 | 38 | p.on('message', data => { |
35 | | - console.log('[✅RECEIVED]', data) |
| 39 | + console.log('[✅RECEIVED]', data); |
36 | 40 | switch (data) { |
37 | 41 | case 'reset': |
38 | | - p.process.kill() |
39 | | - isRunning = false |
40 | | - start.apply(this, arguments) |
41 | | - break |
| 42 | + p.kill(); // Change here |
| 43 | + isRunning = false; |
| 44 | + start(file); |
| 45 | + break; |
42 | 46 | case 'uptime': |
43 | | - p.send(process.uptime()) |
44 | | - break |
| 47 | + p.send(process.uptime()); |
| 48 | + break; |
45 | 49 | } |
46 | | - }) |
| 50 | + }); |
| 51 | + |
47 | 52 | p.on('exit', (_, code) => { |
48 | | - isRunning = false |
49 | | - console.error('[❗]Exited with code:', code) |
| 53 | + isRunning = false; |
| 54 | + console.error('[❗]Exited with code:', code); |
50 | 55 | if (code !== 0) return start(file); |
51 | 56 | watchFile(args[0], () => { |
52 | 57 | unwatchFile(args[0]); |
53 | 58 | start(file); |
54 | 59 | }); |
55 | 60 | }); |
56 | | - let opts = new Object(yargs(process.argv.slice(2)).exitProcess(false).parse()) |
57 | | - if (!opts['test']) |
58 | | - if (!rl.listenerCount()) rl.on('line', line => { |
59 | | - p.emit('message', line.trim()) |
60 | | - }) |
61 | | - // console.log(p) |
| 61 | + |
| 62 | + let opts = yargs(process.argv.slice(2)).exitProcess(false).parse(); |
| 63 | + |
| 64 | + if (!opts['test']) { |
| 65 | + if (!rl.listenerCount()) { |
| 66 | + rl.on('line', line => { |
| 67 | + p.emit('message', line.trim()); |
| 68 | + }); |
| 69 | + } |
| 70 | + } |
62 | 71 | } |
63 | 72 |
|
64 | | -start('main.js') |
| 73 | +start('main.js'); |
0 commit comments