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