All files / src mermaid.ts

20% Statements 2/10
0% Branches 0/2
0% Functions 0/1
20% Lines 2/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x   1x                                                                              
/**
 * A wrapper of mermaid CLI
 * https://github.com/mermaid-js/mermaid.cli
 * But it doesn't work well
 */
 
import * as utility from "./utility";
 
export async function mermaidToPNG(
  mermaidCode: string,
  pngFilePath: string,
  projectDirectoryPath: string,
  themeName,
): Promise<string> {
  const info = await utility.tempOpen({
    prefix: "mume-mermaid",
    suffix: ".mmd",
  });
  await utility.write(info.fd, mermaidCode);
  if (!themeName) {
    themeName = "null";
  }
  try {
    await utility.execFile(
      "npx",
      [
        "mmdc",
        "--theme",
        themeName,
        "--input",
        info.path,
        "--output",
        pngFilePath,
      ],
      {
        shell: true,
        cwd: projectDirectoryPath,
      },
    );
    return pngFilePath;
  } catch (error) {
    throw new Error(
      "mermaid CLI is required to be installed.\nCheck https://github.com/mermaid-js/mermaid.cli for more information.\n\n" +
        error.toString(),
    );
  }
}