@@ -4,13 +4,31 @@ import grayMatter from "gray-matter"
44import {
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
914async 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