index.js 3.4 KB

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