mermaid_diagram_creator/index.ts
2026-03-20 12:44:48 +01:00

37 lines
990 B
TypeScript

import { z } from "zod";
import { renderMermaidSVG } from "beautiful-mermaid";
const args = process.argv.slice(2);
if (args.length < 2) {
console.error("Usage: bun index.ts <input.mmd> <output.svg>");
console.error("Example: bun index.ts diagram.mmd output.svg");
process.exit(1);
}
const argsSchema = z.tuple([z.string(), z.string()]);
const parsed = argsSchema.parse(args);
const [inputPath, outputPath] = parsed;
try {
// Read the .mmd file
const diagramText = await Bun.file(inputPath).text();
// Render to SVG
const svg = renderMermaidSVG(diagramText);
// Write to output file
await Bun.write(outputPath, svg);
console.log(`✓ Generated SVG: ${outputPath}`);
} catch (error) {
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
console.error(`✗ Input file not found: ${inputPath}`);
} else {
console.error(
`✗ Error: ${error instanceof Error ? error.message : String(error)}`,
);
}
process.exit(1);
}