-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (62 loc) · 1.56 KB
/
index.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
/**
* Module dependencies.
*/
var request = require('superagent')
, Batch = require('batch')
, jquery = require('cheerio').load;
/**
* Wiki url.
*/
var wiki = 'https://github.com/component/component/wiki/Components';
/**
* Remove url.
*/
var remote = 'https://github.com';
/**
* Fetch component.json files and invoke `fn(err, pkgs)`.
*
* @param {Function} fn
* @return {Batch}
* @api public
*/
module.exports = function(fn){
var batch = new Batch;
request
.get(wiki)
.end(function(err, res){
if (err) return fn(err);
var $ = jquery(res.text);
$('#wiki-body h2 + ul li').each(function(){
var a = $(this).find('a');
var href = a.attr('href').replace(/\/$/, '') // remove trailing slash
var url = href.replace('://', '://raw.') + '/master/component.json';
batch.push(function(done){
request
.get(url)
.end(function(err, res){
if (err || !res.ok) {
console.warn('failed %s (%d)', url, res && res.status);
return done();
}
try {
var obj = JSON.parse(res.text);
if (!obj.repo) {
var repo = a.text();
console.warn('"repo" missing for %s', repo);
// TODO: remove and warn user via GH api
obj.repo = repo;
}
done(null, obj);
} catch (err) {
err.url = url;
err.json = true;
batch.emit('error', err);
done();
}
});
});
});
batch.end(fn);
});
return batch;
};