import { getPayload } from "payload"; import configPromise from "./payload.config"; import fs from "fs"; import path from "path"; async function run() { try { const payload = await getPayload({ config: configPromise }); console.log("Payload initialized."); const docsDir = path.resolve(process.cwd(), "docs"); if (!fs.existsSync(docsDir)) { console.log(`Docs directory not found at ${docsDir}`); process.exit(0); } const files = fs.readdirSync(docsDir); let count = 0; for (const file of files) { if (file.endsWith(".md")) { const content = fs.readFileSync(path.join(docsDir, file), "utf8"); // Check if already exists const existing = await payload.find({ collection: "context-files", where: { filename: { equals: file } }, }); if (existing.totalDocs === 0) { await payload.create({ collection: "context-files", data: { filename: file, content: content, }, }); count++; } } } console.log( `Migration successful! Added ${count} new context files to the database.`, ); process.exit(0); } catch (e) { console.error("Migration failed:", e); process.exit(1); } } run();