All files / src magick.ts

25% Statements 3/12
0% Branches 0/5
0% Functions 0/1
25% Lines 3/12

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      1x 1x   1x                                                
/**
 * ImageMagick magick command wrapper
 */
import imagemagickCli = require("imagemagick-cli");
import { execFile, tempOpen, write } from "./utility";
 
export async function svgElementToPNGFile(
  svgElement: string,
  pngFilePath: string,
  imageMagickPath: string = "",
): Promise<string> {
  const info = await tempOpen({ prefix: "mume-svg", suffix: ".svg" });
  await write(info.fd, svgElement); // write svgElement to temp .svg file
  const args = [info.path, pngFilePath];
  try {
    if (imageMagickPath && imageMagickPath.length) {
      await execFile(imageMagickPath, args);
    } else {
      await imagemagickCli.exec(`convert ${args.join(" ")}`);
    }
  } catch (error) {
    throw new Error(
      "imagemagick-cli failure\n" +
        error.toString() +
        "\n\nPlease make sure you have ImageMagick installed.",
    );
  }
 
  return pngFilePath;
}