Files
j_kedzierski/scripts/export_snapshot.ts
T

81 lines
1.8 KiB
TypeScript

import { getPayload } from 'payload'
import configPromise from '../src/payload.config'
import fs from 'fs'
import path from 'path'
async function run() {
console.log('Connecting to Payload Local API...')
const payload = await getPayload({ config: configPromise })
const locales = ['pl', 'de']
const snapshot: Record<string, any> = {
pages: [],
globals: {
header: {},
footer: {},
company: {},
'site-settings': {}
}
}
// 1. Fetch all pages in all locales
console.log('Exporting pages...')
const plPages = await payload.find({
collection: 'pages',
locale: 'pl',
limit: 100,
depth: 0,
})
const dePages = await payload.find({
collection: 'pages',
locale: 'de',
limit: 100,
depth: 0,
})
// We combine them by ID
for (const plPage of plPages.docs) {
const dePage = dePages.docs.find(p => p.id === plPage.id)
snapshot.pages.push({
id: plPage.id,
pl: plPage,
de: dePage
})
}
// 2. Fetch globals
console.log('Exporting globals...')
const globalsList = ['header', 'footer', 'company', 'site-settings']
for (const slug of globalsList) {
const plGlobal = await payload.findGlobal({
slug: slug as any,
locale: 'pl',
depth: 0
})
const deGlobal = await payload.findGlobal({
slug: slug as any,
locale: 'de',
depth: 0
})
snapshot.globals[slug] = {
pl: plGlobal,
de: deGlobal
}
}
const outputPath = path.join(process.cwd(), 'scripts', 'content-snapshot.json')
fs.writeFileSync(outputPath, JSON.stringify(snapshot, null, 2))
console.log(`Snapshot saved to ${outputPath}`)
process.exit(0)
}
try {
await run()
} catch (err) {
console.error('ERROR during export:', err)
process.exit(1)
}