import { getPayload } from "payload"; import configPromise from "./payload.config"; import fs from "fs"; import path from "path"; import { fileURLToPath } from "url"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function run() { try { const payload = await getPayload({ config: configPromise }); const existing = await payload.find({ collection: "context-files", limit: 0, }); if (existing.totalDocs > 0) { console.log("Context collection already populated. Skipping seed."); process.exit(0); } const seedDir = path.resolve( __dirname, "src/payload/collections/ContextFiles/seed", ); if (!fs.existsSync(seedDir)) { console.log(`Seed directory not found at ${seedDir}`); process.exit(0); } const files = fs.readdirSync(seedDir).filter((f) => f.endsWith(".md")); let count = 0; for (const file of files) { const content = fs.readFileSync(path.join(seedDir, file), "utf8"); await payload.create({ collection: "context-files", data: { filename: file, content: content, }, }); count++; } console.log(`Seeded ${count} context files.`); process.exit(0); } catch (e) { console.error("Seeding failed:", e); process.exit(1); } } run();