Last active
November 7, 2023 01:36
-
-
Save SgtPooki/125b0f30d12bc19fc799e78c317955e1 to your computer and use it in GitHub Desktop.
This script requires a markdown table for input, as generated by https://github.com/nvuillam/github-dependents-info. It compares the current table with the previous one and outputs a new table with the changes in stars.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* This script requires a markdown table for input, as generated by https://github.com/nvuillam/github-dependents-info | |
* It compares the current table with the previous one and outputs a new table with the changes in stars. | |
* Usage: | |
* github-dependents-info --repo ipfs/helia --markdownfile ./package-usage-2023-09.md --sort stars --verbose -x 2 -p | |
* // manually copy the Repostory and Stars table to `current.md` | |
* // manually copy the Repostory and Stars table from the previous month to `previous.md` | |
* node dependents-change.js | |
* | |
* This is used to generate the table in Helia reports, like at https://pl-strflt.notion.site/Helia-Report-2023-09-892d8510e49549b6b96fd87323c0c715#2b2d82cd734e45fab4c6b6549cb06e2c | |
*/ | |
import fs from 'node:fs' | |
// read the previous table from `previous.md` | |
const previousMonthTable = fs.readFileSync('previous.md', 'utf8'); | |
const currentMonthTable = fs.readFileSync('current.md', 'utf8'); | |
// Function to parse a Markdown table and create an object | |
function parseMarkdownTable(table) { | |
const lines = table.trim().split('\n'); | |
const header = lines[1].split('|').map(cell => cell.trim()).filter(Boolean); | |
const data = {}; | |
for (let i = 2; i < lines.length; i++) { | |
const row = lines[i].split('|').map(cell => cell.trim()).filter(Boolean); | |
if (row.length === header.length) { | |
data[row[0].toLowerCase()] = parseInt(row[1].toLowerCase()); | |
} | |
} | |
// make sure all keys are github urls only so we can compare them correctly. | |
// i.e. convert from something like `[linkName](url)` to just `url` | |
for (const key in data) { | |
const match = key.match(/\[.*\]\((.*)\)/); | |
if (match) { | |
data[match[1]] = data[key]; | |
delete data[key]; | |
} | |
} | |
return data; | |
} | |
// Parse the tables and create objects | |
const previousMonthData = parseMarkdownTable(previousMonthTable); | |
const currentMonthData = parseMarkdownTable(currentMonthTable); | |
// Find new repositories and changes in stars | |
const changesInStars = {}; | |
for (const repository in previousMonthData) { | |
if (repository in currentMonthData) { | |
const starChange = currentMonthData[repository] - previousMonthData[repository]; | |
changesInStars[repository] = starChange; | |
} | |
} | |
// Prepare and log the result in Markdown format with clickable links | |
console.log('| Repository | Stars | Change |'); | |
console.log('| --- | --- | --- |'); | |
for (const repository in currentMonthData) { | |
const starsCurrent = currentMonthData[repository]; | |
const change = changesInStars[repository] ?? 'NEW'; | |
const repoName = repository.replace('https://github.com/', '').replace('/', '/'); | |
const repoLink = `[${repoName}](${repository})`; | |
console.log(`| ${repoLink} | ${starsCurrent} | ${change} |`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "@SgtPooki/dependents-change", | |
"version": "1.0.1", | |
"bin": "./dependents-change.js", | |
"type": "module" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
October report
current.md
output: