refactor(tts): replace Bun-specific APIs with execa and Node.js stdlib
- Replace `$` and `Bun.spawn` with `execa` for process execution - Swap `Bun.JSONL.parse` for native `JSON.parse` and use `node:readline` for stdin handling
This commit is contained in:
parent
4912864a75
commit
3235a08633
3 changed files with 277 additions and 83 deletions
89
tts.ts
89
tts.ts
|
|
@ -1,15 +1,17 @@
|
|||
import os from "node:os";
|
||||
import process from "node:process";
|
||||
import { createInterface } from "node:readline";
|
||||
import { parseArgs } from "node:util";
|
||||
import { Readability } from "@mozilla/readability";
|
||||
import { $, which } from "bun";
|
||||
import chalk from "chalk";
|
||||
import { Window } from "happy-dom-without-node";
|
||||
import { execa } from "execa";
|
||||
import { Window } from "happy-dom";
|
||||
import wrapAnsi from "wrap-ansi";
|
||||
|
||||
const start = Date.now();
|
||||
|
||||
const { values: args } = parseArgs({
|
||||
args: Bun.argv,
|
||||
args: process.argv,
|
||||
options: {
|
||||
wait: { type: "boolean" },
|
||||
w: { type: "boolean" },
|
||||
|
|
@ -22,7 +24,15 @@ const { values: args } = parseArgs({
|
|||
|
||||
const wait = args.wait ?? args.w;
|
||||
const url = args.url ?? args.u;
|
||||
let input = url ? url : await Bun.stdin.text();
|
||||
|
||||
let input = url
|
||||
? url
|
||||
: await new Promise<string>((resolve) => {
|
||||
let data = "";
|
||||
process.stdin.setEncoding("utf8");
|
||||
process.stdin.on("data", (chunk) => (data += chunk));
|
||||
process.stdin.on("end", () => resolve(data));
|
||||
});
|
||||
|
||||
if (url !== undefined) {
|
||||
const result = await fetch(input.trim());
|
||||
|
|
@ -45,9 +55,8 @@ if (!input) {
|
|||
|
||||
const home = os.homedir();
|
||||
const cwd = `${home}/.ttsc/`;
|
||||
$.cwd(cwd);
|
||||
|
||||
await $`rm *.wav`.nothrow();
|
||||
await execa({ cwd, shell: true, reject: false })`rm *.wav`;
|
||||
|
||||
console.log(chalk.blue("Welcome to TTSC!"));
|
||||
console.log(
|
||||
|
|
@ -56,16 +65,16 @@ console.log(
|
|||
),
|
||||
);
|
||||
|
||||
const koko = Bun.spawn({
|
||||
cmd: ["uv", "run", "--", "python", "-u", "main.py"],
|
||||
const koko = execa({
|
||||
cwd,
|
||||
stdin: Buffer.from(input),
|
||||
input: Buffer.from(input), // execa v6+ uses 'input'. Use 'stdin' for v5
|
||||
stdout: "pipe",
|
||||
stderr: "inherit",
|
||||
});
|
||||
})`uv run -- python -u main.py`;
|
||||
|
||||
if (!wait) {
|
||||
koko.exited.then(() => {
|
||||
koko.catch(() => {});
|
||||
koko.then(() => {
|
||||
const stop = Date.now();
|
||||
const duration = ((stop - start) / 1000).toFixed(2);
|
||||
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
||||
|
|
@ -73,15 +82,17 @@ if (!wait) {
|
|||
}
|
||||
|
||||
let first = true;
|
||||
const decoder = new TextDecoder();
|
||||
for await (const line of koko.stdout) {
|
||||
const decoded = decoder.decode(line).trim();
|
||||
const rl = createInterface({ input: koko.stdout, crlfDelay: Infinity });
|
||||
|
||||
let infos: { file: number; text: string }[];
|
||||
for await (const line of rl) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
|
||||
let info: { file: number; text: string };
|
||||
try {
|
||||
infos = Bun.JSONL.parse(decoded) as { file: number; text: string }[];
|
||||
info = JSON.parse(trimmed);
|
||||
} catch {
|
||||
console.log(decoded);
|
||||
console.log(trimmed);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -92,25 +103,22 @@ for await (const line of koko.stdout) {
|
|||
console.log(chalk.green(`\nTime-to-audio: ${duration}s`));
|
||||
}
|
||||
|
||||
for (const info of infos) {
|
||||
const [columns] = process.stdout.getWindowSize();
|
||||
const text = Bun.wrapAnsi(info.text, columns);
|
||||
const file = `${home}/.ttsc/${info.file}.wav`;
|
||||
console.log(`\n${chalk.blue(file)}:\n${text}\n`);
|
||||
const [columns] = process.stdout.getWindowSize();
|
||||
const text = wrapAnsi(info.text, columns);
|
||||
const file = `${home}/.ttsc/${info.file}.wav`;
|
||||
console.log(`\n${chalk.blue(file)}:\n${text}\n`);
|
||||
|
||||
if (!wait) {
|
||||
await Bun.spawn({
|
||||
cmd: ["mpv", file],
|
||||
cwd,
|
||||
stdin: "ignore",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
}).exited;
|
||||
}
|
||||
if (!wait) {
|
||||
await execa({
|
||||
cwd,
|
||||
stdin: "ignore",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
})`mpv ${file}`;
|
||||
}
|
||||
}
|
||||
|
||||
await koko.exited;
|
||||
await koko;
|
||||
if (!wait) process.exit();
|
||||
|
||||
const stop = Date.now();
|
||||
|
|
@ -118,19 +126,22 @@ const duration = ((stop - start) / 1000).toFixed(2);
|
|||
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
||||
|
||||
console.log(chalk.yellow(`Combining audio with sox...`));
|
||||
if (which("sox")) {
|
||||
await $`sox *.wav output.wav`;
|
||||
try {
|
||||
await execa({ cwd, shell: true })`sox *.wav output.wav`;
|
||||
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
|
||||
} else {
|
||||
console.error("Sox not found. Unable to combine outputs.");
|
||||
} catch (err) {
|
||||
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
||||
console.error("Sox not found. Unable to combine outputs.");
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
||||
process.exit();
|
||||
}
|
||||
|
||||
await Bun.spawn({
|
||||
cmd: ["mpv", "--pause", "output.wav"],
|
||||
await execa({
|
||||
cwd,
|
||||
stdin: "ignore",
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
}).exited;
|
||||
})`mpv --pause output.wav`;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue