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:
- id: "**"
- id: "*"
spaced: false
- id: cdebug
- id: cosmic
spaced: true
- id: c
spaced: false

View File

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

View File

@ -46,7 +46,7 @@ export type BaseCommandMessage<T = unknown> = Omit<
export class CommandHandler {
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");
@ -172,5 +172,5 @@ export class CommandHandler {
// Add 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);
}
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 { ServiceLoader } from "./services";
import { ConsoleAgent } from "./services/console";
import { printStartupASCII } from "./util/ascii";
printStartupASCII();
loadRoleConfig();
loadCommands();
ServiceLoader.loadServices();
declare global {
var loaded: boolean;
var serviceLoader: any;
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) {
return eval(code);

View File

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

View File

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