@@ -4,13 +4,31 @@ import grayMatter from "gray-matter"
4
4
import {
5
5
getGitHubStats ,
6
6
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
8
13
9
14
async function main ( ) {
10
- const filePaths = await fg ( "./src/code/ **/*.md" )
15
+ const filePaths = await fg ( "./**/*.md" , { cwd : CODE_DIR , absolute : true } )
11
16
12
17
const errors : Error [ ] = [ ]
13
18
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
+
14
32
const newState = new Map < string /* repo name */ , GitHubInfo > ( )
15
33
const filePathToRepoName = new Map <
16
34
string /* file path */ ,
@@ -57,8 +75,7 @@ async function main() {
57
75
// If it errored for some reason, we don't do anything.
58
76
// If we got it, we overwrite.
59
77
{
60
- const dataPath = "./src/github-stats.json"
61
- const data = await fs . readFile ( dataPath , "utf8" )
78
+ const data = await fs . readFile ( DATA_PATH , "utf8" )
62
79
const existingStats = JSON . parse ( data ) as Record < string , object >
63
80
64
81
const result : Record < string , object > = { }
@@ -79,7 +96,8 @@ async function main() {
79
96
result [ repoName ] = newState . get ( repoName ) !
80
97
}
81
98
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 ( ) )
83
101
}
84
102
}
85
103
0 commit comments