Lakukan optimalisasi pada kode berikut: console.log('Starting...'); import {…
- User
Lakukan optimalisasi pada kode berikut: console.log('Starting...');
import { fileURLToPath } from 'url'; import { join, dirname } from 'path'; import { createRequire } from 'module'; import { createInterface } from 'readline'; import { setupMaster, fork } from 'cluster'; import { watchFile, unwatchFile } from 'fs'; import cfonts from 'cfonts'; import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers';
const { say } = cfonts; const rl = createInterface(process.stdin, process.stdout); const __dirname = dirname(fileURLToPath(import.meta.url)); const require = createRequire(import.meta.url); const { name, author } = require(join(__dirname, './package.json'));
say('Lightweight\nWhatsApp Bot', { font: 'chrome', align: 'center', gradient: ['red', 'magenta'], });
let isRunning = false; let p; const opts = yargs(hideBin(process.argv)).exitProcess(false).parse();
function start(file) { if (isRunning) return; isRunning = true; const args = [join(__dirname, file), ...process.argv.slice(2)]; setupMaster({ exec: args[0], args: args.slice(1) }); p = fork(); p.on('message', (data) => { switch (data) { case 'reset': p.kill(); isRunning = false; start(file); break; case 'uptime': p.send(process.uptime()); break; } }); p.on('exit', (code) => { isRunning = false; console.error('Exited with code:', code); if (code === 0) { start(file); } else { watchFile(args[0], () => { unwatchFile(args[0]); start(file); }); } }); }
start('main.js');
if (!opts.test) { rl.on('line', (line) => { if (p && !p.killed) { p.send(line.trim()); } }); }
- Assistant