refactor startup.js

This commit is contained in:
Daniel afx 2022-03-23 05:47:22 +02:00
parent 33fa4e7b9b
commit 794d1eb909

View file

@ -1,4 +1,3 @@
var RedditRadio = require("./RedditRadio");
var toml = require("toml"); var toml = require("toml");
var fs = require("fs"); var fs = require("fs");
var Radio = require("./Radio"); var Radio = require("./Radio");
@ -7,11 +6,6 @@ class Startup
{ {
constructor() constructor()
{ {
this.RADIO = 'radio';
this.RADIOS = 'radios';
this.NO_RADIO = 'no-radio';
this.REGULAR_BOT = 'regular-bot';
this.radios = []; this.radios = [];
let configFile = process.env.CONFIG_FILE || "config.toml"; let configFile = process.env.CONFIG_FILE || "config.toml";
@ -20,41 +14,18 @@ class Startup
run() run()
{ {
let args = this.parseArgs(); this.setupRadio(); break;
switch (args.type) {
case this.RADIO: this.setupRadio(args.name); break;
case this.RADIOS: this.setupRadios(); break;
case this.NO_RADIO: this.setupNoRadio(); break;
default: this.startBot(this.config); break;
}
} }
setupNoRadio() setupRadio()
{
let config = this.config;
delete config.radios;
this.startBot(config);
}
setupRadio(name)
{ {
if (this.config.radios === undefined) { if (this.config.radios === undefined) {
console.error('No radios are defined in the config file, exiting...'); console.error('No radios are defined in the config file, exiting...');
process.exit(1); process.exit(1);
} }
for (let i = 0; i < this.config.radios.length; i++) { for (let i = 0; i < this.config.radios.length; i++) {
if (this.config.radios[i].name === name) {
this.startRadio(this.config.radios[i]);
break;
}
}
this.setupSignals();
}
setupRadios()
{
for (var i = 0; i < this.config.radios.length; i++) {
this.startRadio(this.config.radios[i]); this.startRadio(this.config.radios[i]);
}
} }
this.setupSignals(); this.setupSignals();
} }
@ -64,14 +35,6 @@ class Startup
this.radios.push(new Radio(this.config, radioConfig)); this.radios.push(new Radio(this.config, radioConfig));
} }
startBot(config)
{
this.bot = new RedditRadio(config);
this.bot.start();
this.setupSignals();
}
setupSignals() setupSignals()
{ {
var stopHandler = () => { var stopHandler = () => {
@ -85,17 +48,6 @@ class Startup
process.on("SIGINT", stopHandler); // Ctrl+C process.on("SIGINT", stopHandler); // Ctrl+C
process.on("SIGTERM", stopHandler); // Terminate process.on("SIGTERM", stopHandler); // Terminate
} }
parseArgs()
{
let args = process.argv.splice(2);
switch (args[0]) {
case '--radio': return { type: this.RADIO, name: args[1]};
case '--radios': return { type: this.RADIOS };
case '--no-radios': return { type: this.NO_RADIO };
default: return { type: this.REGULAR_BOT };
}
}
} }
module.exports = Startup; module.exports = Startup;