Files
j_kedzierski/scripts/migrate_first_person.ts

140 lines
4.8 KiB
TypeScript

import { getPayload } from 'payload'
import configPromise from '../src/payload.config'
import fs from 'fs'
import path from 'path'
const replacements: { old: string, new: string }[] = [
// PL
{ old: 'Przeprowadzę Cię', new: 'Przeprowadzimy Cię' },
{ old: 'Czym się zajmuję', new: 'Czym się zajmujemy' },
{ old: 'Działam w ramach', new: 'Działamy w ramach' },
{ old: 'Napisz do mnie', new: 'Napisz do nas' },
{ old: 'Odpowiem najszybciej', new: 'Odpowiemy najszybciej' },
{ old: 'Zapewniam rzetelne', new: 'Zapewniamy rzetelne' },
{ old: 'Zapewniam pełne', new: 'Zapewniamy pełne' },
{ old: 'Skontaktuj się ze mną', new: 'Skontaktuj się z nami' },
{ old: 'Przygotowuję kompletną', new: 'Przygotowujemy kompletną' },
{ old: 'wyliczam przewidywany', new: 'wyliczamy przewidywany' },
{ old: 'przesyłam gotowy', new: 'przesyłamy gotowy' },
{ old: 'a zajmę się', new: 'a zajmiemy się' },
{ old: 'Przeanalizuję Twoją', new: 'Przeanalizujemy Twoją' },
{ old: 'pomogę uzyskać', new: 'pomożemy uzyskać' },
{ old: 'chętnie pomogę', new: 'chętnie pomożemy' },
// DE
{ old: 'Was ich anbiete', new: 'Was wir anbieten' },
{ old: 'Ich bin im Rahmen', new: 'Wir sind im Rahmen' },
{ old: 'Schreiben Sie mir jetzt', new: 'Schreiben Sie uns jetzt' },
{ old: 'Ich werde so schnell', new: 'Wir werden so schnell' },
{ old: 'Schreiben Sie mir direkt', new: 'Schreiben Sie uns direkt' },
{ old: 'Ich erstelle Ihre', new: 'Wir erstellen Ihre' },
{ old: 'berechne die', new: 'berechnen die' },
{ old: 'übermittle Ihnen', new: 'übermitteln Ihnen' },
{ old: 'Kontaktieren Sie mich jetzt', new: 'Kontaktieren Sie uns jetzt' },
{ old: 'Kontaktieren Sie mich noch', new: 'Kontaktieren Sie uns noch' },
{ old: 'Ich analysiere Ihre', new: 'Wir analysieren Ihre' },
{ old: 'und unterstütze Sie', new: 'und unterstützen Sie' },
{ old: 'Kontaktieren Sie mich — ich helfe', new: 'Kontaktieren Sie uns — wir helfen' },
]
function applyReplacements(text: string): string {
let result = text
for (const rep of replacements) {
result = result.replace(new RegExp(rep.old, 'g'), rep.new)
}
return result
}
function processObject(obj: any): any {
if (Array.isArray(obj)) {
return obj.map(item => processObject(item))
}
if (obj !== null && typeof obj === 'object') {
if (obj.blockType === 'bio') {
return obj // pomiń Bio
}
const newObj: any = {}
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'string') {
newObj[key] = applyReplacements(value)
} else {
newObj[key] = processObject(value)
}
}
return newObj
}
return obj
}
async function run() {
console.log('Rozpoczynam migrację z pierwszej na pierwszą osobę liczby mnogiej...')
const payload = await getPayload({ config: configPromise })
// 1. Aktualizacja zawartości w bazie
console.log('--- Aktualizowanie bazy danych ---')
for (const locale of ['pl', 'de'] as const) {
// Pages
const pages = await payload.find({
collection: 'pages',
locale,
limit: 100
})
for (const page of pages.docs) {
if (!page.layout) continue
const newLayout = processObject(page.layout)
// Optymalizacja: sprawdźmy czy layout się zmienił po stringify
if (JSON.stringify(page.layout) !== JSON.stringify(newLayout)) {
console.log(`[${locale}] Aktualizacja strony: ${page.title} (ID: ${page.id})`)
await payload.update({
collection: 'pages',
id: page.id,
locale,
data: { layout: newLayout }
})
}
}
// Globals (footer)
const footer = await payload.findGlobal({
slug: 'footer',
locale
})
if (footer) {
const newFooter = processObject(footer)
if (JSON.stringify(footer) !== JSON.stringify(newFooter)) {
console.log(`[${locale}] Aktualizacja stopki (footer)`)
await payload.updateGlobal({
slug: 'footer',
locale,
data: newFooter
})
}
}
}
// 2. Aktualizacja w content-snapshot.json
console.log('--- Aktualizowanie pliku content-snapshot.json ---')
const snapshotPath = path.join(process.cwd(), 'scripts', 'content-snapshot.json')
const snapshotText = fs.readFileSync(snapshotPath, 'utf-8')
// Przetwórz po prostu cały JSON rekursywnie. Zabezpieczamy blok "bio" w processObject
const snapshotJson = JSON.parse(snapshotText)
const newSnapshotJson = processObject(snapshotJson)
if (JSON.stringify(snapshotJson) !== JSON.stringify(newSnapshotJson)) {
fs.writeFileSync(snapshotPath, JSON.stringify(newSnapshotJson, null, 2), 'utf-8')
console.log('Zaktualizowano plik content-snapshot.json')
} else {
console.log('Brak zmian w content-snapshot.json (być może zostało już zrobione)')
}
console.log('Zakończono pomyślnie.')
process.exit(0)
}
run()