index.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var log = require('hlogger').createLogger("installer");
  2. var fs = require('fs');
  3. var path = require('path');
  4. var unzip = require('unzip');
  5. var exec = require('child_process').exec;
  6. var HitCounter = require('./hit-counter');
  7. var installerActivator = {
  8. start(context) {
  9. // With config
  10. this.deployPath = context.manager.config.deployPath;
  11. this.runPath = context.manager.config.runPath;
  12. if(this.deployPath == undefined) {
  13. log.warn("There is no deploy path configured");
  14. context.events.channel(context.name).set("installed",true);
  15. //context.emit('installed');
  16. return;
  17. }
  18. if(this.runPath == undefined) {
  19. context.events.channel(context.name).set("installed",true);
  20. return;
  21. }
  22. var self = this;
  23. log.info("Deploy: " + this.deployPath +", runPath: " + this.runPath);
  24. var mods = fs.readdirSync(this.deployPath);
  25. var hitCounter = new HitCounter(mods.length, () => {
  26. context.channel(context.name).set("installed",true);
  27. });
  28. for(var v of mods) {
  29. if(!v.endsWith(".zip")) {
  30. log.warn("Ignoring file in deploy: " + v);
  31. hitCounter.hit();
  32. continue;
  33. }
  34. this.installPlugin(path.resolve( path.join(this.deployPath,v)), hitCounter.hit.bind(hitCounter));
  35. }
  36. },
  37. stop(context) {
  38. context.channel(context.name).unset('installed');
  39. },
  40. installPlugin(fullPath,cb) {
  41. log.info("Intalling: " + fullPath);
  42. var basename = path.basename(fullPath,".zip");
  43. log.info("Base name: " + basename);
  44. var sourceZip = fullPath;
  45. var modTargetPath = path.join(this.runPath,basename);
  46. if(fs.existsSync(modTargetPath)) {
  47. log.verb("Already exists ignoring");
  48. if(!fs.existsSync(modTargetPath + "/node_modules")) {
  49. installNpm(modTargetPath,cb);
  50. } else {
  51. cb();
  52. }
  53. return;
  54. }
  55. fs.mkdirSync(modTargetPath);
  56. fs.createReadStream(sourceZip)
  57. .pipe(unzip.Extract({path: modTargetPath}))
  58. .on('close',() => {
  59. this.installNpm(modTargetPath,cb);
  60. });
  61. },
  62. installNpm(modTargetPath,cb) {
  63. var child = exec("npm install", {cwd: modTargetPath});
  64. child.on('close',cb);
  65. child.stdout.pipe(process.stdout);
  66. child.stderr.pipe(process.stderr);
  67. }
  68. }
  69. module.exports.bundleActivator = installerActivator;