Skip to content

Commit 0455e0e

Browse files
authored
improve the GitHub star sync script (#2134)
* Move to scripts directory * Don't repeat GitHub star syncs more often than every two hours
1 parent a867640 commit 0455e0e

File tree

7 files changed

+499
-472
lines changed

7 files changed

+499
-472
lines changed

.prettierignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ pnpm-lock.yaml
99
!src/pages/blog/2025-06-19-multioption-inputs-with-oneof/index.mdx
1010
*.jpg
1111

12-
scripts/sync-sched/*.json
13-
src/github-stats.json
12+
scripts/**/*.json

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"lint:docs": "eslint --ignore-path .gitignore src/pages/learn --format stylish",
1616
"lint:docs:ci": "eslint --ignore-path .gitignore src/pages/learn --format eslint-formatter-github",
1717
"postbuild": "next-sitemap",
18-
"prebuild": "tsx src/get-github-info.ts",
18+
"prebuild": "tsx scripts/get-github-info",
1919
"serve": "pnpx serve out",
2020
"test": "playwright test && pnpm test:unit",
2121
"test:e2e": "playwright test",

src/get-github-info.ts renamed to scripts/get-github-info/get-github-info.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,31 @@ import grayMatter from "gray-matter"
44
import {
55
getGitHubStats,
66
type GitHubInfo,
7-
} from "../scripts/sort-libraries/get-github-stats"
7+
} from "../sort-libraries/get-github-stats"
8+
9+
const DATA_PATH = new URL("./github-stats.json", import.meta.url).pathname
10+
const LAST_RUN_PATH = new URL("./last-success.isodate", import.meta.url)
11+
.pathname
12+
const CODE_DIR = new URL("../../src/code", import.meta.url).pathname
813

914
async function main() {
10-
const filePaths = await fg("./src/code/**/*.md")
15+
const filePaths = await fg("./**/*.md", { cwd: CODE_DIR, absolute: true })
1116

1217
const errors: Error[] = []
1318

19+
{
20+
// we only sync once every two hours
21+
const TWO_HOURS = 2 * 60 * 60 * 1000
22+
const lastRun = await fs.readFile(LAST_RUN_PATH, "utf8").catch(() => "")
23+
const twoHoursAgo = new Date(Date.now() - TWO_HOURS)
24+
if (lastRun && new Date(lastRun).getTime() > twoHoursAgo.getTime()) {
25+
console.info(
26+
"Skipping sync of GitHub stars, last run was within two hours.",
27+
)
28+
return
29+
}
30+
}
31+
1432
const newState = new Map<string /* repo name */, GitHubInfo>()
1533
const filePathToRepoName = new Map<
1634
string /* file path */,
@@ -57,8 +75,7 @@ async function main() {
5775
// If it errored for some reason, we don't do anything.
5876
// If we got it, we overwrite.
5977
{
60-
const dataPath = "./src/github-stats.json"
61-
const data = await fs.readFile(dataPath, "utf8")
78+
const data = await fs.readFile(DATA_PATH, "utf8")
6279
const existingStats = JSON.parse(data) as Record<string, object>
6380

6481
const result: Record<string, object> = {}
@@ -79,7 +96,8 @@ async function main() {
7996
result[repoName] = newState.get(repoName)!
8097
}
8198

82-
await fs.writeFile(dataPath, JSON.stringify(result, null, 2))
99+
await fs.writeFile(DATA_PATH, JSON.stringify(result, null, 2))
100+
await fs.writeFile(LAST_RUN_PATH, new Date().toISOString())
83101
}
84102
}
85103

0 commit comments

Comments
 (0)