11import { defineCollection , reference , z } from "astro:content" ;
2- import fs from "fs/promises" ;
3- import path from "path" ;
2+ import { loadData } from "../utils/dataLoader" ;
43
5- const CACHE_DIR = ".cache/data" ;
6-
7- async function ensureCacheDir ( ) {
8- await fs . mkdir ( CACHE_DIR , { recursive : true } ) ;
9- }
10-
11- async function fetchWithCache ( url : string , filename : string ) : Promise < Buffer > {
12- const filePath = path . join ( CACHE_DIR , filename ) ;
13-
14- try {
15- // Return cached if available
16- const data = await fs . readFile ( filePath ) ;
17- console . log ( `Fetch from cache: ${ filePath } ` ) ;
18- return data ;
19- } catch {
20- const res = await fetch ( url ) ;
21- if ( ! res . ok ) throw new Error ( `Failed to fetch: ${ url } ` ) ;
22-
23- const arrayBuffer = await res . arrayBuffer ( ) ;
24- const buffer : Buffer = Buffer . from ( arrayBuffer ) ;
25- await fs . writeFile ( filePath , new Uint8Array ( buffer ) ) ;
26- return buffer ;
27- }
28- }
4+ const mode = import . meta. env . MODE ;
5+ console . log ( `\x1b[35m[EP]\x1b[0m Current MODE: \x1b[1m\x1b[34m${ mode } \x1b[0m` ) ;
296
307const tiers = [
318 "Keystone" ,
@@ -83,32 +60,19 @@ const keynoters = defineCollection({
8360
8461// Shared data fetching function
8562async function getCollectionsData ( ) {
86- // Only fetch if not already cached
87- await ensureCacheDir ( ) ;
88-
89- const speakersBuffer = await fetchWithCache (
90- "https://gist.githubusercontent.com/nikoshell/d8efd41f90961cc6298519c0eec04843/raw/d13a7b1d35f61be1773404e7faf8395dd4862313/speakers.json" ,
91- "speakers.json"
92- ) ;
93-
94- const sessionsBuffer = await fetchWithCache (
95- "https://gist.githubusercontent.com/egeakman/eddfb15f32ae805e8cfb4c5856ae304b/raw/466f8c20c17a9f6c5875f973acaec60e4e4d0fae/sessions.json" ,
96- "sessions.json"
97- ) ;
98-
99- const cachedSpeakersData = JSON . parse ( speakersBuffer . toString ( "utf-8" ) ) ;
100- const cachedSessionsData = JSON . parse ( sessionsBuffer . toString ( "utf-8" ) ) ;
63+ const speakersData = await loadData ( import . meta. env . EP_SPEAKERS_API ) ;
64+ const sessionsData = await loadData ( import . meta. env . EP_SESSIONS_API ) ;
10165
10266 // Create indexed versions for efficient lookups
103- const speakersById = Object . entries ( cachedSpeakersData ) . reduce (
67+ const speakersById = Object . entries ( speakersData ) . reduce (
10468 ( acc , [ id , speaker ] : [ string , any ] ) => {
10569 acc [ id ] = { id, ...speaker } ;
10670 return acc ;
10771 } ,
10872 { } as Record < string , any >
10973 ) ;
11074
111- const sessionsById = Object . entries ( cachedSessionsData ) . reduce (
75+ const sessionsById = Object . entries ( sessionsData ) . reduce (
11276 ( acc , [ id , session ] : [ string , any ] ) => {
11377 acc [ id ] = { id, ...session } ;
11478 return acc ;
@@ -117,8 +81,8 @@ async function getCollectionsData() {
11781 ) ;
11882
11983 return {
120- speakersData : cachedSpeakersData ,
121- sessionsData : cachedSessionsData ,
84+ speakersData,
85+ sessionsData,
12286 speakersById,
12387 sessionsById,
12488 } ;
0 commit comments