1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/packages/tools/update-readme-contributors.js

101 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-07-18 19:36:29 +02:00
'use strict';
const request = require('request');
2020-11-09 21:36:49 +02:00
const readmePath = `${__dirname}/../../README.md`;
2019-07-18 19:36:29 +02:00
const { insertContentIntoFile } = require('./tool-utils.js');
async function gitHubContributors(page) {
return new Promise((resolve, reject) => {
request.get({
2019-09-19 23:51:18 +02:00
url: `https://api.github.com/repos/laurent22/joplin/contributors${page ? `?page=${page}` : ''}`,
2019-07-18 19:36:29 +02:00
json: true,
headers: { 'User-Agent': 'Joplin Readme Updater' },
2019-07-18 19:36:29 +02:00
}, (error, response, data) => {
if (error) {
reject(error);
} else if (response.statusCode !== 200) {
2019-09-19 23:51:18 +02:00
reject(new Error(`Error HTTP ${response.statusCode}`));
2019-07-18 19:36:29 +02:00
} else {
resolve(data);
}
});
});
}
function contributorTable(contributors) {
const rows = [];
2019-07-18 19:36:29 +02:00
let row = [];
rows.push(row);
const rowLength = 5;
2019-07-18 19:36:29 +02:00
let contributorIndex = 0;
while (contributorIndex < contributors.length) {
const c = contributors[contributorIndex];
contributorIndex++;
2020-11-09 21:36:49 +02:00
const cell = `<img width="50" src="${c.avatar_url}"/></br>[${c.login}](${c.html_url})`;
2019-07-18 19:36:29 +02:00
row.push(cell);
if (row.length >= rowLength) {
row = [];
rows.push(row);
}
}
while (rows[rows.length - 1].length < rowLength) rows[rows.length - 1].push('');
const header = [];
const headerLine = [];
for (let i = 0; i < rowLength; i++) {
header.push(' ');
headerLine.push(':---:');
}
const lines = [];
2019-09-19 23:51:18 +02:00
lines.push(`| ${header.join(' | ')} |`);
lines.push(`| ${headerLine.join(' | ')} |`);
2019-07-18 19:36:29 +02:00
for (const row of rows) {
2019-09-19 23:51:18 +02:00
lines.push(`| ${row.join(' | ')} |`);
2019-07-18 19:36:29 +02:00
}
return lines.join('\n');
}
async function main() {
2019-07-18 19:36:29 +02:00
let contributors = [];
let pageIndex = 0;
const doneNames = [];
while (true) {
const response = await gitHubContributors(pageIndex);
pageIndex++;
if (!response.length) break;
// Remove duplicates
const temp = [];
for (const r of response) {
if (doneNames.indexOf(r.login) >= 0) continue;
doneNames.push(r.login);
temp.push(r);
}
contributors = contributors.concat(temp);
}
const tableHtml = contributorTable(contributors);
await insertContentIntoFile(
readmePath,
'<!-- CONTRIBUTORS-TABLE-AUTO-GENERATED -->\n',
'\n<!-- CONTRIBUTORS-TABLE-AUTO-GENERATED -->',
tableHtml
);
}
main(process.argv).catch((error) => {
console.error('Fatal error', error);
process.exit(1);
});