feat: replace exec with execFile

This commit is contained in:
C4illin 2025-03-06 18:16:51 +01:00
parent c1b75a13fd
commit 9263d17609
10 changed files with 869 additions and 855 deletions

View file

@ -1,4 +1,4 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
@ -119,10 +119,8 @@ export async function convert(
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
const command = `assimp export "${filePath}" "${targetPath}"`;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => { execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,4 +1,4 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
@ -64,10 +64,8 @@ export async function convert(
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
const command = `ebook-convert "${filePath}" "${targetPath}"`;
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => { execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,4 +1,4 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
// This could be done dynamically by running `ffmpeg -formats` and parsing the output // This could be done dynamically by running `ffmpeg -formats` and parsing the output
export const properties = { export const properties = {
@ -691,19 +691,28 @@ export async function convert(
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
let extra = ""; let extraArgs: string[] = [];
let message = "Done"; let message = "Done";
if (convertTo === "ico") { if (convertTo === "ico") {
// make sure image is 256x256 or smaller // make sure image is 256x256 or smaller
extra = `-filter:v "scale='min(256,iw)':min'(256,ih)':force_original_aspect_ratio=decrease"`; extraArgs = ['-filter:v', "scale='min(256,iw)':min'(256,ih)':force_original_aspect_ratio=decrease"];
message = "Done: resized to 256x256"; message = "Done: resized to 256x256";
} }
const command = `ffmpeg ${process.env.FFMPEG_ARGS || ""} -i "${filePath}" ${extra} "${targetPath}"`; // Parse FFMPEG_ARGS environment variable into array
const ffmpegArgs = process.env.FFMPEG_ARGS ? process.env.FFMPEG_ARGS.split(/\s+/) : [];
// Build arguments array
const args = [
...ffmpegArgs,
"-i", filePath,
...extraArgs,
targetPath
];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => { execFile("ffmpeg", args, (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }

View file

@ -1,339 +1,340 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
image: [ image: [
"3fr", "3fr",
"8bim", "8bim",
"8bimtext", "8bimtext",
"8bimwtext", "8bimwtext",
"app1", "app1",
"app1jpeg", "app1jpeg",
"art", "art",
"arw", "arw",
"avs", "avs",
"b", "b",
"bie", "bie",
"bigtiff", "bigtiff",
"bmp", "bmp",
"c", "c",
"cals", "cals",
"caption", "caption",
"cin", "cin",
"cmyk", "cmyk",
"cmyka", "cmyka",
"cr2", "cr2",
"crw", "crw",
"cur", "cur",
"cut", "cut",
"dcm", "dcm",
"dcr", "dcr",
"dcx", "dcx",
"dng", "dng",
"dpx", "dpx",
"epdf", "epdf",
"epi", "epi",
"eps", "eps",
"epsf", "epsf",
"epsi", "epsi",
"ept", "ept",
"ept2", "ept2",
"ept3", "ept3",
"erf", "erf",
"exif", "exif",
"fax", "fax",
"file", "file",
"fits", "fits",
"fractal", "fractal",
"ftp", "ftp",
"g", "g",
"gif", "gif",
"gif87", "gif87",
"gradient", "gradient",
"gray", "gray",
"graya", "graya",
"heic", "heic",
"heif", "heif",
"hrz", "hrz",
"http", "http",
"icb", "icb",
"icc", "icc",
"icm", "icm",
"ico", "ico",
"icon", "icon",
"identity", "identity",
"image", "image",
"iptc", "iptc",
"iptctext", "iptctext",
"iptcwtext", "iptcwtext",
"jbg", "jbg",
"jbig", "jbig",
"jng", "jng",
"jnx", "jnx",
"jpeg", "jpeg",
"jpg", "jpg",
"k", "k",
"k25", "k25",
"kdc", "kdc",
"label", "label",
"m", "m",
"mac", "mac",
"map", "map",
"mat", "mat",
"mef", "mef",
"miff", "miff",
"mng", "mng",
"mono", "mono",
"mpc", "mpc",
"mrw", "mrw",
"msl", "msl",
"mtv", "mtv",
"mvg", "mvg",
"nef", "nef",
"null", "null",
"o", "o",
"orf", "orf",
"otb", "otb",
"p7", "p7",
"pal", "pal",
"palm", "palm",
"pam", "pam",
"pbm", "pbm",
"pcd", "pcd",
"pcds", "pcds",
"pct", "pct",
"pcx", "pcx",
"pdb", "pdb",
"pdf", "pdf",
"pef", "pef",
"pfa", "pfa",
"pfb", "pfb",
"pgm", "pgm",
"picon", "picon",
"pict", "pict",
"pix", "pix",
"plasma", "plasma",
"png", "png",
"png00", "png00",
"png24", "png24",
"png32", "png32",
"png48", "png48",
"png64", "png64",
"png8", "png8",
"pnm", "pnm",
"ppm", "ppm",
"ps", "ps",
"ptif", "ptif",
"pwp", "pwp",
"r", "r",
"raf", "raf",
"ras", "ras",
"rgb", "rgb",
"rgba", "rgba",
"rla", "rla",
"rle", "rle",
"sct", "sct",
"sfw", "sfw",
"sgi", "sgi",
"sr2", "sr2",
"srf", "srf",
"stegano", "stegano",
"sun", "sun",
"svg", "svg",
"svgz", "svgz",
"text", "text",
"tga", "tga",
"tif", "tif",
"tiff", "tiff",
"tile", "tile",
"tim", "tim",
"topol", "topol",
"ttf", "ttf",
"txt", "txt",
"uyvy", "uyvy",
"vda", "vda",
"vicar", "vicar",
"vid", "vid",
"viff", "viff",
"vst", "vst",
"wbmp", "wbmp",
"webp", "webp",
"wmf", "wmf",
"wpg", "wpg",
"x3f", "x3f",
"xbm", "xbm",
"xc", "xc",
"xcf", "xcf",
"xmp", "xmp",
"xpm", "xpm",
"xv", "xv",
"xwd", "xwd",
"y", "y",
"yuv", "yuv",
], ],
}, },
to: { to: {
image: [ image: [
"8bim", "8bim",
"8bimtext", "8bimtext",
"8bimwtext", "8bimwtext",
"app1", "app1",
"app1jpeg", "app1jpeg",
"art", "art",
"avs", "avs",
"b", "b",
"bie", "bie",
"bigtiff", "bigtiff",
"bmp", "bmp",
"bmp2", "bmp2",
"bmp3", "bmp3",
"brf", "brf",
"c", "c",
"cals", "cals",
"cin", "cin",
"cmyk", "cmyk",
"cmyka", "cmyka",
"dcx", "dcx",
"dpx", "dpx",
"epdf", "epdf",
"epi", "epi",
"eps", "eps",
"eps2", "eps2",
"eps3", "eps3",
"epsf", "epsf",
"epsi", "epsi",
"ept", "ept",
"ept2", "ept2",
"ept3", "ept3",
"exif", "exif",
"fax", "fax",
"fits", "fits",
"g", "g",
"gif", "gif",
"gif87", "gif87",
"gray", "gray",
"graya", "graya",
"histogram", "histogram",
"html", "html",
"icb", "icb",
"icc", "icc",
"icm", "icm",
"info", "info",
"iptc", "iptc",
"iptctext", "iptctext",
"iptcwtext", "iptcwtext",
"isobrl", "isobrl",
"isobrl6", "isobrl6",
"jbg", "jbg",
"jbig", "jbig",
"jng", "jng",
"jpeg", "jpeg",
"k", "k",
"m", "m",
"m2v", "m2v",
"map", "map",
"mat", "mat",
"matte", "matte",
"miff", "miff",
"mng", "mng",
"mono", "mono",
"mpc", "mpc",
"mpeg", "mpeg",
"mpg", "mpg",
"msl", "msl",
"mtv", "mtv",
"mvg", "mvg",
"null", "null",
"o", "o",
"otb", "otb",
"p7", "p7",
"pal", "pal",
"pam", "pam",
"pbm", "pbm",
"pcd", "pcd",
"pcds", "pcds",
"pcl", "pcl",
"pct", "pct",
"pcx", "pcx",
"pdb", "pdb",
"pdf", "pdf",
"pgm", "pgm",
"picon", "picon",
"pict", "pict",
"png", "png",
"png00", "png00",
"png24", "png24",
"png32", "png32",
"png48", "png48",
"png64", "png64",
"png8", "png8",
"pnm", "pnm",
"ppm", "ppm",
"preview", "preview",
"ps", "ps",
"ps2", "ps2",
"ps3", "ps3",
"ptif", "ptif",
"r", "r",
"ras", "ras",
"rgb", "rgb",
"rgba", "rgba",
"sgi", "sgi",
"shtml", "shtml",
"sun", "sun",
"text", "text",
"tga", "tga",
"tiff", "tiff",
"txt", "txt",
"ubrl", "ubrl",
"ubrl6", "ubrl6",
"uil", "uil",
"uyvy", "uyvy",
"vda", "vda",
"vicar", "vicar",
"vid", "vid",
"viff", "viff",
"vst", "vst",
"wbmp", "wbmp",
"webp", "webp",
"x", "x",
"xbm", "xbm",
"xmp", "xmp",
"xpm", "xpm",
"xv", "xv",
"xwd", "xwd",
"y", "y",
"yuv", "yuv",
], ],
}, },
}; };
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec( execFile(
`gm convert "${filePath}" "${targetPath}"`, "gm",
(error, stdout, stderr) => { ["convert", filePath, targetPath],
if (error) { (error, stdout, stderr) => {
reject(`error: ${error}`); if (error) {
} reject(`error: ${error}`);
}
if (stdout) {
console.log(`stdout: ${stdout}`); if (stdout) {
} console.log(`stdout: ${stdout}`);
}
if (stderr) {
console.error(`stderr: ${stderr}`); if (stderr) {
} console.error(`stderr: ${stderr}`);
}
resolve("Done");
}, resolve("Done");
); },
}); );
} });
}

