var log = require('hlogger').createLogger("installer"); var fs = require('fs'); var path = require('path'); var unzip = require('unzip'); var exec = require('child_process').exec; var HitCounter = require('./hit-counter'); var installerActivator = { start(context) { // With config this.deployPath = context.manager.config.deployPath; this.runPath = context.manager.config.runPath; if(this.deployPath == undefined) { log.warn("There is no deploy path configured"); context.channel(context.name).set("installed",true); //context.emit('installed'); return; } if(this.runPath == undefined) { context.channel(context.name).set("installed",true); return; } var self = this; log.info("Deploy: " + this.deployPath +", runPath: " + this.runPath); var mods = fs.readdirSync(this.deployPath); var hitCounter = new HitCounter(mods.length, () => { context.channel(context.name).set("installed",true); }); for(var v of mods) { if(!v.endsWith(".zip")) { log.warn("Ignoring file in deploy: " + v); hitCounter.hit(); continue; } this.installPlugin(path.resolve( path.join(this.deployPath,v)), hitCounter.hit.bind(hitCounter)); } }, stop(context) { context.channel(context.name).unset('installed'); }, installPlugin(fullPath,cb) { log.info("Intalling: " + fullPath); var basename = path.basename(fullPath,".zip"); log.info("Base name: " + basename); var sourceZip = fullPath; var modTargetPath = path.join(this.runPath,basename); if(fs.existsSync(modTargetPath)) { log.verb("Already exists ignoring"); if(!fs.existsSync(modTargetPath + "/node_modules")) { installNpm(modTargetPath,cb); } else { cb(); } return; } fs.mkdirSync(modTargetPath); fs.createReadStream(sourceZip) .pipe(unzip.Extract({path: modTargetPath})) .on('close',() => { this.installNpm(modTargetPath,cb); }); }, installNpm(modTargetPath,cb) { var child = exec("npm install", {cwd: modTargetPath}); child.on('close',cb); child.stdout.pipe(process.stdout); child.stderr.pipe(process.stderr); } } module.exports.bundleActivator = installerActivator;