index.js 3.4 KB

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