11import fs from "node:fs" ;
2+ import process from "node:process" ;
23import { getJson } from "./utils.js" ;
34
45let queryCount = 0 ;
@@ -12,8 +13,8 @@ function printProgress (count, total) {
1213// Function to check whether new data should be retrieved.
1314function shouldFetch ( repository ) {
1415 let retrieve = false ;
15- if ( repository . url . includes ( "github.com" ) ) {
16- if ( queryCount < maxQueryCount ) {
16+ if ( repository . url . includes ( "github.com" ) && maxQueryCount > 0 ) {
17+ if ( queryCount < maxQueryCount || process . env . GITHUB_TOKEN ) {
1718 retrieve = true ;
1819 }
1920 }
@@ -75,6 +76,11 @@ async function updateData () {
7576
7677 sortModuleListByLastUpdate ( previousData , moduleList ) ;
7778
79+ const headers = { } ;
80+ if ( process . env . GITHUB_TOKEN ) {
81+ headers . Authorization = `Bearer ${ process . env . GITHUB_TOKEN } ` ;
82+ }
83+
7884 for ( const module of moduleList ) {
7985 const repositoryId = module . id ;
8086 const repositoryApiUrl = `https://api.github.com/repos/${ repositoryId } ` ;
@@ -85,12 +91,12 @@ async function updateData () {
8591
8692 if ( shouldFetchData ) {
8793 printProgress ( moduleCount , moduleListLength ) ;
88- const response = await fetch ( repositoryApiUrl ) ;
94+ const response = await fetch ( repositoryApiUrl , { headers } ) ;
8995 const data = await response . json ( ) ;
9096 queryCount += 1 ;
9197
9298 const branchUrl = `https://api.github.com/repos/${ repositoryId } /commits/${ data . default_branch } ` ;
93- const branchResponse = await fetch ( branchUrl ) ;
99+ const branchResponse = await fetch ( branchUrl , { headers } ) ;
94100 const branchData = await branchResponse . json ( ) ;
95101 queryCount += 1 ;
96102
@@ -176,6 +182,7 @@ async function updateData () {
176182
177183 fs . writeFileSync ( localFilePath , JSON . stringify ( updateInfo , null , 2 ) ) ;
178184 fs . writeFileSync ( "docs/data/modules.stage.2.json" , JSON . stringify ( sortedModuleList , null , 2 ) ) ;
185+ if ( maxQueryCount < queryCount ) { maxQueryCount = 0 }
179186 console . info ( "\nGitHub data update completed. queryCount:" , queryCount , "maxQueryCount:" , maxQueryCount , "results:" , results . length , "modules:" , moduleListLength ) ;
180187 } catch ( error ) {
181188 console . error ( "Error fetching GitHub API data:" , error ) ;
0 commit comments