55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
export async function checkFileExists(filePath: string): Promise<boolean> {
|
|
try {
|
|
await Deno.lstat(filePath);
|
|
return true;
|
|
} catch (error) {
|
|
if (error instanceof Deno.errors.NotFound) {
|
|
return false;
|
|
} else {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function checkExecutableExists(name: string): Promise<boolean> {
|
|
try {
|
|
const command = new Deno.Command("which", { args: [name] });
|
|
const { success } = await command.output();
|
|
return success;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function createSubtrTmpDirectories(): Promise<void> {
|
|
const dirs = [
|
|
"/tmp/substr/img/",
|
|
];
|
|
|
|
for (const path of dirs) {
|
|
await Deno.mkdir(path, { recursive: true });
|
|
}
|
|
}
|
|
|
|
export async function runCommand(cmd: string, args: string[]) {
|
|
const command = new Deno.Command(cmd, { args });
|
|
const { code, success, stdout, stderr } = await command.output();
|
|
|
|
if (code === 1) {
|
|
console.log(new TextDecoder().decode(stdout));
|
|
console.log(new TextDecoder().decode(stderr));
|
|
}
|
|
|
|
return { success, stdout, stderr };
|
|
}
|
|
|
|
export async function getImageMagickCommand(): Promise<string | undefined> {
|
|
if (await checkExecutableExists("magick")) {
|
|
return "magick";
|
|
} else if (await checkExecutableExists("convert")) {
|
|
return "convert";
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|