View file

@ -1,64 +1,59 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
images: [ images: ["svg", "pdf", "eps", "ps", "wmf", "emf", "png"],
"svg", },
"pdf", to: {
"eps", images: [
"ps", "dxf",
"wmf", "emf",
"emf", "eps",
"png" "fxg",
] "gpl",
}, "hpgl",
to: { "html",
images: [ "odg",
"dxf", "pdf",
"emf", "png",
"eps", "pov",
"fxg", "ps",
"gpl", "sif",
"hpgl", "svg",
"html", "svgz",
"odg", "tex",
"pdf", "wmf",
"png", ],
"pov", },
"ps", };
"sif",
"svg",
"svgz",
"tex",
"wmf",
]
},
};
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`inkscape "${filePath}" -o "${targetPath}"`, (error, stdout, stderr) => { execFile(
"inkscape",
[filePath, "-o", targetPath],
(error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}); },
}); );
} });
}

View file

@ -1,71 +1,71 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
// declare possible conversions // declare possible conversions
export const properties = { export const properties = {
from: { from: {
jxl: ["jxl"], jxl: ["jxl"],
images: [ images: [
"apng", "apng",
"exr", "exr",
"gif", "gif",
"jpeg", "jpeg",
"pam", "pam",
"pfm", "pfm",
"pgm", "pgm",
"pgx", "pgx",
"png", "png",
"ppm", "ppm",
], ],
}, },
to: { to: {
jxl: [ jxl: [
"apng", "apng",
"exr", "exr",
"gif", "gif",
"jpeg", "jpeg",
"pam", "pam",
"pfm", "pfm",
"pgm", "pgm",
"pgx", "pgx",
"png", "png",
"ppm", "ppm",
], ],
images: ["jxl"], images: ["jxl"],
}, },
}; };
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
let tool = ""; let tool = "";
if (fileType === "jxl") { if (fileType === "jxl") {
tool = "djxl"; tool = "djxl";
} }
if (convertTo === "jxl") { if (convertTo === "jxl") {
tool = "cjxl"; tool = "cjxl";
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`${tool} "${filePath}" "${targetPath}"`, (error, stdout, stderr) => { execFile(tool, [filePath, targetPath], (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}); });
}); });
} }

