kibana/tasks/build/download_node_builds.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-08-14 06:16:54 +02:00
module.exports = function (grunt) {
2015-08-14 20:53:26 +02:00
let { map, fromNode } = require('bluebird');
2015-08-14 06:16:54 +02:00
let { resolve } = require('path');
let { pluck } = require('lodash');
2015-08-14 06:16:54 +02:00
let { createWriteStream } = require('fs');
let { createGunzip } = require('zlib');
let { Extract } = require('tar');
2015-08-14 20:53:26 +02:00
let { rename } = require('fs');
let wreck = require('wreck');
2015-08-14 06:16:54 +02:00
let platforms = grunt.config.get('platforms');
2015-08-14 20:53:26 +02:00
let activeDownloads = [];
let start = async (platform) => {
let finalDir = platform.nodeDir;
let downloadDir = `${finalDir}.temp`;
if (grunt.file.isDir(platform.nodeDir)) {
grunt.log.ok(`${platform.name} exists`);
return;
}
let resp = await fromNode(cb => {
let req = wreck.request('GET', platform.nodeUrl, null, function (err, resp) {
if (err) {
return cb(err);
}
if (resp.statusCode !== 200) {
return cb(new Error(`${platform.nodeUrl} failed with a ${resp.statusCode} response`));
}
return cb(null, resp);
});
2015-08-14 06:16:54 +02:00
});
2015-08-14 20:53:26 +02:00
// use an async iife to store promise for download
// then store platform in active downloads list
// which we will read from in the finish task
platform.downloadPromise = (async function () {
2015-08-14 20:53:26 +02:00
grunt.file.mkdir(downloadDir);
if (platform.win) {
2015-08-14 20:53:26 +02:00
await fromNode(cb => {
resp
.pipe(createWriteStream(resolve(downloadDir, 'node.exe')))
.on('error', cb)
.on('finish', cb);
});
2015-08-14 06:16:54 +02:00
} else {
2015-08-14 20:53:26 +02:00
await fromNode(cb => {
resp
.pipe(createGunzip())
.on('error', cb)
.pipe(new Extract({ path: downloadDir, strip: 1 }))
.on('error', cb)
.on('end', cb);
});
2015-08-14 06:16:54 +02:00
}
2015-08-14 20:53:26 +02:00
await fromNode(cb => {
rename(downloadDir, finalDir, cb);
});
}());
2015-08-14 06:16:54 +02:00
2015-08-14 20:53:26 +02:00
activeDownloads.push(platform);
2015-08-14 06:16:54 +02:00
2015-08-14 20:53:26 +02:00
var bytes = parseInt(resp.headers['content-length'], 10) || 'unknown number of';
var mb = ((bytes / 1024) / 1024).toFixed(2);
grunt.log.ok(`downloading ${platform.name} - ${mb} mb`);
2015-08-14 06:16:54 +02:00
};
2015-08-26 22:35:01 +02:00
grunt.registerTask('_build:downloadNodeBuilds:start', function () {
2015-08-14 20:53:26 +02:00
map(platforms, start).nodeify(this.async());
});
2015-08-14 06:16:54 +02:00
2015-08-26 22:35:01 +02:00
grunt.registerTask('_build:downloadNodeBuilds:finish', function () {
2015-08-14 20:53:26 +02:00
map(activeDownloads, async (platform) => {
await platform.downloadPromise;
grunt.log.ok(`${platform.name} download complete`);
2015-08-14 06:16:54 +02:00
})
.nodeify(this.async());
});
};