67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
|
/*jshint node:true */
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
module.exports = function (grunt) {
|
||
|
grunt.loadNpmTasks('grunt-http');
|
||
|
|
||
|
/**
|
||
|
* Select matching asset from last release in releases list
|
||
|
* from GitHub API
|
||
|
*
|
||
|
* @param {releases: array} Releases list
|
||
|
* @return {string|false} Asset download URL, false if none found
|
||
|
*/
|
||
|
function selectAsset(releases) {
|
||
|
var assets, asset, release,
|
||
|
assetsLength, releasesLength,
|
||
|
name, types = grunt.config.get('platforms'),
|
||
|
type = types[grunt.task.current.args[0]],
|
||
|
i, j;
|
||
|
|
||
|
releasesLength = releases.length;
|
||
|
|
||
|
for (i = 0; i < releasesLength; i += 1) {
|
||
|
release = releases[i];
|
||
|
name = 'atom-shell-' + release.tag_name + '-' + type + '.zip';
|
||
|
|
||
|
// we only want stable releases
|
||
|
if (release.prerelease === false && release.draft === false) {
|
||
|
assets = release.assets;
|
||
|
assetsLength = assets.length;
|
||
|
|
||
|
for (j = 0; j < assetsLength; j += 1) {
|
||
|
asset = assets[j];
|
||
|
|
||
|
if (asset.name === name) {
|
||
|
return asset.browser_download_url;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
grunt.config('http.find-asset', {
|
||
|
options: {
|
||
|
url: '<%= releases %>',
|
||
|
headers: {
|
||
|
'User-Agent': 'Grunt'
|
||
|
},
|
||
|
callback: function (err, res, body) {
|
||
|
if (err) {
|
||
|
throw err;
|
||
|
}
|
||
|
|
||
|
var releases = JSON.parse(body), asset = selectAsset(releases);
|
||
|
|
||
|
if (asset !== false) {
|
||
|
grunt.config.set('asset', asset);
|
||
|
} else {
|
||
|
grunt.fail.warn(new Error('No recent release found'));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
};
|