Skip to content

Commit 4a631a9

Browse files
authored
Idk
1 parent 44e8267 commit 4a631a9

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

index.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,73 @@
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';
29

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'));
1116

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'] });
1819

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;
2123

22-
var isRunning = false
2324
/**
2425
* Start a js file
2526
* @param {String} file `path/to/file`
2627
*/
2728
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+
3438
p.on('message', data => {
35-
console.log('[✅RECEIVED]', data)
39+
console.log('[✅RECEIVED]', data);
3640
switch (data) {
3741
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;
4246
case 'uptime':
43-
p.send(process.uptime())
44-
break
47+
p.send(process.uptime());
48+
break;
4549
}
46-
})
50+
});
51+
4752
p.on('exit', (_, code) => {
48-
isRunning = false
49-
console.error('[❗]Exited with code:', code)
53+
isRunning = false;
54+
console.error('[❗]Exited with code:', code);
5055
if (code !== 0) return start(file);
5156
watchFile(args[0], () => {
5257
unwatchFile(args[0]);
5358
start(file);
5459
});
5560
});
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+
}
6271
}
6372

64-
start('main.js')
73+
start('main.js');

0 commit comments

Comments
 (0)