refactor(errors): extract isErrorLike and use ExecaError class in tts
This commit is contained in:
parent
bf5e223abf
commit
5b138df664
2 changed files with 13 additions and 8 deletions
|
|
@ -1,13 +1,18 @@
|
||||||
import { chalkStderr } from "chalk";
|
import { chalkStderr } from "chalk";
|
||||||
import wrapAnsi from "wrap-ansi";
|
import wrapAnsi from "wrap-ansi";
|
||||||
|
|
||||||
|
export interface ErrorLike {
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const isErrorLike = (err: unknown): err is ErrorLike =>
|
||||||
|
typeof err === "object" &&
|
||||||
|
err !== null &&
|
||||||
|
"message" in err &&
|
||||||
|
typeof err.message === "string";
|
||||||
|
|
||||||
export function handleTopLevel(err: unknown) {
|
export function handleTopLevel(err: unknown) {
|
||||||
if (
|
if (isErrorLike(err)) {
|
||||||
typeof err === "object" &&
|
|
||||||
err !== null &&
|
|
||||||
"message" in err &&
|
|
||||||
typeof err.message === "string"
|
|
||||||
) {
|
|
||||||
if (process.stderr.isTTY) {
|
if (process.stderr.isTTY) {
|
||||||
const [columns] = process.stderr.getWindowSize();
|
const [columns] = process.stderr.getWindowSize();
|
||||||
console.error(wrapAnsi(chalkStderr.red(err.message), columns));
|
console.error(wrapAnsi(chalkStderr.red(err.message), columns));
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import consumers from "node:stream/consumers";
|
||||||
import util from "node:util";
|
import util from "node:util";
|
||||||
import { Readability } from "@mozilla/readability";
|
import { Readability } from "@mozilla/readability";
|
||||||
import chalk from "chalk";
|
import chalk from "chalk";
|
||||||
import { execa } from "execa";
|
import { ExecaError, execa } from "execa";
|
||||||
import { Window } from "happy-dom";
|
import { Window } from "happy-dom";
|
||||||
import wrapAnsi from "wrap-ansi";
|
import wrapAnsi from "wrap-ansi";
|
||||||
import { handleTopLevel } from "./errors";
|
import { handleTopLevel } from "./errors";
|
||||||
|
|
@ -151,7 +151,7 @@ async function main() {
|
||||||
try {
|
try {
|
||||||
await execa({ cwd, shell: true })`sox ${files} output.wav`;
|
await execa({ cwd, shell: true })`sox ${files} output.wav`;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
if (err instanceof ExecaError && err.code === "ENOENT") {
|
||||||
console.error("Sox not found. Unable to combine outputs.");
|
console.error("Sox not found. Unable to combine outputs.");
|
||||||
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue