Skip to content

Commit

Permalink
feat: give bin ability to output svg files, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
nlf committed Oct 19, 2023
1 parent 88dcf1e commit c63bcca
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
22 changes: 19 additions & 3 deletions bin/threatdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseArgs } from "node:util";
import {
parse,
compileToMermaid,
renderMermaid,
} from "../lib";

const {
Expand All @@ -20,6 +21,7 @@ const {
},
outputType: {
type: "string",
alias: "type",
short: "t",
default: "mermaid",
},
Expand All @@ -34,11 +36,11 @@ function usage () {
`);
}

function main () {
async function main () {
const inputFile = positionals.shift();
// non-null assertion safe because the outputType has a default
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (!inputFile || !["json", "mermaid"].includes(values.outputType!)) {
if (!inputFile || !["json", "mermaid", "svg"].includes(values.outputType!)) {
usage();
} else {
const fileContent = readFileSync(resolve(process.cwd(), inputFile), { encoding: "utf8" });
Expand All @@ -59,8 +61,22 @@ function main () {
} else {
console.log(mermaidContent);
}
return;
}

const svgContent = await renderMermaid(mermaidContent);
if (values.outputType === "svg") {
if (values.output) {
writeFileSync(resolve(process.cwd(), values.output), svgContent);
} else {
console.log(svgContent);
}
}
}
}

main();
main()
.catch((err: Error) => {
process.exitCode = 1;
console.error(err.stack);
});
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { parse } from "./parser";
export { compileToMermaid } from "./compiler";
export { renderMermaid } from "./renderer";
31 changes: 31 additions & 0 deletions lib/renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { spawn } from "node:child_process";
import { readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";

export async function renderMermaid (contents: string): Promise<string> {
const tmpOutput = join(tmpdir(), `.threatdown-${Date.now()}.svg`);

let resolve: (value?: unknown) => void;
let reject: (err: Error) => void;
const p = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});

const mmdc = spawn("mmdc", ["--input", "-", "--output", tmpOutput], { stdio: "pipe" });

mmdc.on("error", (err: Error) => {
reject(err);
});

mmdc.on("close", () => {
resolve();
});

mmdc.stdin.end(contents);
await p;

const rendered = await readFile(tmpOutput, { encoding: "utf8" });
return rendered;
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,8 @@
"lib/**/*.js",
"lib/**/*.d.ts",
"!lib/types/**"
]
],
"dependencies": {
"mermaid-cli": "^0.2.4"
}
}

0 comments on commit c63bcca

Please sign in to comment.