Hot reload code (not finished)
This commit is contained in:
parent
cdaba3c951
commit
6a4021d5ae
|
@ -1,5 +1,7 @@
|
|||
prefixes:
|
||||
- id: "**"
|
||||
- id: "*"
|
||||
spaced: false
|
||||
- id: cdebug
|
||||
- id: cosmic
|
||||
spaced: true
|
||||
- id: c
|
||||
spaced: false
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
debug: true
|
||||
enableConsole: true
|
||||
enableMPP: true
|
||||
enableMPP: false
|
||||
enableDiscord: false
|
||||
enableSwitchChat: false
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -50,3 +50,5 @@ export function loadCommands() {
|
|||
]);
|
||||
CommandHandler.addCommandGroup(utility);
|
||||
}
|
||||
|
||||
export { CommandHandler, CommandGroup };
|
||||
|
|
39
src/index.ts
39
src/index.ts
|
@ -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";
|
||||
|
||||
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();
|
||||
ServiceLoader.loadServices();
|
||||
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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)) {
|
||||
|
|
Loading…
Reference in New Issue