www_ourworld_new/scripts/generate-people-data.js
2025-07-22 15:09:30 +02:00

128 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
// Paths
const peopleComponentsDir = path.join(__dirname, '../src/components/people')
const peopleDataFile = path.join(__dirname, '../src/lib/peopleData.ts')
// Function to get all People_*.tsx files
function getPeopleComponents() {
if (!fs.existsSync(peopleComponentsDir)) {
console.log('People components directory not found')
return []
}
const files = fs.readdirSync(peopleComponentsDir)
return files
.filter(file => file.startsWith('People_') && file.endsWith('.tsx'))
.map(file => {
const componentName = file.replace('.tsx', '')
const importName = componentName.toLowerCase().replace('people_', '')
return {
fileName: file,
componentName,
importName,
importPath: `@/components/people/${componentName}`
}
})
}
// Function to generate the peopleData.ts content
function generatePeopleDataContent(components) {
const imports = components
.map(comp => `import { data as ${comp.importName}Data } from '${comp.importPath}'`)
.join('\n')
const dynamicImports = components
.map(comp => ` () => import('${comp.importPath}'),`)
.join('\n')
const syncDataPush = components
.map(comp => `
try {
allPeopleData.push(...${comp.importName}Data)
} catch (error) {
console.error('Error loading ${comp.importName} data:', error)
}`)
.join('\n')
return `// This file is auto-generated by scripts/generate-people-data.js
// When new people components are added, run: npm run generate-people-data
export interface PersonData {
name: string
role: string
imageUrl: string
xUrl: string
linkedinUrl: string
}
// Function to dynamically get all people data
export async function getAllPeopleData(): Promise<PersonData[]> {
const allPeopleData: PersonData[] = []
// Auto-generated list of all people components
const peopleComponents = [
${dynamicImports}
]
for (const importComponent of peopleComponents) {
try {
const module = await importComponent()
if (module.data && Array.isArray(module.data)) {
allPeopleData.push(...module.data)
}
} catch (error) {
console.error('Error loading people data:', error)
}
}
return allPeopleData
}
// Synchronous version using static imports for immediate data access
${imports}
export function getAllPeopleDataSync(): PersonData[] {
const allPeopleData: PersonData[] = []
${syncDataPush}
return allPeopleData
}
`
}
// Main function
function main() {
console.log('🔍 Scanning for people components...')
const components = getPeopleComponents()
if (components.length === 0) {
console.log('❌ No people components found')
return
}
console.log(`✅ Found ${components.length} people component(s):`)
components.forEach(comp => {
console.log(` - ${comp.componentName}`)
})
console.log('📝 Generating peopleData.ts...')
const content = generatePeopleDataContent(components)
fs.writeFileSync(peopleDataFile, content, 'utf8')
console.log('✅ Successfully updated src/lib/peopleData.ts')
console.log('\n🎉 All people components will now automatically appear on the people page!')
}
// Run the script
if (require.main === module) {
main()
}
module.exports = { main, getPeopleComponents, generatePeopleDataContent }