代码拉取完成,页面将自动刷新
'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);
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。