59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import { getPayload } from 'payload'
|
|
import configPromise from '../src/payload.config'
|
|
import fs from 'fs'
|
|
import path from 'path'
|
|
|
|
async function run() {
|
|
const payload = await getPayload({ config: configPromise })
|
|
|
|
// Create media records if they don't exist
|
|
const mediaDir = path.join(__dirname, '../media')
|
|
const files = fs.readdirSync(mediaDir).filter(f => f !== '.DS_Store' && f !== 'placeholder-desk.jpg')
|
|
|
|
const mediaRecords = []
|
|
|
|
for (const file of files) {
|
|
const filePath = path.join(mediaDir, file)
|
|
// Check if exists in db
|
|
const existing = await payload.find({
|
|
collection: 'media',
|
|
where: { filename: { equals: file } }
|
|
})
|
|
|
|
if (existing.docs.length > 0) {
|
|
mediaRecords.push(existing.docs[0])
|
|
} else {
|
|
console.log(`Creating media record for ${file}`)
|
|
// Have to read file and pass to payload.create
|
|
// It's tricky to upload local file via local API without express req
|
|
// Let's just create the db record manually?
|
|
// No, payload.create({ collection: 'media', data: { alt: 'Hero image' }, file: { data: fs.readFileSync(filePath), mimetype: 'image/png', name: file, size: fs.statSync(filePath).size } })
|
|
try {
|
|
const created = await payload.create({
|
|
collection: 'media',
|
|
data: { alt: 'Hero background' },
|
|
file: {
|
|
data: fs.readFileSync(filePath),
|
|
mimetype: 'image/png',
|
|
name: file,
|
|
size: fs.statSync(filePath).size
|
|
}
|
|
})
|
|
mediaRecords.push(created)
|
|
} catch (e) {
|
|
console.error("Error creating", file, e.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(`Found/Created ${mediaRecords.length} media records.`)
|
|
|
|
// Dump home page
|
|
const home = await payload.find({ collection: 'pages', where: { slug: { equals: 'home' } } })
|
|
console.log("Home Layout:")
|
|
home.docs[0]?.layout?.forEach(b => console.log(b.blockType))
|
|
|
|
process.exit(0)
|
|
}
|
|
run().catch(console.error)
|