Skip to content

Commit

Permalink
handle unhandled exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahsin authored and Tahsin committed Dec 15, 2024
1 parent c52df35 commit ea90e05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
33 changes: 17 additions & 16 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { Operations } from "./types";
import { runShellCommandAndReturnLine } from "./utils";
import { Operations, type Binary } from "./types";
import { checkIfBinaryExists } from "./utils";

const checkIfBinaryExists = async (binary: string) => {
const lines: string[] = [];
await runShellCommandAndReturnLine(`which ${binary}`, (l) => l && lines.push(l));
return lines.length > 0;
};

const commonRequiredBinaries = [
export const commonRequiredBinaries: Binary[] = [
{
name: "gum",
purpose: "To convert audio files to mp3.",
purpose: "Gum is a CLI tool to interact with the terminal.",
howTo: `Install gum from: https://github.com/charmbracelet/gum`,
},
];

const requiredBinariesForVideo = [
const requiredBinariesForVideo: Binary[] = [
...commonRequiredBinaries,
{
name: "HandBrakeCLI",
Expand All @@ -24,7 +18,7 @@ const requiredBinariesForVideo = [
},
];

const requiredBinariesForAudio = [
const requiredBinariesForAudio: Binary[] = [
...commonRequiredBinaries,
{
name: "ffmpeg",
Expand All @@ -41,14 +35,20 @@ const requiredBinariesForAudio = [
const mappedByOp = {
[Operations.VIDEO_COMPRESS]: requiredBinariesForVideo,
[Operations.AUDIO_COMPRESS]: requiredBinariesForAudio,
[Operations.GENERAL]: commonRequiredBinaries,
};

export const checkRequiredBinaries = async (op: Operations) => {
const missing = [];
export const checkRequiredBinaries = (op: Operations) => {
const requiredBinaries = mappedByOp[op] as { name: string; purpose: string; howTo: string }[];

for (const binary of requiredBinaries) {
const isFound = await checkIfBinaryExists(binary.name);
checkBinaries(requiredBinaries);
};

export const checkBinaries = (binaries: Binary[]) => {
const missing = [];

for (const binary of binaries) {
const isFound = checkIfBinaryExists(binary.name);
if (!isFound) missing.push(binary);
}

Expand All @@ -59,6 +59,7 @@ export const checkRequiredBinaries = async (op: Operations) => {
console.error(`- ${binary.name}: ${binary.purpose}`);
console.error(binary.howTo);
}

process.exit(1);
}
};
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { askBoolean, askChoose, askFiles, askFilter, askPreset } from "./prompts";
import { Operations } from "./types";
import { askBoolean, askChoose, askFiles, askFilter, askPreset, showErrorAndExit } from "./prompts";
import { compressVideo, compressAudio } from "./engine";
import { checkRequiredBinaries } from "./check";
import { Operations } from "./types";

process.on("uncaughtException", (error) => {
const stackFirstLine = error.stack?.split("\n")[1] as string;
showErrorAndExit([error.message, stackFirstLine]);
});

process.on("unhandledRejection", (error) => {
showErrorAndExit([error as string]);
});

const videoCompress = async () => {
await checkRequiredBinaries(Operations.VIDEO_COMPRESS);
checkRequiredBinaries(Operations.VIDEO_COMPRESS);

const files = await askFiles(["mp4", "mkv", "avi", "mov", "flv", "wmv", "webm", "m4v", "lrf"]);

const preset = await askPreset();

const keepAudio = await askBoolean("Do you want to keep the audio?", "true");

await compressVideo(files, preset, { keepAudio });
Expand All @@ -29,7 +36,7 @@ const videoCompress = async () => {
};

const audioCompress = async () => {
await checkRequiredBinaries(Operations.AUDIO_COMPRESS);
checkRequiredBinaries(Operations.AUDIO_COMPRESS);

const files = await askFiles(["mp3", "wav", "flac", "m4a", "aac", "ogg", "wma", "aiff", "alac"]);
const bitrate = await askFilter("Select a bitrate", ["16k", "32k", "64k", "128k", "256k", "320k"], { limit: 1, min: 1 });
Expand All @@ -39,14 +46,17 @@ const audioCompress = async () => {
}
};

const fnMap: Record<Operations, () => Promise<void>> = {
const fnMap: Record<any, () => Promise<void>> = {
[Operations.VIDEO_COMPRESS]: videoCompress,
[Operations.AUDIO_COMPRESS]: audioCompress,
};

const root = async () => {
checkRequiredBinaries(Operations.GENERAL);

const choice = await askChoose("Select an operation", Object.values(Operations));
const op = choice[0] as Operations;
if (!op) throw new Error("No operation selected. Exiting.");

await fnMap[op]();
};
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export enum Operations {
AUDIO_COMPRESS = "AUDIO_COMPRESS",
VIDEO_COMPRESS = "VIDEO_COMPRESS",
GENERAL = "GENERAL",
}

export type Binary = {
name: string;
purpose: string;
howTo: string;
};

0 comments on commit ea90e05

Please sign in to comment.