-
Notifications
You must be signed in to change notification settings - Fork 84
/
grunt-plugins.js
104 lines (86 loc) · 2.45 KB
/
grunt-plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* grunt-plugins.js - gets the plugin list from npm
* server.js serves the plugin list via the website.
*/
'use strict';
var request = require('request');
var async = require('async');
var fs = require('fs');
require('date-utils');
var pluginFile = 'src/plugin-list.json';
function requestIndex(url, callback) {
setTimeout(function() {
request({
url: url,
json: true
}, function handlePluginList(error, response, body) {
if (!error && response.statusCode === 200) {
return callback(null, body.objects);
} else {
console.error(error);
return callback(new Error(error), null);
}
});
}, Math.random()* 10000);
}
function getPlugins(opts, callback) {
console.log('Downloading plugin list, this will take some time...');
var query = [];
for(var i = 0; i < 25; i++) {
var url = 'http://registry.npmjs.com/-/v1/search?text=keywords:gruntplugin&size=250&popularity=1.0&quality=1.0&from=' + i * 250;
query.push(url);
}
async.concat(query, requestIndex, function(err, results) {
console.log('Got ' + results.length + ' plugins');
callback(results, err);
});
}
function download(opts, callback) {
opts = opts || {};
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
fs.stat(pluginFile, function (err) {
if (err || !opts.cache) {
if (err) {
console.log('File missing...');
}
getPlugins(opts, function (results) {
console.log('Saving to file...');
var plugins = [];
results.forEach(function (result) {
plugins.push({
package: {
name: result.package.name,
author: result.package.author,
description: result.package.description,
links: {
npm: result.package.links.npm
}
},
score: {
detail: {
popularity: result.score.detail.popularity
}
}
});
});
var pluginData = JSON.stringify({'aaData': plugins});
fs.writeFile(pluginFile, pluginData, function (err) {
if (err) {
throw err;
}
console.log('Saved!');
if (callback) {
callback(err, true);
}
});
});
} else {
console.log('File already cached. Manually delete to redownload...');
callback();
}
});
}
module.exports.download = download;