View file

@ -1,156 +1,162 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
text: [ text: [
"textile", "textile",
"tikiwiki", "tikiwiki",
"tsv", "tsv",
"twiki", "twiki",
"typst", "typst",
"vimwiki", "vimwiki",
"biblatex", "biblatex",
"bibtex", "bibtex",
"bits", "bits",
"commonmark", "commonmark",
"commonmark_x", "commonmark_x",
"creole", "creole",
"csljson", "csljson",
"csv", "csv",
"djot", "djot",
"docbook", "docbook",
"docx", "docx",
"dokuwiki", "dokuwiki",
"endnotexml", "endnotexml",
"epub", "epub",
"fb2", "fb2",
"gfm", "gfm",
"haddock", "haddock",
"html", "html",
"ipynb", "ipynb",
"jats", "jats",
"jira", "jira",
"json", "json",
"latex", "latex",
"man", "man",
"markdown", "markdown",
"markdown_mmd", "markdown_mmd",
"markdown_phpextra", "markdown_phpextra",
"markdown_strict", "markdown_strict",
"mediawiki", "mediawiki",
"muse", "muse",
"pandoc native", "pandoc native",
"opml", "opml",
"org", "org",
"ris", "ris",
"rst", "rst",
"rtf", "rtf",
"t2t", "t2t",
], ],
}, },
to: { to: {
text: [ text: [
"tei", "tei",
"texinfo", "texinfo",
"textile", "textile",
"typst", "typst",
"xwiki", "xwiki",
"zimwiki", "zimwiki",
"asciidoc", "asciidoc",
"asciidoc_legacy", "asciidoc_legacy",
"asciidoctor", "asciidoctor",
"beamer", "beamer",
"biblatex", "biblatex",
"bibtex", "bibtex",
"chunkedhtml", "chunkedhtml",
"commonmark", "commonmark",
"commonmark_x", "commonmark_x",
"context", "context",
"csljson", "csljson",
"djot", "djot",
"docbook", "docbook",
"docbook4", "docbook4",
"docbook5", "docbook5",
"docx", "docx",
"dokuwiki", "dokuwiki",
"dzslides", "dzslides",
"epub", "epub",
"epub2", "epub2",
"epub3", "epub3",
"fb2", "fb2",
"gfm", "gfm",
"haddock", "haddock",
"html", "html",
"html4", "html4",
"html5", "html5",
"icml", "icml",
"ipynb", "ipynb",
"jats", "jats",
"jats_archiving", "jats_archiving",
"jats_articleauthoring", "jats_articleauthoring",
"jats_publishing", "jats_publishing",
"jira", "jira",
"json", "json",
"latex", "latex",
"man", "man",
"markdown", "markdown",
"markdown_mmd", "markdown_mmd",
"markdown_phpextra", "markdown_phpextra",
"markdown_strict", "markdown_strict",
"markua", "markua",
"mediawiki", "mediawiki",
"ms", "ms",
"muse", "muse",
"pandoc native", "pandoc native",
"odt", "odt",
"opendocument", "opendocument",
"opml", "opml",
"org", "org",
"pdf", "pdf",
"plain", "plain",
"pptx", "pptx",
"revealjs", "revealjs",
"rst", "rst",
"rtf", "rtf",
"s5", "s5",
"slideous", "slideous",
"slidy", "slidy",
], ],
}, },
}; };
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
// set xelatex here // set xelatex here
const xelatex = ["pdf", "latex"]; const xelatex = ["pdf", "latex"];
let option = "";
if (xelatex.includes(convertTo)) { // Build arguments array
option = "--pdf-engine=xelatex"; const args: string[] = [];
}
return new Promise((resolve, reject) => { if (xelatex.includes(convertTo)) {
exec( args.push("--pdf-engine=xelatex");
`pandoc ${option} "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`, }
(error, stdout, stderr) => {
if (error) { args.push(filePath);
reject(`error: ${error}`); args.push("-f", fileType);
} args.push("-t", convertTo);
args.push("-o", targetPath);
if (stdout) {
console.log(`stdout: ${stdout}`); return new Promise((resolve, reject) => {
} execFile("pandoc", args, (error, stdout, stderr) => {
if (error) {
if (stderr) { reject(`error: ${error}`);
console.error(`stderr: ${stderr}`); }
}
if (stdout) {
resolve("Done"); console.log(`stdout: ${stdout}`);
}, }
);
}); if (stderr) {
} console.error(`stderr: ${stderr}`);
}
resolve("Done");
});
});
}

