import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const TEMPLATES_DIR = path.resolve(__dirname, "../src/data/templates");
async function migrate() {
console.log(`Scanning ${TEMPLATES_DIR}...`);
// Helper to recurse
async function scan(dir: string) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
await scan(fullPath);
} else if (entry.isFile() && entry.name.endsWith(".ts")) {
await convert(fullPath);
}
}
}
async function convert(filePath: string) {
try {
const content = await fs.readFile(filePath, "utf-8");
// Just wrap the entire file content in a markdown code block
// This supports both "string exports" (if properly formatted) and raw code files
const newContent = "```typescript\n" + content + "\n```";
const newPath = filePath.replace(/\.ts$/, ".md");
await fs.writeFile(newPath, newContent, "utf-8");
console.log(
`Converted: ${path.relative(TEMPLATES_DIR, filePath)} -> .md`,
);
// Optional: Delete original
// await fs.unlink(filePath);
} catch (err) {
console.error(`Error converting ${filePath}:`, err);
}
}
await scan(TEMPLATES_DIR);
console.log("Migration complete.");
}
migrate().catch(console.error);