Created
July 1, 2014 20:11
-
-
Save dogancelik/cbd9b28a7e1126f2a930 to your computer and use it in GitHub Desktop.
(Windows) Update Node.js (64-bit version) (No download if versions match)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var fs = require('fs'); | |
var child_process = require('child_process'); | |
var pageUrl = "http://nodejs.org/download/"; | |
var downloadUrlRegex = /http:\/\/nodejs\.org\/dist\/v[0-9\.]+\/x64\/node-(v[0-9\.]+)-x64\.msi/i; | |
var downloadUrl; | |
var downloadVersion; | |
var pageSource; | |
var filePath = "node.msi"; | |
var nodeVersion; | |
child_process.exec("node -v", function (err, stdout) { | |
nodeVersion = stdout.trim(); | |
var pageReq = http.get(pageUrl, function (res) { | |
res.setEncoding("utf8"); | |
res.on("data", function (chunk) { | |
pageSource += chunk; | |
}); | |
res.on("end", function () { | |
var match = pageSource.match(downloadUrlRegex); | |
downloadUrl = match[0]; | |
downloadVersion = match[1]; | |
if (nodeVersion === downloadVersion) return; | |
var file = fs.createWriteStream(filePath); | |
http.get(downloadUrl, function (res) { | |
res.pipe(file); | |
file.on('finish', function() { | |
file.close(); | |
child_process.exec(filePath + " /quiet", function() { | |
fs.unlinkSync(filePath); | |
}); | |
}); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment