-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-modules.ts
More file actions
54 lines (44 loc) · 1.83 KB
/
Copy pathsync-modules.ts
File metadata and controls
54 lines (44 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env node
import prisma from "../db";
import fs from "fs";
import path from "path";
import matter from "gray-matter";
const modulePath = path.join(process.cwd(), "src/data/docs/modules");
export async function syncModules() {
console.log("📚 Starting module sync...");
const start = Date.now();
const files = fs.readdirSync(modulePath);
for (const file of files) {
if (!file.endsWith(".mdx")) continue;
try {
const fileContent = fs.readFileSync(path.join(modulePath, file), "utf8");
const { data: frontmatter } = matter(fileContent);
const slug = file.replace(".mdx", "");
const {topicRefs, ...rest} = frontmatter;
const dt = {
...rest,
slug,
title: rest.title || slug.replace(/-/g, " "),
description: rest.description || "",
// order: rest.order ?? 999,
// locked: rest.locked ?? true,
topics: {
connect: topicRefs?.map((slug: string) => ({ slug })) || [],
},
locked: frontmatter.locked ?? false,
createdAt: (frontmatter.createdAt ? new Date(frontmatter.createdAt) : new Date()).toISOString(),
updatedAt: new Date()
}
await prisma.module.upsert({
where: { slug },
update: dt,
create: dt,
});
console.log(`🔄 Synced module: ${slug} with ${(frontmatter.topicRefs || []).length} topics`);
} catch (error) {
console.error(`❌ Failed syncing ${file}:`, error instanceof Error ? error.message : error);
}
}
console.log("🎉 Module sync completed");
console.log(`⏱️ Sync took ${(Date.now() - start) / 1000}s`);
}