File size: 2,016 Bytes
62be781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";

export const properties = {
  from: {
    images: ["jpg", "jpeg", "png", "bmp", "gif", "tiff", "tif", "webp"],
  },
  to: {
    images: ["svg"],
  },
};

interface VTracerOptions {
  colormode?: string;
  hierarchical?: string;
  mode?: string;
  filter_speckle?: string | number;
  color_precision?: string | number;
  layer_difference?: string | number;
  corner_threshold?: string | number;
  length_threshold?: string | number;
  max_iterations?: string | number;
  splice_threshold?: string | number;
  path_precision?: string | number;
}

export function convert(
  filePath: string,
  fileType: string,
  convertTo: string,
  targetPath: string,
  options?: unknown,
  execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
  return new Promise((resolve, reject) => {
    // Build vtracer arguments
    const args = ["--input", filePath, "--output", targetPath];

    // Add optional parameter if provided
    if (options && typeof options === "object") {
      const opts = options as VTracerOptions;
      const validOptions: Array<keyof VTracerOptions> = [
        "colormode",
        "hierarchical",
        "mode",
        "filter_speckle",
        "color_precision",
        "layer_difference",
        "corner_threshold",
        "length_threshold",
        "max_iterations",
        "splice_threshold",
        "path_precision",
      ];

      for (const option of validOptions) {
        if (opts[option] !== undefined) {
          args.push(`--${option}`, String(opts[option]));
        }
      }
    }

    execFile("vtracer", args, (error, stdout, stderr) => {
      if (error) {
        reject(`error: ${error}${stderr ? `\nstderr: ${stderr}` : ""}`);
        return;
      }

      if (stdout) {
        console.log(`stdout: ${stdout}`);
      }

      if (stderr) {
        console.log(`stderr: ${stderr}`);
      }

      resolve("Done");
    });
  });
}