refactor(tts): make cjs compatible and add sea config
This commit is contained in:
parent
3235a08633
commit
39cb573619
2 changed files with 135 additions and 122 deletions
6
sea-config-tts.json
Normal file
6
sea-config-tts.json
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"main": "dist/tts.js",
|
||||||
|
"output": "dist/ttsc",
|
||||||
|
"useCodeCache": true,
|
||||||
|
"disableExperimentalSEAWarning": true
|
||||||
|
}
|
||||||
83
tts.ts
83
tts.ts
|
|
@ -8,10 +8,11 @@ import { execa } from "execa";
|
||||||
import { Window } from "happy-dom";
|
import { Window } from "happy-dom";
|
||||||
import wrapAnsi from "wrap-ansi";
|
import wrapAnsi from "wrap-ansi";
|
||||||
|
|
||||||
const start = Date.now();
|
async function main() {
|
||||||
|
const start = Date.now();
|
||||||
|
|
||||||
const { values: args } = parseArgs({
|
const { values: args } = parseArgs({
|
||||||
args: process.argv,
|
args: process.argv.slice(2),
|
||||||
options: {
|
options: {
|
||||||
wait: { type: "boolean" },
|
wait: { type: "boolean" },
|
||||||
w: { type: "boolean" },
|
w: { type: "boolean" },
|
||||||
|
|
@ -19,13 +20,13 @@ const { values: args } = parseArgs({
|
||||||
u: { type: "string" },
|
u: { type: "string" },
|
||||||
},
|
},
|
||||||
strict: true,
|
strict: true,
|
||||||
allowPositionals: true,
|
allowPositionals: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
const wait = args.wait ?? args.w;
|
const wait = args.wait ?? args.w;
|
||||||
const url = args.url ?? args.u;
|
const url = args.url ?? args.u;
|
||||||
|
|
||||||
let input = url
|
let input = url
|
||||||
? url
|
? url
|
||||||
: await new Promise<string>((resolve) => {
|
: await new Promise<string>((resolve) => {
|
||||||
let data = "";
|
let data = "";
|
||||||
|
|
@ -34,7 +35,7 @@ let input = url
|
||||||
process.stdin.on("end", () => resolve(data));
|
process.stdin.on("end", () => resolve(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
if (url !== undefined) {
|
if (url !== undefined) {
|
||||||
const result = await fetch(input.trim());
|
const result = await fetch(input.trim());
|
||||||
const window = new Window({ url: input.trim() });
|
const window = new Window({ url: input.trim() });
|
||||||
const doc = window.document;
|
const doc = window.document;
|
||||||
|
|
@ -46,45 +47,45 @@ if (url !== undefined) {
|
||||||
console.error("Unable to read web page.");
|
console.error("Unable to read web page.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!input) {
|
if (!input) {
|
||||||
console.error("No input given.");
|
console.error("No input given.");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const home = os.homedir();
|
const home = os.homedir();
|
||||||
const cwd = `${home}/.ttsc/`;
|
const cwd = `${home}/.ttsc/`;
|
||||||
|
|
||||||
await execa({ cwd, shell: true, reject: false })`rm *.wav`;
|
await execa({ cwd, shell: true, reject: false })`rm *.wav`;
|
||||||
|
|
||||||
console.log(chalk.blue("Welcome to TTSC!"));
|
console.log(chalk.blue("Welcome to TTSC!"));
|
||||||
console.log(
|
console.log(
|
||||||
chalk.yellow(
|
chalk.yellow(
|
||||||
"Beginning TTS processing... (This will take extra time the first time)",
|
"Beginning TTS processing... (This will take extra time the first time)",
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
const koko = execa({
|
const koko = execa({
|
||||||
cwd,
|
cwd,
|
||||||
input: Buffer.from(input), // execa v6+ uses 'input'. Use 'stdin' for v5
|
input,
|
||||||
stdout: "pipe",
|
stdout: "pipe",
|
||||||
stderr: "inherit",
|
stderr: "inherit",
|
||||||
})`uv run -- python -u main.py`;
|
})`uv run -- python -u main.py`;
|
||||||
|
|
||||||
if (!wait) {
|
if (!wait) {
|
||||||
koko.catch(() => {});
|
koko.catch(() => {});
|
||||||
koko.then(() => {
|
koko.then(() => {
|
||||||
const stop = Date.now();
|
const stop = Date.now();
|
||||||
const duration = ((stop - start) / 1000).toFixed(2);
|
const duration = ((stop - start) / 1000).toFixed(2);
|
||||||
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let first = true;
|
let first = true;
|
||||||
const rl = createInterface({ input: koko.stdout, crlfDelay: Infinity });
|
const rl = createInterface({ input: koko.stdout, crlfDelay: Infinity });
|
||||||
|
|
||||||
for await (const line of rl) {
|
for await (const line of rl) {
|
||||||
const trimmed = line.trim();
|
const trimmed = line.trim();
|
||||||
if (!trimmed) continue;
|
if (!trimmed) continue;
|
||||||
|
|
||||||
|
|
@ -116,20 +117,20 @@ for await (const line of rl) {
|
||||||
stderr: "inherit",
|
stderr: "inherit",
|
||||||
})`mpv ${file}`;
|
})`mpv ${file}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await koko;
|
await koko;
|
||||||
if (!wait) process.exit();
|
if (!wait) process.exit();
|
||||||
|
|
||||||
const stop = Date.now();
|
const stop = Date.now();
|
||||||
const duration = ((stop - start) / 1000).toFixed(2);
|
const duration = ((stop - start) / 1000).toFixed(2);
|
||||||
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
console.log(chalk.green(`\nProcessing finished: ${duration}s`));
|
||||||
|
|
||||||
console.log(chalk.yellow(`Combining audio with sox...`));
|
console.log(chalk.yellow(`Combining audio with sox...`));
|
||||||
try {
|
try {
|
||||||
await execa({ cwd, shell: true })`sox *.wav output.wav`;
|
await execa({ cwd, shell: true })`sox *.wav output.wav`;
|
||||||
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
|
console.log(chalk.green(`Playing audio from ${cwd}output.wav`));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
||||||
console.error("Sox not found. Unable to combine outputs.");
|
console.error("Sox not found. Unable to combine outputs.");
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -137,11 +138,17 @@ try {
|
||||||
}
|
}
|
||||||
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
await execa({
|
await execa({
|
||||||
cwd,
|
cwd,
|
||||||
stdin: "ignore",
|
stdin: "ignore",
|
||||||
stdout: "inherit",
|
stdout: "inherit",
|
||||||
stderr: "inherit",
|
stderr: "inherit",
|
||||||
})`mpv --pause output.wav`;
|
})`mpv --pause output.wav`;
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue