index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. var log = require('hlogger').createLogger('loader');
  2. var path = require('path');
  3. var fs = require('fs');
  4. var clitable = require('./clitable');
  5. var loaderActivator = {
  6. start(context) {
  7. this.context = context;
  8. this.manager = context.manager;
  9. this.runPath = context.manager.config.runPath;
  10. if(!this.runPath ) {
  11. log.warn("There is no runPath configured in manager");
  12. return;
  13. }
  14. // Check this out its wrong!?
  15. context.on('core-installer:installed',() => {
  16. this.loadBundles();
  17. });
  18. this.registerShell(context);
  19. },
  20. loadBundles() {
  21. var mods = fs.readdirSync(this.runPath);
  22. for(var v of mods) {
  23. var modPath = path.join(this.runPath , v);
  24. this.startPlugin(modPath);
  25. }
  26. },
  27. startPlugin(plugPath) {
  28. var dirparts = path.basename(plugPath).split(path.sep);
  29. var dirname = dirparts[dirparts.length-1];
  30. //var plugInfo = require(path.join(plugPath , "package.json"));
  31. var plugName = dirname;
  32. this.manager.register({name:plugName, bundle:plugPath});
  33. },
  34. registerShell(context) {
  35. var prefix = context.prefix(".*:cmd"); // Any cmd sender
  36. prefix.on('lb',(req,res) => {
  37. var tbl = [];
  38. for(var k in context.manager.registry) {
  39. var v = context.manager.registry[k];
  40. if(v == undefined) {
  41. continue;
  42. }
  43. var modPath = v.modulePath;
  44. if(modPath) {
  45. modPath = modPath.replace(new RegExp("^" + process.env.PWD + "/"),"");
  46. }
  47. tbl.push({
  48. id: v.id,
  49. name:v.name,
  50. state: v.info.state,
  51. package: (v.info.pkgInfo)?v.info.pkgInfo.name:"",
  52. version: (v.info.pkgInfo)?v.info.pkgInfo.version:"",
  53. author: (v.info.pkgInfo)?v.info.pkgInfo.author.name:"",
  54. modulePath: modPath,
  55. });
  56. }
  57. tbl = tbl.sort((a,b) => { return a.id - b.id});
  58. res.write(clitable(tbl));
  59. //log.info("\n"+clitable(tbl));
  60. });
  61. function commonGetBundle(req,res,nargs) {
  62. if(req.args < nargs) {
  63. res.write("Not enough parameters\n");
  64. return null;
  65. }
  66. var bcontext = context.manager.get(req.args[0]);
  67. if(!bcontext) {
  68. res.write("Bundle not found\n");
  69. }
  70. return bcontext;
  71. }
  72. prefix.on('stop',(req,res) => {
  73. var bcontext = commonGetBundle(req,res,1);
  74. if(bcontext == null) { return; }
  75. bcontext.stop();
  76. });
  77. prefix.on('start',(req,res) => {
  78. var bcontext = commonGetBundle(req,res,1);
  79. if(bcontext == null) { return; }
  80. bcontext.start();
  81. });
  82. prefix.on('reload',(req,res) => {
  83. var bcontext = commonGetBundle(req,res,1);
  84. if(bcontext == null) { return; }
  85. var modulePath = bcontext.modulePath;
  86. context.manager.unregister(bcontext);
  87. context.manager.register({id:bcontext.id, name:bcontext.name, bundle:bcontext.modulePath});
  88. });
  89. prefix.on('unload',(req,res) => {
  90. var bcontext = commonGetBundle(req,res,1);
  91. if(bcontext == null) { return; }
  92. context.manager.unregister(bcontext);
  93. });
  94. prefix.on('load',(req,res) => {
  95. if(req.args.length < 1) {
  96. res.write("Not enough parameters\n");
  97. return;
  98. }
  99. res.write("Loading module from: " + process.env.PWD +"\n");
  100. context.manager.load(process.env.PWD + "/" + req.args[0]);
  101. //context.manager.register({name:args[1], bundle:path.resolve(path.join(process.env.PWD, args[2]))});
  102. });
  103. }
  104. };
  105. module.exports.bundleActivator = loaderActivator;