const fs = require('fs'); const path = require('path'); const Module = require('../base/Module'); const RepositorySystem = require('./RepositorySystem'); class ModuleSystem { constructor() { this.modules = []; this.instances = []; this.loadedModules = []; } run() { this.instances = this.modules.map((mod) => new mod()); this.loadedModules = [...this.loadedModules, ...this.modules]; this.modules = []; } loadModule(mod) { mod.__loadedDirectory = "imported/" + mod.name; this.modules.push(mod); console.log("File ", mod.__loadedDirectory + ' loaded successfully'); } loadModules(filepath = 'modules', prefix="") { // search files let p = path.resolve(filepath); let current = []; console.log("Filepath:", p); let files = fs.readdirSync(p); for(var ind in files) { let filename = files[ind]; try { let temp = require(path.resolve(filepath, filename)); if(temp.prototype instanceof Module) { temp.__loadedDirectory = filepath + '/' + filename; current.push(temp); } } catch(e) { let err = "Cannot read file -> " + filepath + '/' + filename; console.log(err); throw e; } } console.log('Modules Loaded:'); for(var i in current) { console.log("File ", current[i].__loadedDirectory + ' loaded successfully'); } this.modules = [ ...this.modules, ...current ]; } } module.exports = new ModuleSystem();