|
1 | 1 |
|
| 2 | +const fs = require("fs"); |
| 3 | +const fetch = require("node-fetch"); |
| 4 | + |
| 5 | +const username = "erasmusedwardobeth"; // <-- Your GitHub username |
| 6 | +const sortBy = "updated"; // change to "created" or "updated" |
| 7 | + |
| 8 | +// Capitalize first letter of each word |
| 9 | +function capitalizeWords(str) { |
| 10 | + return str.replace(/\b\w/g, char => char.toUpperCase()); |
| 11 | +} |
| 12 | + |
| 13 | +async function getRepos() { |
| 14 | + const res = await fetch(`https://api.github.com/users/${username}/repos?per_page=100&sort=${sortBy}`); |
| 15 | + const repos = await res.json(); |
| 16 | + |
| 17 | + // Filter only FCC repos |
| 18 | + const fccRepos = repos.filter(r => r.name.startsWith("fcc-")); |
| 19 | + |
| 20 | + // Skip empty repos (0 commits) |
| 21 | + const checkedRepos = []; |
| 22 | + for (let repo of fccRepos) { |
| 23 | + const commitsRes = await fetch(repo.commits_url.replace("{/sha}", "")); |
| 24 | + const commits = await commitsRes.json(); |
| 25 | + if (Array.isArray(commits) && commits.length > 0) { |
| 26 | + checkedRepos.push(repo); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return checkedRepos; |
| 31 | +} |
| 32 | + |
| 33 | +function buildTable(repos) { |
| 34 | + let table = "| Project | Live Demo | Code | Badges | Description |\n"; |
| 35 | + table += "|---------|-----------|------|--------|-------------|\n"; |
| 36 | + |
| 37 | + repos.forEach(repo => { |
| 38 | + const rawName = repo.name.replace("fcc-", "").replace(/-/g, " "); |
| 39 | + const name = capitalizeWords(rawName); |
| 40 | + |
| 41 | + const demo = `https://${username}.github.io/${repo.name}`; |
| 42 | + const code = repo.html_url; |
| 43 | + |
| 44 | + // Badges for repo |
| 45 | + const badges = ` |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + `.trim(); |
| 50 | + |
| 51 | + const desc = repo.description || "FreeCodeCamp project"; |
| 52 | + |
| 53 | + table += `| ${name} | [Demo](${demo}) | [Repo](${code}) | ${badges} | ${desc} |\n`; |
| 54 | + }); |
| 55 | + |
| 56 | + return table; |
| 57 | +} |
| 58 | + |
| 59 | +async function updateReadme() { |
| 60 | + const readme = fs.readFileSync("README.md", "utf8"); |
| 61 | + const repos = await getRepos(); |
| 62 | + const table = buildTable(repos); |
| 63 | + |
| 64 | + const newReadme = readme.replace( |
| 65 | + /<!-- PROJECTS_TABLE_START -->([\s\S]*?)<!-- PROJECTS_TABLE_END -->/, |
| 66 | + `<!-- PROJECTS_TABLE_START -->\n${table}\n<!-- PROJECTS_TABLE_END -->` |
| 67 | + ); |
| 68 | + |
| 69 | + fs.writeFileSync("README.md", newReadme); |
| 70 | +} |
| 71 | + |
| 72 | +updateReadme(); |
0 commit comments