View file

@ -1,37 +1,37 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
images: ["svg"], images: ["svg"],
}, },
to: { to: {
images: ["png"], images: ["png"],
}, },
}; };
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
exec(`resvg "${filePath}" "${targetPath}"`, (error, stdout, stderr) => { execFile("resvg", [filePath, targetPath], (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}); });
}); });
} }

View file

@ -1,142 +1,142 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
// declare possible conversions
// declare possible conversions export const properties = {
export const properties = { from: {
from: { images: [
images: [ "avif",
"avif", "bif",
"bif", "csv",
"csv", "exr",
"exr", "fits",
"fits", "gif",
"gif", "hdr.gz",
"hdr.gz", "hdr",
"hdr", "heic",
"heic", "heif",
"heif", "img.gz",
"img.gz", "img",
"img", "j2c",
"j2c", "j2k",
"j2k", "jp2",
"jp2", "jpeg",
"jpeg", "jpx",
"jpx", "jxl",
"jxl", "mat",
"mat", "mrxs",
"mrxs", "ndpi",
"ndpi", "nia.gz",
"nia.gz", "nia",
"nia", "nii.gz",
"nii.gz", "nii",
"nii", "pdf",
"pdf", "pfm",
"pfm", "pgm",
"pgm", "pic",
"pic", "png",
"png", "ppm",
"ppm", "raw",
"raw", "scn",
"scn", "svg",
"svg", "svs",
"svs", "svslide",
"svslide", "szi",
"szi", "tif",
"tif", "tiff",
"tiff", "v",
"v", "vips",
"vips", "vms",
"vms", "vmu",
"vmu", "webp",
"webp", "zip",
"zip", ],
], },
}, to: {
to: { images: [
images: [ "avif",
"avif", "dzi",
"dzi", "fits",
"fits", "gif",
"gif", "hdr.gz",
"hdr.gz", "heic",
"heic", "heif",
"heif", "img.gz",
"img.gz", "j2c",
"j2c", "j2k",
"j2k", "jp2",
"jp2", "jpeg",
"jpeg", "jpx",
"jpx", "jxl",
"jxl", "mat",
"mat", "nia.gz",
"nia.gz", "nia",
"nia", "nii.gz",
"nii.gz", "nii",
"nii", "png",
"png", "tiff",
"tiff", "vips",
"vips", "webp",
"webp", ],
], },
}, options: {
options: { svg: {
svg: { scale: {
scale: { description: "Scale the image up or down",
description: "Scale the image up or down", type: "number",
type: "number", default: 1,
default: 1, },
}, },
}, },
}, };
};
export function convert(
export function convert( filePath: string,
filePath: string, fileType: string,
fileType: string, convertTo: string,
convertTo: string, targetPath: string,
targetPath: string, // eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars options?: unknown,
options?: unknown, ): Promise<string> {
): Promise<string> { // if (fileType === "svg") {
// if (fileType === "svg") { // const scale = options.scale || 1;
// const scale = options.scale || 1; // const metadata = await sharp(filePath).metadata();
// const metadata = await sharp(filePath).metadata();
// if (!metadata || !metadata.width || !metadata.height) {
// if (!metadata || !metadata.width || !metadata.height) { // throw new Error("Could not get metadata from image");
// throw new Error("Could not get metadata from image"); // }
// }
// const newWidth = Math.round(metadata.width * scale);
// const newWidth = Math.round(metadata.width * scale); // const newHeight = Math.round(metadata.height * scale);
// const newHeight = Math.round(metadata.height * scale);
// return await sharp(filePath)
// return await sharp(filePath) // .resize(newWidth, newHeight)
// .resize(newWidth, newHeight) // .toFormat(convertTo)
// .toFormat(convertTo) // .toFile(targetPath);
// .toFile(targetPath); // }
// } let action = "copy";
let action = "copy"; if (fileType === "pdf") {
if (fileType === "pdf") { action = "pdfload";
action = "pdfload"; }
}
return new Promise((resolve, reject) => {
return new Promise((resolve, reject) => { execFile(
exec( "vips",
`vips ${action} "${filePath}" "${targetPath}"`, [action, filePath, targetPath],
(error, stdout, stderr) => { (error, stdout, stderr) => {
if (error) { if (error) {
reject(`error: ${error}`); reject(`error: ${error}`);
} }
if (stdout) { if (stdout) {
console.log(`stdout: ${stdout}`); console.log(`stdout: ${stdout}`);
} }
if (stderr) { if (stderr) {
console.error(`stderr: ${stderr}`); console.error(`stderr: ${stderr}`);
} }
resolve("Done"); resolve("Done");
}, },
); );
}); });
} }

