var log = require('hlogger').createLogger('loader'); var path = require('path'); var fs = require('fs'); var clitable = require('./clitable'); var loaderActivator = { start(context) { this.context = context; this.manager = context.manager; this.runPath = context.manager.config.runPath; this.registerShell(context); if(!this.runPath ) { log.warn("There is no runPath configured in manager"); return; } context.events .channel('core-installer') .watch('installed',(value) => { console.log("core-installer:installed is:",value); if(value == true) { process.nextTick(() => { this.loadBundles()}); } }); //this.loadBundles(); // Check this out its wrong!? }, loadBundles() { var mods = fs.readdirSync(this.runPath); for(var v of mods) { var modPath = path.join(this.runPath , v); this.startPlugin(modPath); } }, startPlugin(plugPath) { this.manager.load(plugPath); /*var dirparts = path.basename(plugPath).split(path.sep); var dirname = dirparts[dirparts.length-1]; //var plugInfo = require(path.join(plugPath , "package.json")); var plugName = dirname; this.manager.register({name:plugName, bundle:plugPath});*/ }, registerShell(context) { //var prefix = context.prefix(".*:cmd"); // Any cmd sender context.events .channel('core-shell','cmd') .on('lb',(req,res) => { var tbl = []; for(var k in context.manager.registry) { var v = context.manager.registry[k]; if(v == undefined) { continue; } var modPath = v.modulePath; if(modPath) { modPath = modPath.replace(new RegExp("^" + process.env.PWD + "/"),""); } tbl.push({ id: v.id, name:v.name, state: v.info.state, package: (v.info.pkgInfo)?v.info.pkgInfo.name:"", version: (v.info.pkgInfo)?v.info.pkgInfo.version:"", author: (v.info.pkgInfo)?v.info.pkgInfo.author.name:"", modulePath: modPath, }); } tbl = tbl.sort((a,b) => { return a.id - b.id}); res.write(clitable(tbl)); //log.info("\n"+clitable(tbl)); }); function commonGetBundle(req,res,nargs) { if(req.args < nargs) { res.write("Not enough parameters\n"); return null; } var bcontext = context.manager.get(req.args[0]); if(!bcontext) { res.write("Bundle not found\n"); } return bcontext; } context.events .channel('core-shell','cmd') .on('stop',(req,res) => { var bcontext = commonGetBundle(req,res,1); if(bcontext == null) { return; } bcontext.stop(); }).on('start',(req,res) => { var bcontext = commonGetBundle(req,res,1); if(bcontext == null) { return; } bcontext.start(); }).on('reload',(req,res) => { var bcontext = commonGetBundle(req,res,1); if(bcontext == null) { return; } var modulePath = bcontext.modulePath; context.manager.unregister(bcontext); context.manager.register({id:bcontext.id, name:bcontext.name, bundle:bcontext.modulePath}); }).on('unload',(req,res) => { var bcontext = commonGetBundle(req,res,1); if(bcontext == null) { return; } context.manager.unregister(bcontext); }).on('load',(req,res) => { if(req.args.length < 1) { res.write("Not enough parameters\n"); return; } res.write("Loading module from: " + process.env.PWD +"\n"); context.manager.load(process.env.PWD + "/" + req.args[0]); //context.manager.register({name:args[1], bundle:path.resolve(path.join(process.env.PWD, args[2]))}); }); } }; module.exports.bundleActivator = loaderActivator;