index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.toggleOn('installed');
  15. return;
  16. }
  17. if(this.runPath == undefined) {
  18. log.warn("There is no bundlePath configured");
  19. context.toggleOn('installed');
  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. log.info("Emitting installed");
  27. context.toggleOn('installed',context);
  28. });
  29. for(var v of mods) {
  30. if(!v.endsWith(".zip")) {
  31. log.warn("Ignoring file in deploy: " + v);
  32. hitCounter.hit();
  33. continue;
  34. }
  35. this.installPlugin(path.resolve( path.join(this.deployPath,v)), hitCounter.hit.bind(hitCounter));
  36. }
  37. },
  38. installPlugin(fullPath,cb) {
  39. log.info("Intalling: " + fullPath);
  40. var basename = path.basename(fullPath,".zip");
  41. log.info("Base name: " + basename);
  42. var sourceZip = fullPath;
  43. var modTargetPath = path.join(this.runPath,basename);
  44. if(fs.existsSync(modTargetPath)) {
  45. log.verb("Already exists ignoring");
  46. if(!fs.existsSync(modTargetPath + "/node_modules")) {
  47. installNpm(modTargetPath,cb);
  48. } else {
  49. cb();
  50. }
  51. return;
  52. }
  53. fs.mkdirSync(modTargetPath);
  54. fs.createReadStream(sourceZip)
  55. .pipe(unzip.Extract({path: modTargetPath}))
  56. .on('close',() => {
  57. this.installNpm(modTargetPath,cb);
  58. });
  59. },
  60. installNpm(modTargetPath,cb) {
  61. var child = exec("npm install", {cwd: modTargetPath});
  62. child.on('close',cb);
  63. child.stdout.pipe(process.stdout);
  64. child.stderr.pipe(process.stderr);
  65. }
  66. }
  67. module.exports.bundleActivator = installerActivator;