Hot reload code (not finished)

This commit is contained in:
Hri7566 2023-12-16 01:06:19 -05:00
parent cdaba3c951
commit 6a4021d5ae
7 changed files with 54 additions and 11 deletions

View File

@ -1,5 +1,7 @@
prefixes: prefixes:
- id: "**" - id: "*"
spaced: false spaced: false
- id: cdebug - id: cosmic
spaced: true spaced: true
- id: c
spaced: false

View File

@ -1,5 +1,5 @@
debug: true debug: true
enableConsole: true enableConsole: true
enableMPP: true enableMPP: false
enableDiscord: false enableDiscord: false
enableSwitchChat: false enableSwitchChat: false

View File

@ -46,7 +46,7 @@ export type BaseCommandMessage<T = unknown> = Omit<
export class CommandHandler { export class CommandHandler {
public static commandGroups = new Array<CommandGroup>(); public static commandGroups = new Array<CommandGroup>();
public static prefixes = new Array<Prefix>(); public static prefixes = new Set<Prefix>();
public static logger = new Logger("Command Handler"); public static logger = new Logger("Command Handler");
@ -172,5 +172,5 @@ export class CommandHandler {
// Add prefixes // Add prefixes
for (const prefix of prefixConfig.prefixes) { for (const prefix of prefixConfig.prefixes) {
CommandHandler.prefixes.push(prefix); CommandHandler.prefixes.add(prefix);
} }

View File

@ -50,3 +50,5 @@ export function loadCommands() {
]); ]);
CommandHandler.addCommandGroup(utility); CommandHandler.addCommandGroup(utility);
} }
export { CommandHandler, CommandGroup };

View File

@ -1,12 +1,47 @@
import { loadCommands } from "./commands"; import { loadCommands, CommandHandler, type CommandGroup } from "./commands";
import { loadRoleConfig } from "./permissions"; import { loadRoleConfig } from "./permissions";
import { ServiceLoader } from "./services"; import { ServiceLoader } from "./services";
import { ConsoleAgent } from "./services/console";
import { printStartupASCII } from "./util/ascii"; import { printStartupASCII } from "./util/ascii";
printStartupASCII(); declare global {
loadRoleConfig(); var loaded: boolean;
loadCommands(); var serviceLoader: any;
ServiceLoader.loadServices(); var commandHandler: any;
}
// Set on first run
globalThis.loaded ??= false;
globalThis.serviceLoader ??= ServiceLoader;
globalThis.commandHandler ??= CommandHandler;
function load() {
printStartupASCII();
loadRoleConfig();
loadCommands();
globalThis.serviceLoader.loadServices();
globalThis.loaded = true;
}
// Check for hot reload
if (!globalThis.loaded) {
load();
console.log("Command list:", globalThis.commandHandler.commandGroups);
} else {
console.log("Hot reload triggered...");
// Set console prompt
globalThis.serviceLoader.agents.forEach(agent => {
if (agent.platform === "console")
(agent as ConsoleAgent).client.prompt();
});
// Reload commands
globalThis.commandHandler.commandGroups = new Array<CommandGroup>();
loadCommands();
console.log("Command list:", globalThis.commandHandler.commandGroups);
}
export function scopedEval(code: string) { export function scopedEval(code: string) {
return eval(code); return eval(code);

View File

@ -37,7 +37,7 @@ export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
public stop() { public stop() {
if (!this.started) return; if (!this.started) return;
this.started = false; this.started = false;
this.client.setPrompt(""); this.client.close();
} }
protected bindEventListeners(): void { protected bindEventListeners(): void {

View File

@ -50,6 +50,10 @@ export class ServiceLoader {
this.agents.push(agent); this.agents.push(agent);
} }
public static getAgent(index: number) {
return this.agents[index];
}
public static loadServices() { public static loadServices() {
if (config.enableMPP) { if (config.enableMPP) {
for (const uri of Object.keys(mppConfig.agents)) { for (const uri of Object.keys(mppConfig.agents)) {