From 9263d17609dc4b2b367eb7fee67b3182e283b3a3 Mon Sep 17 00:00:00 2001 From: C4illin Date: Thu, 6 Mar 2025 18:16:51 +0100 Subject: [PATCH] feat: replace exec with execFile --- src/converters/assimp.ts | 6 +- src/converters/calibre.ts | 6 +- src/converters/ffmpeg.ts | 19 +- src/converters/graphicsmagick.ts | 679 ++++++++++++++++--------------- src/converters/inkscape.ts | 97 +++-- src/converters/libjxl.ts | 142 +++---- src/converters/pandoc.ts | 318 ++++++++------- src/converters/resvg.ts | 74 ++-- src/converters/vips.ts | 284 ++++++------- src/converters/xelatex.ts | 99 ++--- 10 files changed, 869 insertions(+), 855 deletions(-) diff --git a/src/converters/assimp.ts b/src/converters/assimp.ts index 0ec338a..76085e6 100644 --- a/src/converters/assimp.ts +++ b/src/converters/assimp.ts @@ -1,4 +1,4 @@ -import { exec } from "node:child_process"; +import { execFile } from "node:child_process"; export const properties = { from: { @@ -119,10 +119,8 @@ export async function convert( // eslint-disable-next-line @typescript-eslint/no-unused-vars options?: unknown, ): Promise { - const command = `assimp export "${filePath}" "${targetPath}"`; - return new Promise((resolve, reject) => { - exec(command, (error, stdout, stderr) => { + execFile("assimp", ["export", filePath, targetPath], (error, stdout, stderr) => { if (error) { reject(`error: ${error}`); } diff --git a/src/converters/calibre.ts b/src/converters/calibre.ts index 2d368ab..4edf63e 100644 --- a/src/converters/calibre.ts +++ b/src/converters/calibre.ts @@ -1,4 +1,4 @@ -import { exec } from "node:child_process"; +import { execFile } from "node:child_process"; export const properties = { from: { @@ -64,10 +64,8 @@ export async function convert( // eslint-disable-next-line @typescript-eslint/no-unused-vars options?: unknown, ): Promise { - const command = `ebook-convert "${filePath}" "${targetPath}"`; - return new Promise((resolve, reject) => { - exec(command, (error, stdout, stderr) => { + execFile("ebook-convert", [filePath, targetPath], (error, stdout, stderr) => { if (error) { reject(`error: ${error}`); } diff --git a/src/converters/ffmpeg.ts b/src/converters/ffmpeg.ts index 5154dab..5d942eb 100644 --- a/src/converters/ffmpeg.ts +++ b/src/converters/ffmpeg.ts @@ -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 export const properties = { @@ -691,19 +691,28 @@ export async function convert( // eslint-disable-next-line @typescript-eslint/no-unused-vars options?: unknown, ): Promise { - let extra = ""; + let extraArgs: string[] = []; let message = "Done"; if (convertTo === "ico") { // 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"; } - 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) => { - exec(command, (error, stdout, stderr) => { + execFile("ffmpeg", args, (error, stdout, stderr) => { if (error) { reject(`error: ${error}`); } diff --git a/src/converters/graphicsmagick.ts b/src/converters/graphicsmagick.ts index 78a951d..3186668 100644 --- a/src/converters/graphicsmagick.ts +++ b/src/converters/graphicsmagick.ts @@ -1,339 +1,340 @@ -import { exec } from "node:child_process"; - -export const properties = { - from: { - image: [ - "3fr", - "8bim", - "8bimtext", - "8bimwtext", - "app1", - "app1jpeg", - "art", - "arw", - "avs", - "b", - "bie", - "bigtiff", - "bmp", - "c", - "cals", - "caption", - "cin", - "cmyk", - "cmyka", - "cr2", - "crw", - "cur", - "cut", - "dcm", - "dcr", - "dcx", - "dng", - "dpx", - "epdf", - "epi", - "eps", - "epsf", - "epsi", - "ept", - "ept2", - "ept3", - "erf", - "exif", - "fax", - "file", - "fits", - "fractal", - "ftp", - "g", - "gif", - "gif87", - "gradient", - "gray", - "graya", - "heic", - "heif", - "hrz", - "http", - "icb", - "icc", - "icm", - "ico", - "icon", - "identity", - "image", - "iptc", - "iptctext", - "iptcwtext", - "jbg", - "jbig", - "jng", - "jnx", - "jpeg", - "jpg", - "k", - "k25", - "kdc", - "label", - "m", - "mac", - "map", - "mat", - "mef", - "miff", - "mng", - "mono", - "mpc", - "mrw", - "msl", - "mtv", - "mvg", - "nef", - "null", - "o", - "orf", - "otb", - "p7", - "pal", - "palm", - "pam", - "pbm", - "pcd", - "pcds", - "pct", - "pcx", - "pdb", - "pdf", - "pef", - "pfa", - "pfb", - "pgm", - "picon", - "pict", - "pix", - "plasma", - "png", - "png00", - "png24", - "png32", - "png48", - "png64", - "png8", - "pnm", - "ppm", - "ps", - "ptif", - "pwp", - "r", - "raf", - "ras", - "rgb", - "rgba", - "rla", - "rle", - "sct", - "sfw", - "sgi", - "sr2", - "srf", - "stegano", - "sun", - "svg", - "svgz", - "text", - "tga", - "tif", - "tiff", - "tile", - "tim", - "topol", - "ttf", - "txt", - "uyvy", - "vda", - "vicar", - "vid", - "viff", - "vst", - "wbmp", - "webp", - "wmf", - "wpg", - "x3f", - "xbm", - "xc", - "xcf", - "xmp", - "xpm", - "xv", - "xwd", - "y", - "yuv", - ], - }, - to: { - image: [ - "8bim", - "8bimtext", - "8bimwtext", - "app1", - "app1jpeg", - "art", - "avs", - "b", - "bie", - "bigtiff", - "bmp", - "bmp2", - "bmp3", - "brf", - "c", - "cals", - "cin", - "cmyk", - "cmyka", - "dcx", - "dpx", - "epdf", - "epi", - "eps", - "eps2", - "eps3", - "epsf", - "epsi", - "ept", - "ept2", - "ept3", - "exif", - "fax", - "fits", - "g", - "gif", - "gif87", - "gray", - "graya", - "histogram", - "html", - "icb", - "icc", - "icm", - "info", - "iptc", - "iptctext", - "iptcwtext", - "isobrl", - "isobrl6", - "jbg", - "jbig", - "jng", - "jpeg", - "k", - "m", - "m2v", - "map", - "mat", - "matte", - "miff", - "mng", - "mono", - "mpc", - "mpeg", - "mpg", - "msl", - "mtv", - "mvg", - "null", - "o", - "otb", - "p7", - "pal", - "pam", - "pbm", - "pcd", - "pcds", - "pcl", - "pct", - "pcx", - "pdb", - "pdf", - "pgm", - "picon", - "pict", - "png", - "png00", - "png24", - "png32", - "png48", - "png64", - "png8", - "pnm", - "ppm", - "preview", - "ps", - "ps2", - "ps3", - "ptif", - "r", - "ras", - "rgb", - "rgba", - "sgi", - "shtml", - "sun", - "text", - "tga", - "tiff", - "txt", - "ubrl", - "ubrl6", - "uil", - "uyvy", - "vda", - "vicar", - "vid", - "viff", - "vst", - "wbmp", - "webp", - "x", - "xbm", - "xmp", - "xpm", - "xv", - "xwd", - "y", - "yuv", - ], - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - return new Promise((resolve, reject) => { - exec( - `gm convert "${filePath}" "${targetPath}"`, - (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }, - ); - }); -} +import { execFile } from "node:child_process"; + +export const properties = { + from: { + image: [ + "3fr", + "8bim", + "8bimtext", + "8bimwtext", + "app1", + "app1jpeg", + "art", + "arw", + "avs", + "b", + "bie", + "bigtiff", + "bmp", + "c", + "cals", + "caption", + "cin", + "cmyk", + "cmyka", + "cr2", + "crw", + "cur", + "cut", + "dcm", + "dcr", + "dcx", + "dng", + "dpx", + "epdf", + "epi", + "eps", + "epsf", + "epsi", + "ept", + "ept2", + "ept3", + "erf", + "exif", + "fax", + "file", + "fits", + "fractal", + "ftp", + "g", + "gif", + "gif87", + "gradient", + "gray", + "graya", + "heic", + "heif", + "hrz", + "http", + "icb", + "icc", + "icm", + "ico", + "icon", + "identity", + "image", + "iptc", + "iptctext", + "iptcwtext", + "jbg", + "jbig", + "jng", + "jnx", + "jpeg", + "jpg", + "k", + "k25", + "kdc", + "label", + "m", + "mac", + "map", + "mat", + "mef", + "miff", + "mng", + "mono", + "mpc", + "mrw", + "msl", + "mtv", + "mvg", + "nef", + "null", + "o", + "orf", + "otb", + "p7", + "pal", + "palm", + "pam", + "pbm", + "pcd", + "pcds", + "pct", + "pcx", + "pdb", + "pdf", + "pef", + "pfa", + "pfb", + "pgm", + "picon", + "pict", + "pix", + "plasma", + "png", + "png00", + "png24", + "png32", + "png48", + "png64", + "png8", + "pnm", + "ppm", + "ps", + "ptif", + "pwp", + "r", + "raf", + "ras", + "rgb", + "rgba", + "rla", + "rle", + "sct", + "sfw", + "sgi", + "sr2", + "srf", + "stegano", + "sun", + "svg", + "svgz", + "text", + "tga", + "tif", + "tiff", + "tile", + "tim", + "topol", + "ttf", + "txt", + "uyvy", + "vda", + "vicar", + "vid", + "viff", + "vst", + "wbmp", + "webp", + "wmf", + "wpg", + "x3f", + "xbm", + "xc", + "xcf", + "xmp", + "xpm", + "xv", + "xwd", + "y", + "yuv", + ], + }, + to: { + image: [ + "8bim", + "8bimtext", + "8bimwtext", + "app1", + "app1jpeg", + "art", + "avs", + "b", + "bie", + "bigtiff", + "bmp", + "bmp2", + "bmp3", + "brf", + "c", + "cals", + "cin", + "cmyk", + "cmyka", + "dcx", + "dpx", + "epdf", + "epi", + "eps", + "eps2", + "eps3", + "epsf", + "epsi", + "ept", + "ept2", + "ept3", + "exif", + "fax", + "fits", + "g", + "gif", + "gif87", + "gray", + "graya", + "histogram", + "html", + "icb", + "icc", + "icm", + "info", + "iptc", + "iptctext", + "iptcwtext", + "isobrl", + "isobrl6", + "jbg", + "jbig", + "jng", + "jpeg", + "k", + "m", + "m2v", + "map", + "mat", + "matte", + "miff", + "mng", + "mono", + "mpc", + "mpeg", + "mpg", + "msl", + "mtv", + "mvg", + "null", + "o", + "otb", + "p7", + "pal", + "pam", + "pbm", + "pcd", + "pcds", + "pcl", + "pct", + "pcx", + "pdb", + "pdf", + "pgm", + "picon", + "pict", + "png", + "png00", + "png24", + "png32", + "png48", + "png64", + "png8", + "pnm", + "ppm", + "preview", + "ps", + "ps2", + "ps3", + "ptif", + "r", + "ras", + "rgb", + "rgba", + "sgi", + "shtml", + "sun", + "text", + "tga", + "tiff", + "txt", + "ubrl", + "ubrl6", + "uil", + "uyvy", + "vda", + "vicar", + "vid", + "viff", + "vst", + "wbmp", + "webp", + "x", + "xbm", + "xmp", + "xpm", + "xv", + "xwd", + "y", + "yuv", + ], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + return new Promise((resolve, reject) => { + execFile( + "gm", + ["convert", filePath, targetPath], + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }, + ); + }); +} diff --git a/src/converters/inkscape.ts b/src/converters/inkscape.ts index 1534d17..7af7163 100644 --- a/src/converters/inkscape.ts +++ b/src/converters/inkscape.ts @@ -1,64 +1,59 @@ -import { exec } from "node:child_process"; +import { execFile } from "node:child_process"; export const properties = { - from: { - images: [ - "svg", - "pdf", - "eps", - "ps", - "wmf", - "emf", - "png" - ] - }, - to: { - images: [ - "dxf", - "emf", - "eps", - "fxg", - "gpl", - "hpgl", - "html", - "odg", - "pdf", - "png", - "pov", - "ps", - "sif", - "svg", - "svgz", - "tex", - "wmf", - ] - }, - }; + from: { + images: ["svg", "pdf", "eps", "ps", "wmf", "emf", "png"], + }, + to: { + images: [ + "dxf", + "emf", + "eps", + "fxg", + "gpl", + "hpgl", + "html", + "odg", + "pdf", + "png", + "pov", + "ps", + "sif", + "svg", + "svgz", + "tex", + "wmf", + ], + }, +}; - export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, - ): Promise { - return new Promise((resolve, reject) => { - exec(`inkscape "${filePath}" -o "${targetPath}"`, (error, stdout, stderr) => { +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + return new Promise((resolve, reject) => { + execFile( + "inkscape", + [filePath, "-o", targetPath], + (error, stdout, stderr) => { if (error) { reject(`error: ${error}`); } - + if (stdout) { console.log(`stdout: ${stdout}`); } - + if (stderr) { console.error(`stderr: ${stderr}`); } - + resolve("Done"); - }); - }); - } - \ No newline at end of file + }, + ); + }); +} diff --git a/src/converters/libjxl.ts b/src/converters/libjxl.ts index fbf61bd..d08d6fd 100644 --- a/src/converters/libjxl.ts +++ b/src/converters/libjxl.ts @@ -1,71 +1,71 @@ -import { exec } from "node:child_process"; - -// declare possible conversions -export const properties = { - from: { - jxl: ["jxl"], - images: [ - "apng", - "exr", - "gif", - "jpeg", - "pam", - "pfm", - "pgm", - "pgx", - "png", - "ppm", - ], - }, - to: { - jxl: [ - "apng", - "exr", - "gif", - "jpeg", - "pam", - "pfm", - "pgm", - "pgx", - "png", - "ppm", - ], - images: ["jxl"], - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - let tool = ""; - if (fileType === "jxl") { - tool = "djxl"; - } - - if (convertTo === "jxl") { - tool = "cjxl"; - } - - return new Promise((resolve, reject) => { - exec(`${tool} "${filePath}" "${targetPath}"`, (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }); - }); -} +import { execFile } from "node:child_process"; + +// declare possible conversions +export const properties = { + from: { + jxl: ["jxl"], + images: [ + "apng", + "exr", + "gif", + "jpeg", + "pam", + "pfm", + "pgm", + "pgx", + "png", + "ppm", + ], + }, + to: { + jxl: [ + "apng", + "exr", + "gif", + "jpeg", + "pam", + "pfm", + "pgm", + "pgx", + "png", + "ppm", + ], + images: ["jxl"], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + let tool = ""; + if (fileType === "jxl") { + tool = "djxl"; + } + + if (convertTo === "jxl") { + tool = "cjxl"; + } + + return new Promise((resolve, reject) => { + execFile(tool, [filePath, targetPath], (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }); + }); +} diff --git a/src/converters/pandoc.ts b/src/converters/pandoc.ts index 36053f1..a3995fa 100644 --- a/src/converters/pandoc.ts +++ b/src/converters/pandoc.ts @@ -1,156 +1,162 @@ -import { exec } from "node:child_process"; - -export const properties = { - from: { - text: [ - "textile", - "tikiwiki", - "tsv", - "twiki", - "typst", - "vimwiki", - "biblatex", - "bibtex", - "bits", - "commonmark", - "commonmark_x", - "creole", - "csljson", - "csv", - "djot", - "docbook", - "docx", - "dokuwiki", - "endnotexml", - "epub", - "fb2", - "gfm", - "haddock", - "html", - "ipynb", - "jats", - "jira", - "json", - "latex", - "man", - "markdown", - "markdown_mmd", - "markdown_phpextra", - "markdown_strict", - "mediawiki", - "muse", - "pandoc native", - "opml", - "org", - "ris", - "rst", - "rtf", - "t2t", - ], - }, - to: { - text: [ - "tei", - "texinfo", - "textile", - "typst", - "xwiki", - "zimwiki", - "asciidoc", - "asciidoc_legacy", - "asciidoctor", - "beamer", - "biblatex", - "bibtex", - "chunkedhtml", - "commonmark", - "commonmark_x", - "context", - "csljson", - "djot", - "docbook", - "docbook4", - "docbook5", - "docx", - "dokuwiki", - "dzslides", - "epub", - "epub2", - "epub3", - "fb2", - "gfm", - "haddock", - "html", - "html4", - "html5", - "icml", - "ipynb", - "jats", - "jats_archiving", - "jats_articleauthoring", - "jats_publishing", - "jira", - "json", - "latex", - "man", - "markdown", - "markdown_mmd", - "markdown_phpextra", - "markdown_strict", - "markua", - "mediawiki", - "ms", - "muse", - "pandoc native", - "odt", - "opendocument", - "opml", - "org", - "pdf", - "plain", - "pptx", - "revealjs", - "rst", - "rtf", - "s5", - "slideous", - "slidy", - ], - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - // set xelatex here - const xelatex = ["pdf", "latex"]; - let option = ""; - if (xelatex.includes(convertTo)) { - option = "--pdf-engine=xelatex"; - } - return new Promise((resolve, reject) => { - exec( - `pandoc ${option} "${filePath}" -f ${fileType} -t ${convertTo} -o "${targetPath}"`, - (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }, - ); - }); -} +import { execFile } from "node:child_process"; + +export const properties = { + from: { + text: [ + "textile", + "tikiwiki", + "tsv", + "twiki", + "typst", + "vimwiki", + "biblatex", + "bibtex", + "bits", + "commonmark", + "commonmark_x", + "creole", + "csljson", + "csv", + "djot", + "docbook", + "docx", + "dokuwiki", + "endnotexml", + "epub", + "fb2", + "gfm", + "haddock", + "html", + "ipynb", + "jats", + "jira", + "json", + "latex", + "man", + "markdown", + "markdown_mmd", + "markdown_phpextra", + "markdown_strict", + "mediawiki", + "muse", + "pandoc native", + "opml", + "org", + "ris", + "rst", + "rtf", + "t2t", + ], + }, + to: { + text: [ + "tei", + "texinfo", + "textile", + "typst", + "xwiki", + "zimwiki", + "asciidoc", + "asciidoc_legacy", + "asciidoctor", + "beamer", + "biblatex", + "bibtex", + "chunkedhtml", + "commonmark", + "commonmark_x", + "context", + "csljson", + "djot", + "docbook", + "docbook4", + "docbook5", + "docx", + "dokuwiki", + "dzslides", + "epub", + "epub2", + "epub3", + "fb2", + "gfm", + "haddock", + "html", + "html4", + "html5", + "icml", + "ipynb", + "jats", + "jats_archiving", + "jats_articleauthoring", + "jats_publishing", + "jira", + "json", + "latex", + "man", + "markdown", + "markdown_mmd", + "markdown_phpextra", + "markdown_strict", + "markua", + "mediawiki", + "ms", + "muse", + "pandoc native", + "odt", + "opendocument", + "opml", + "org", + "pdf", + "plain", + "pptx", + "revealjs", + "rst", + "rtf", + "s5", + "slideous", + "slidy", + ], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + // set xelatex here + const xelatex = ["pdf", "latex"]; + + // Build arguments array + const args: string[] = []; + + if (xelatex.includes(convertTo)) { + args.push("--pdf-engine=xelatex"); + } + + args.push(filePath); + args.push("-f", fileType); + args.push("-t", convertTo); + args.push("-o", targetPath); + + return new Promise((resolve, reject) => { + execFile("pandoc", args, (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }); + }); +} diff --git a/src/converters/resvg.ts b/src/converters/resvg.ts index 0fb9ca5..9ea3745 100644 --- a/src/converters/resvg.ts +++ b/src/converters/resvg.ts @@ -1,37 +1,37 @@ -import { exec } from "node:child_process"; - -export const properties = { - from: { - images: ["svg"], - }, - to: { - images: ["png"], - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - return new Promise((resolve, reject) => { - exec(`resvg "${filePath}" "${targetPath}"`, (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }); - }); -} +import { execFile } from "node:child_process"; + +export const properties = { + from: { + images: ["svg"], + }, + to: { + images: ["png"], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + return new Promise((resolve, reject) => { + execFile("resvg", [filePath, targetPath], (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }); + }); +} diff --git a/src/converters/vips.ts b/src/converters/vips.ts index bf49907..6781359 100644 --- a/src/converters/vips.ts +++ b/src/converters/vips.ts @@ -1,142 +1,142 @@ -import { exec } from "node:child_process"; - - -// declare possible conversions -export const properties = { - from: { - images: [ - "avif", - "bif", - "csv", - "exr", - "fits", - "gif", - "hdr.gz", - "hdr", - "heic", - "heif", - "img.gz", - "img", - "j2c", - "j2k", - "jp2", - "jpeg", - "jpx", - "jxl", - "mat", - "mrxs", - "ndpi", - "nia.gz", - "nia", - "nii.gz", - "nii", - "pdf", - "pfm", - "pgm", - "pic", - "png", - "ppm", - "raw", - "scn", - "svg", - "svs", - "svslide", - "szi", - "tif", - "tiff", - "v", - "vips", - "vms", - "vmu", - "webp", - "zip", - ], - }, - to: { - images: [ - "avif", - "dzi", - "fits", - "gif", - "hdr.gz", - "heic", - "heif", - "img.gz", - "j2c", - "j2k", - "jp2", - "jpeg", - "jpx", - "jxl", - "mat", - "nia.gz", - "nia", - "nii.gz", - "nii", - "png", - "tiff", - "vips", - "webp", - ], - }, - options: { - svg: { - scale: { - description: "Scale the image up or down", - type: "number", - default: 1, - }, - }, - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - // if (fileType === "svg") { - // const scale = options.scale || 1; - // const metadata = await sharp(filePath).metadata(); - - // if (!metadata || !metadata.width || !metadata.height) { - // throw new Error("Could not get metadata from image"); - // } - - // const newWidth = Math.round(metadata.width * scale); - // const newHeight = Math.round(metadata.height * scale); - - // return await sharp(filePath) - // .resize(newWidth, newHeight) - // .toFormat(convertTo) - // .toFile(targetPath); - // } - let action = "copy"; - if (fileType === "pdf") { - action = "pdfload"; - } - - return new Promise((resolve, reject) => { - exec( - `vips ${action} "${filePath}" "${targetPath}"`, - (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }, - ); - }); -} \ No newline at end of file +import { execFile } from "node:child_process"; + +// declare possible conversions +export const properties = { + from: { + images: [ + "avif", + "bif", + "csv", + "exr", + "fits", + "gif", + "hdr.gz", + "hdr", + "heic", + "heif", + "img.gz", + "img", + "j2c", + "j2k", + "jp2", + "jpeg", + "jpx", + "jxl", + "mat", + "mrxs", + "ndpi", + "nia.gz", + "nia", + "nii.gz", + "nii", + "pdf", + "pfm", + "pgm", + "pic", + "png", + "ppm", + "raw", + "scn", + "svg", + "svs", + "svslide", + "szi", + "tif", + "tiff", + "v", + "vips", + "vms", + "vmu", + "webp", + "zip", + ], + }, + to: { + images: [ + "avif", + "dzi", + "fits", + "gif", + "hdr.gz", + "heic", + "heif", + "img.gz", + "j2c", + "j2k", + "jp2", + "jpeg", + "jpx", + "jxl", + "mat", + "nia.gz", + "nia", + "nii.gz", + "nii", + "png", + "tiff", + "vips", + "webp", + ], + }, + options: { + svg: { + scale: { + description: "Scale the image up or down", + type: "number", + default: 1, + }, + }, + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + // if (fileType === "svg") { + // const scale = options.scale || 1; + // const metadata = await sharp(filePath).metadata(); + + // if (!metadata || !metadata.width || !metadata.height) { + // throw new Error("Could not get metadata from image"); + // } + + // const newWidth = Math.round(metadata.width * scale); + // const newHeight = Math.round(metadata.height * scale); + + // return await sharp(filePath) + // .resize(newWidth, newHeight) + // .toFormat(convertTo) + // .toFile(targetPath); + // } + let action = "copy"; + if (fileType === "pdf") { + action = "pdfload"; + } + + return new Promise((resolve, reject) => { + execFile( + "vips", + [action, filePath, targetPath], + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }, + ); + }); +} diff --git a/src/converters/xelatex.ts b/src/converters/xelatex.ts index 73cd8f5..905202c 100644 --- a/src/converters/xelatex.ts +++ b/src/converters/xelatex.ts @@ -1,46 +1,53 @@ -import { exec } from "node:child_process"; - -export const properties = { - from: { - text: ["tex", "latex"], - }, - to: { - text: ["pdf"], - }, -}; - -export function convert( - filePath: string, - fileType: string, - convertTo: string, - targetPath: string, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - options?: unknown, -): Promise { - return new Promise((resolve, reject) => { - // const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "") - const outputPath = targetPath - .split("/") - .slice(0, -1) - .join("/") - .replace("./", ""); - exec( - `latexmk -xelatex -interaction=nonstopmode -output-directory="${outputPath}" "${filePath}"`, - (error, stdout, stderr) => { - if (error) { - reject(`error: ${error}`); - } - - if (stdout) { - console.log(`stdout: ${stdout}`); - } - - if (stderr) { - console.error(`stderr: ${stderr}`); - } - - resolve("Done"); - }, - ); - }); -} +import { execFile } from "node:child_process"; + +export const properties = { + from: { + text: ["tex", "latex"], + }, + to: { + text: ["pdf"], + }, +}; + +export function convert( + filePath: string, + fileType: string, + convertTo: string, + targetPath: string, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + options?: unknown, +): Promise { + return new Promise((resolve, reject) => { + // const fileName: string = (targetPath.split("/").pop() as string).replace(".pdf", "") + const outputPath = targetPath + .split("/") + .slice(0, -1) + .join("/") + .replace("./", ""); + + execFile( + "latexmk", + [ + "-xelatex", + "-interaction=nonstopmode", + `-output-directory=${outputPath}`, + filePath, + ], + (error, stdout, stderr) => { + if (error) { + reject(`error: ${error}`); + } + + if (stdout) { + console.log(`stdout: ${stdout}`); + } + + if (stderr) { + console.error(`stderr: ${stderr}`); + } + + resolve("Done"); + }, + ); + }); +}