index.js 2.2 KB

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