Files
mintel.me/apps/web/scripts/migrate-posts.ts
Marc Mintel 072b6b13f1
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 3m43s
Build & Deploy / 🏗️ Build (push) Failing after 37s
Build & Deploy / 🧪 QA (push) Failing after 3m40s
Build & Deploy / 🚀 Deploy (push) Has been skipped
Build & Deploy / 🩺 Health Check (push) Has been skipped
Build & Deploy / 🔔 Notify (push) Successful in 2s
feat(cms): migrate from Directus to Payload v3 and remove contentlayer
2026-02-22 20:50:51 +01:00

73 lines
2.1 KiB
TypeScript

import { getPayload } from "payload";
import configPromise from "../payload.config";
import fs from "fs";
import path from "path";
function parseMatter(content: string) {
const match = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
if (!match) return { data: {}, content };
const data: Record<string, any> = {};
match[1].split("\n").forEach((line) => {
const [key, ...rest] = line.split(":");
if (key && rest.length) {
const field = key.trim();
let val = rest.join(":").trim();
if (val.startsWith("[")) {
// basic array parsing
data[field] = val
.slice(1, -1)
.split(",")
.map((s) => s.trim().replace(/^["']|["']$/g, ""));
} else {
data[field] = val.replace(/^["']|["']$/g, "");
}
}
});
return { data, content: match[2].trim() };
}
async function run() {
const payload = await getPayload({ config: configPromise });
const contentDir = path.join(process.cwd(), "content", "blog");
const files = fs.readdirSync(contentDir).filter((f) => f.endsWith(".mdx"));
for (const file of files) {
const filePath = path.join(contentDir, file);
const content = fs.readFileSync(filePath, "utf-8");
const { data, content: body } = parseMatter(content);
const slug = file.replace(/\.mdx$/, "");
console.log(`Migrating ${slug}...`);
const existing = await payload.find({
collection: "posts",
where: { slug: { equals: slug } },
});
if (existing.docs.length === 0) {
await payload.create({
collection: "posts",
data: {
title: data.title || slug,
slug,
description: data.description || "",
date: data.date
? new Date(data.date).toISOString()
: new Date().toISOString(),
tags: (data.tags || []).map((t: string) => ({ tag: t })),
thumbnail: data.thumbnail || "",
content: body,
},
});
console.log(`✔ Inserted ${slug}`);
} else {
console.log(`⚠ Skipped ${slug} (already exists)`);
}
}
console.log("Migration complete.");
process.exit(0);
}
run().catch(console.error);