| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 | 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.on('core-installer:installed',() => {				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) {		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.on('core-shell:cmd: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.on('core-shell:cmd:stop',(req,res) => {			var bcontext = commonGetBundle(req,res,1);			if(bcontext == null) { return; }			bcontext.stop();		});		context.on('core-shell:cmd:start',(req,res) => {			var bcontext = commonGetBundle(req,res,1);			if(bcontext == null) { return; }			bcontext.start();		});		context.on('core-shell:cmd: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});		});		context.on('core-shell:cmd:unload',(req,res) => {			var bcontext = commonGetBundle(req,res,1);			if(bcontext == null) { return; }			context.manager.unregister(bcontext);		});				context.on('core-shell:cmd: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;
 |