@uisap/core
Version:
A modular Fastify-based framework inspired by Laravel
46 lines (41 loc) • 1.38 kB
JavaScript
import { Command } from "./command.js";
import { pathToFileURL } from "url";
export class ConsoleKernel {
constructor(app) {
this.app = app;
this.commands = new Map();
}
register(signature, CommandClass) {
this.commands.set(signature, CommandClass);
this.app.fastify.logger.debug(`Komut kaydedildi: ${signature}`);
}
async run(signature, ...args) {
const CommandClass = this.commands.get(signature);
if (!CommandClass) {
throw new Error(`Komut bulunamadı: ${signature}`);
}
const command = new CommandClass(this.app.fastify);
return await command.execute(...args);
}
loadCommands(commandsConfig = {}) {
for (const [signature, path] of Object.entries(commandsConfig)) {
const fileUrl = pathToFileURL(path).href;
import(fileUrl)
.then((module) => {
const CommandClass = module.default || module;
if (!(CommandClass.prototype instanceof Command)) {
throw new Error(`Geçersiz komut sınıfı: ${signature}`);
}
this.register(signature, CommandClass);
this.app.fastify.logger.debug(
`Komut yüklendi: ${signature} -> ${path}`
);
})
.catch((err) => {
this.app.fastify.logger.error(`Komut yükleme hatası: ${signature}`, {
error: err.message,
});
});
}
}
}