2015-01-09 19:08:14 +01:00
|
|
|
var _ = require('lodash');
|
|
|
|
var zlib = require('zlib');
|
|
|
|
var tar = require('tar');
|
|
|
|
var request = require('request');
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var fs = require('fs');
|
|
|
|
var join = require('path').join;
|
|
|
|
var filesPatern = _.template('node-v<%- version %>-<%- platform %>.tar.gz');
|
|
|
|
var urlPattern = _.template('http://nodejs.org/dist/v<%- version %>/<%- file %>');
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
module.exports = function (grunt) {
|
|
|
|
grunt.registerTask('download_node_binaries', 'Download the node.js binaries', function () {
|
|
|
|
var done = this.async();
|
|
|
|
var platforms = _.without(grunt.config.get('platforms'), 'windows');
|
2015-01-16 20:47:43 +01:00
|
|
|
var rootPath = grunt.config.get('root');
|
2015-01-09 19:08:14 +01:00
|
|
|
var version = grunt.config.get('nodeVersion');
|
|
|
|
|
2015-01-16 19:35:46 +01:00
|
|
|
var handle404 = function (response) {
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
throw new Error(response.request.href + ' failed with a ' + response.statusCode);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-09 19:08:14 +01:00
|
|
|
var downloadWindows = function (cb) {
|
2015-01-16 20:47:43 +01:00
|
|
|
var dest = join(rootPath, '.node_binaries', 'windows');
|
|
|
|
fs.stat(dest, function (err) {
|
|
|
|
if (!err) return cb(); // skip downloading if we already have them
|
|
|
|
var url = urlPattern({ version: version, file: 'node.exe'});
|
|
|
|
mkdirp(dest, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
var out = fs.createWriteStream(join(dest, 'node.exe'));
|
|
|
|
out.on('close', cb).on('error', cb);
|
|
|
|
var req = request.get(url);
|
|
|
|
req.on('response', handle404);
|
|
|
|
req.pipe(out);
|
|
|
|
});
|
2015-01-09 19:08:14 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var download = function (platform, cb) {
|
2015-01-16 20:47:43 +01:00
|
|
|
var dest = join(rootPath, '.node_binaries', platform);
|
|
|
|
fs.stat(dest, function (err) {
|
|
|
|
if (!err) return cb(); // skip downloading if we already have them
|
|
|
|
var file = filesPatern({ version: version, platform: platform });
|
|
|
|
var url = urlPattern({ version: version, file: file });
|
|
|
|
mkdirp(dest, function (err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
var unzip = zlib.createGunzip();
|
|
|
|
var out = tar.Extract({ path: dest, strip: 1 });
|
|
|
|
out.on('close', cb).on('error', cb);
|
|
|
|
var req = request.get(url);
|
|
|
|
req.on('response', handle404);
|
|
|
|
req.pipe(unzip).pipe(out);
|
|
|
|
});
|
2015-01-09 19:08:14 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
async.each(platforms, download, function (err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
downloadWindows(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|