View file

@ -1,46 +1,53 @@
import { exec } from "node:child_process"; import { execFile } from "node:child_process";
export const properties = { export const properties = {
from: { from: {
text: ["tex", "latex"], text: ["tex", "latex"],
}, },
to: { to: {
text: ["pdf"], text: ["pdf"],
}, },
}; };
export function convert( export function convert(
filePath: string, filePath: string,
fileType: string, fileType: string,
convertTo: string, convertTo: string,
targetPath: string, targetPath: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
options?: unknown, options?: unknown,
): Promise<string> { ): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "") // const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "")
const outputPath = targetPath const outputPath = targetPath
.split("/") .split("/")
.slice(0, -1) .slice(0, -1)
.join("/") .join("/")
.replace("./", ""); .replace("./", "");
exec(
`latexmk -xelatex -interaction=nonstopmode -output-directory="${outputPath}" "${filePath}"`, execFile(
(error, stdout, stderr) => { "latexmk",
if (error) { [
reject(`error: ${error}`); "-xelatex",
} "-interaction=nonstopmode",
`-output-directory=${outputPath}`,
if (stdout) { filePath,
console.log(`stdout: ${stdout}`); ],
} (error, stdout, stderr) => {
if (error) {
if (stderr) { reject(`error: ${error}`);
console.error(`stderr: ${stderr}`); }
}
if (stdout) {
resolve("Done"); console.log(`stdout: ${stdout}`);
}, }
);
}); if (stderr) {
} console.error(`stderr: ${stderr}`);
}
resolve("Done");
},
);
});
}