You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
913 B

const fs = require('fs');
const path = require('path');
class ModuleLoader {
constructor(modulesDir) {
this.modulesDir = path.resolve(modulesDir);
this.modules = {};
}
discover() {
if (!fs.existsSync(this.modulesDir)) return [];
const entries = fs.readdirSync(this.modulesDir, { withFileTypes: true });
return entries.filter(e => e.isDirectory()).map(d => d.name);
}
loadAll(engineContext) {
const names = this.discover();
names.forEach(name => {
const modPath = path.join(this.modulesDir, name);
try {
const mod = require(modPath);
if (typeof mod.init === 'function') {
mod.init(engineContext);
}
this.modules[name] = mod;
} catch (err) {
console.error(`Failed to load module ${name}:`, err && err.stack ? err.stack : err);
}
});
return this.modules;
}
}
module.exports = ModuleLoader;