index.js 3.3 KB

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