forked from gruntjs/gruntjs.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrunt-plugins.js
156 lines (133 loc) · 4.38 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* grunt-plugins.js - gets the plugin list from npm
* server.js serves the plugin list via the website.
*/
var request = require('request');
var _ = require('lodash');
var async = require('async');
var ent = require('ent');
var fs = require('fs');
require('date-utils');
// A list of grunt plugins that have wronged!
var bannedPlugins = [
'grunt-contrib-jst-2', // Reason: unofficial contrib plugin
'loadnpmtasks', // Reason: not a plugin
'dp-grunt-contrib-copy',
'grunt-test',
'grunt-testingoscar123',
'assemble-less-variables',
'grunt-contrib-eslint'
];
var pluginFile = 'build/plugin-list.json';
function getPlugin(item, callback) {
var name = item.key[1];
var url = 'https://skimdb.npmjs.com/registry/' + name;
request({url: url, json: true}, function handlePlugin(error, response, body) {
if (!error && response.statusCode == 200) {
callback(null, condensePlugin(body));
} else {
console.log('Failed to get data for:', name);
callback(null, null);
}
});
}
function getDownloads(item, callback) {
var name = item.name;
var url = 'https://api.npmjs.org/downloads/point/last-month/' + name;
request({url: url, json: true}, function handlePlugin(error, response, body) {
if (!error && response.statusCode == 200) {
if (body.downloads) {
item.dl = body.downloads;
} else {
item.dl = 0;
}
callback(null, item);
} else {
callback(null, item);
}
});
}
function condensePlugin(plugin) {
var keywords = _.last(_.values(plugin.versions)).keywords;
var gruntVersion,
latestTagInfo = plugin.versions[ plugin['dist-tags'].latest ];
if (latestTagInfo && latestTagInfo.peerDependencies && latestTagInfo.peerDependencies.grunt) {
gruntVersion = latestTagInfo.peerDependencies.grunt;
}
return {
name: plugin.name,
ds: plugin.description != null ? ent.encode(plugin.description) : '',
a: (plugin.author && plugin.author.name != null) ? plugin.author.name : '',
url: plugin.url,
v: gruntVersion,
// only get created and modified date, leave out all of the version timestamps
m: plugin.time.modified
//created: plugin.time.created
};
}
function getPlugins(opts, callback) {
console.log('Downloading plugin list, this will take 40 or more seconds...');
async.waterfall([
// download plugin names based on the keyword
function(callback) {
var keyword = 'gruntplugin';
var url = 'https://skimdb.npmjs.com/registry/_design/app/_view/byKeyword?startkey=[%22' +
keyword + '%22]&endkey=[%22' + keyword + '%22,{}]&group_level=3';
request({url: url, json: true}, function handlePluginList(error, response, body) {
if (!error && response.statusCode == 200) {
callback(null, body.rows);
} else {
callback(null, new Error(error));
}
});
},
function(results, callback){
console.log('Downloading npm data for each plugin...');
var filtered = _.filter(results, function (el) {
return _.indexOf(bannedPlugins, el.key[1]) == -1;
});
async.mapLimit(filtered, 200, getPlugin, function(err, results){
// registry can be out of sync with deleted plugins
var results = _.reject(results, function(plugin) { return plugin === null; });
callback(err, results);
});
},
function(results, callback){
console.log('Fetching download information...');
async.mapLimit(results, 200, getDownloads, function(err, results){
callback(err, results);
});
}
], function (err, pluginList) {
if (err) {
console.log(err);
} else {
//console.log('Downloading GitHub data for each plugin...');
console.log('Saving to file...');
var pluginData = JSON.stringify({ "aaData": pluginList });
fs.writeFile(pluginFile, pluginData, function (err) {
if (err) throw err;
console.log('Saved!');
if (callback) callback(err, true);
});
}
});
}
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, callback);
} else {
console.log('File already cached. Manually delete to redownload...');
}
});
}
module.exports.download = download;