So, I finally fixed this problem. If anyone is interested in how I did this, here's how:
I built a function using the NPM api to check if the current version is updated:
exports.checkVersion = function(req, res) { npm.load([], function (err, npm) { npm.commands.search(["mediacenterjs"], function(err, data){ if (err){ console.log('NPM search error ' + err); return; } else{ var currentInfo = checkCurrentVersion(); for (var key in data) { var obj = data[key]; if(obj.name === 'mediacenterjs' && obj.version > currentInfo.version){ var message = 'New version '+obj.version+' Available'; res.json(message); } } } }); }); } var checkCurrentVersion = function(){ var info = {}; var data = fs.readFileSync('./package.json' , 'utf8'); try{ info = JSON.parse(data); }catch(e){ console.log('JSON Parse Error', e); } return info; };
If the version is not updated, I start the download (in my case the github master repository URL) using node-wget:
var wget = require('wget'); var download = wget.download(src, output, options); download.on('error', function(err) { console.log('Error', err); callback(output); }); download.on('end', function(output) { console.log(output); callback(output); }); download.on('progress', function(progress) { console.log(progress * 100); });
The callback drops the unzip database on the @John Munsch Self Help script, but I added a check to check if there was a previous unpack attempt, and if so, I delete the folder:
if(fs.existsSync(dir) === false){ fs.mkdirSync(dir); } else { rimraf(dir, function (err) { if(err) { console.log('Error removing temp folder', err); } else { fileHandler.downloadFile(src, output, options, function(output){ console.log('Done', output); unzip(req, res, output, dir); }); } }); } console.log("Unzipping New Version..."); var AdmZip = require("adm-zip"); var zip = new AdmZip(output); zip.extractAllTo(dir, true); fs.openSync('./configuration/update.js', 'w');
The openSync function runs my NodeMon-based file ( https://github.com/jansmolders86/mediacenterjs/blob/master/server.js ), which kills the server as it lists the changes for that particular file. Finally, it reboots and launches the following functions:
function installUpdate(output, dir){ console.log('Installing update...'); var fsExtra = require("fs.extra"); fsExtra.copy(dir+'/mediacenterjs-master', './', function (err) { if (err) { console.error('Error', err); } else { console.log("success!"); cleanUp(output, dir); } }); } function cleanUp(output, dir) { console.log('Cleanup...'); var rimraf = require('rimraf'); rimraf(dir, function (e) { if(e) { console.log('Error removing module', e .red); } }); if(fs.existsSync(output) === true){ fs.unlinkSync(output); console.log('Done, restarting server...') server.start(); } }
Thanks to everyone who helped me!