forked from XamSome/awesome-xamarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-stars.js
64 lines (57 loc) · 1.73 KB
/
update-stars.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
'use strict';
var fs = require('fs'), https = require('https');
var readme = fs.readFileSync('README.md', {encoding: 'utf8'});
var regex = /\[\**([^\]]*)★[^\]]*\**\]\((https?:\/\/github.com\/([A-z0-9\-]+\/[A-z0-9\-]+))\)/g
var stars = {};
var promises = [];
var agent = new https.Agent({maxSockets: 1, keepAlive: true});
var requestOptions = {
hostname: 'api.github.com',
auth: 'benoitjadinon:6d5082698188d0863565c74b8b3a52d9a1468cd4',
headers: {'User-Agent':'benoitjadinon'},
method: 'GET',
agent: agent
};
var match;
while ((match = regex.exec(readme)) !== null) {
var promise = new Promise(function(resolve, reject) {
var repo = match[3].slice();
var options = Object.assign({}, requestOptions);
options.path = '/repos/' + repo;
https.request(options, function(res) {
if (res.statusCode != 200) {
resolve();
return;
}
var body = '';
res.on('data', function(chunk) {
body += chunk.toString('utf8');
});
res.on('end', function() {
stars[repo] = JSON.parse(body).stargazers_count;
console.log(repo + ' ' + stars[repo]);
resolve();
});
}).on('error', function(err) {
resolve();
}).end();
});
promises.push(promise);
}
Promise.all(promises).then(function() {
console.log('Writing README...');
readme = readme.replace(regex, function(match, description, url, repo) {
if (stars[repo] === undefined) { return match; }
var text = '[';
if (stars[repo] > 100) {
text += '**';
}
text += description + "★" + stars[repo].toLocaleString();
if (stars[repo] > 100) {
text += '**';
}
text += '](' + url + ')';
return text;
});
fs.writeFileSync('README.md', readme);
});