Files
j_kedzierski/scripts/fix_all_home.ts
T

125 lines
3.9 KiB
TypeScript

import { getPayload } from 'payload'
import configPromise from '../src/payload.config'
import fs from 'fs'
import path from 'path'
// Helper to remove ids deeply from layout blocks so Payload recreates them
function removeIds(obj: any): any {
if (Array.isArray(obj)) return obj.map(removeIds)
if (obj !== null && typeof obj === 'object') {
const newObj: any = {}
for (const key of Object.keys(obj)) {
if (key !== 'id') {
newObj[key] = removeIds(obj[key])
}
}
return newObj
}
return obj
}
async function run() {
const payload = await getPayload({ config: configPromise })
// 1. Ensure we have an uploaded image to use
const mediaDir = path.join(process.cwd(), 'media')
const files = fs.readdirSync(mediaDir).filter(f => f.endsWith('.webp'))
let heroImageId: number | null = null
let aboutImageId: number | null = null
if (files.length > 0) {
try {
const created1 = await payload.create({
collection: 'media',
data: { alt: 'Hero background' },
file: {
data: fs.readFileSync(path.join(mediaDir, files[0])),
mimetype: 'image/webp',
name: 'hero-bg.webp',
size: fs.statSync(path.join(mediaDir, files[0])).size
}
})
heroImageId = created1.id as number
console.log(`Uploaded hero image ID: ${heroImageId}`)
if (files.length > 1) {
const created2 = await payload.create({
collection: 'media',
data: { alt: 'About section image' },
file: {
data: fs.readFileSync(path.join(mediaDir, files[1])),
mimetype: 'image/webp',
name: 'about-img.webp',
size: fs.statSync(path.join(mediaDir, files[1])).size
}
})
aboutImageId = created2.id as number
console.log(`Uploaded about image ID: ${aboutImageId}`)
}
} catch(e) {
console.error("Media upload error", e)
}
}
// 2. Load original layouts from snapshot
const snapshotPath = path.join(process.cwd(), 'scripts', 'content-snapshot.json')
const snapshot = JSON.parse(fs.readFileSync(snapshotPath, 'utf-8'))
const homeSnapshot = snapshot.pages.find((p: any) => p.pl.slug === 'home')
if (!homeSnapshot) {
console.error("Home snapshot not found")
process.exit(1)
}
const homeDoc = await payload.find({ collection: 'pages', where: { slug: { equals: 'home' } }, locale: 'pl' })
if (!homeDoc.docs[0]) {
console.error("Home page not found in DB")
process.exit(1)
}
const homeId = homeDoc.docs[0].id
const updateLayout = async (loc: 'pl' | 'de' | 'en', baseLoc: 'pl' | 'de') => {
let layout = removeIds(homeSnapshot[baseLoc].layout)
// Inject images and EN translations
layout = layout.map((block: any) => {
if (block.blockType === 'hero') {
block.image = heroImageId
if (loc === 'en') {
block.title = 'Your Trusted Accounting Office in Germany'
block.titleAccent = 'We speak English and Polish'
block.subtitle = 'Professional bookkeeping, tax settlements, and business consulting for companies in Germany.'
block.primaryCta = 'Contact Us'
block.secondaryCta = { text: 'Our Services', url: '/uslugi' }
}
} else if (block.blockType === 'about') {
block.image = aboutImageId || heroImageId
}
return block
})
try {
await payload.update({
collection: 'pages',
id: homeId,
locale: loc,
data: {
title: loc === 'en' ? 'Accounting Services in Germany' : homeSnapshot[baseLoc].title,
layout
}
})
console.log(`Successfully fixed home page layout for ${loc}`)
} catch(e) {
console.error(`Failed to fix ${loc} home page`, e)
}
}
await updateLayout('pl', 'pl')
await updateLayout('de', 'de')
await updateLayout('en', 'pl') // Clone EN from PL base
process.exit(0)
}
run().catch(console.error)