1919import { createClient } from '@supabase/supabase-js'
2020import { config } from 'dotenv'
2121import { execSync } from 'child_process'
22+ import { readFileSync , existsSync } from 'fs'
23+ import { resolve , dirname } from 'path'
24+ import { fileURLToPath } from 'url'
2225import area from '@turf/area'
2326
2427// Load .env file for local development (GitHub Actions sets env vars directly)
@@ -162,6 +165,47 @@ async function rebuildCache() {
162165 console . log ( '⚙️ Processing service areas...' )
163166 const serviceAreas = buildServiceAreasFromEvents ( events , geometryMap )
164167
168+ // STEP 3.5: Load news.csv if it exists
169+ console . log ( '📰 Loading news data...' )
170+ let newsItems = [ ]
171+ try {
172+ const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
173+ const newsPath = resolve ( __dirname , '..' , 'news.csv' )
174+ if ( existsSync ( newsPath ) ) {
175+ const newsCSV = readFileSync ( newsPath , 'utf-8' )
176+ const lines = newsCSV . trim ( ) . split ( '\n' )
177+ if ( lines . length > 1 ) {
178+ const header = lines [ 0 ] . split ( ',' )
179+ for ( let i = 1 ; i < lines . length ; i ++ ) {
180+ const line = lines [ i ] . trim ( )
181+ if ( ! line ) continue
182+ // Simple CSV parse (handles quoted fields)
183+ const fields = [ ]
184+ let current = '' , inQuotes = false
185+ for ( let j = 0 ; j < line . length ; j ++ ) {
186+ const ch = line [ j ]
187+ if ( inQuotes ) {
188+ if ( ch === '"' && line [ j + 1 ] === '"' ) { current += '"' ; j ++ }
189+ else if ( ch === '"' ) { inQuotes = false }
190+ else { current += ch }
191+ } else {
192+ if ( ch === '"' ) { inQuotes = true }
193+ else if ( ch === ',' ) { fields . push ( current ) ; current = '' }
194+ else { current += ch }
195+ }
196+ }
197+ fields . push ( current )
198+ const newsItem = { }
199+ header . forEach ( ( col , idx ) => { newsItem [ col . trim ( ) ] = ( fields [ idx ] || '' ) . trim ( ) } )
200+ if ( newsItem . headline ) newsItems . push ( newsItem )
201+ }
202+ }
203+ }
204+ console . log ( ` Found ${ newsItems . length } news items` )
205+ } catch ( err ) {
206+ console . warn ( '⚠️ Failed to load news.csv:' , err . message )
207+ }
208+
165209 // STEP 4: Create final data structure
166210 // Transform geometries to camelCase for frontend
167211 const geometriesForFrontend = geometriesWithData . map ( geo => ( {
@@ -188,6 +232,7 @@ async function rebuildCache() {
188232 }
189233 } ,
190234 events : events ,
235+ news : newsItems ,
191236 geometries : geometriesForFrontend ,
192237 service_areas : serviceAreas ,
193238 date_range : {
0 commit comments