refactor(tts): CLI argument parsing and input handling
This commit is contained in:
parent
749559e19c
commit
dd6df6dd6d
1 changed files with 31 additions and 21 deletions
52
tts.ts
52
tts.ts
|
|
@ -12,41 +12,34 @@ import { execa } from "execa";
|
|||
import { Window } from "happy-dom";
|
||||
import wrapAnsi from "wrap-ansi";
|
||||
|
||||
class InputError extends Error {
|
||||
override name = "InputError";
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const start = Date.now();
|
||||
|
||||
const { values: args } = util.parseArgs({
|
||||
const { values: args, positionals } = util.parseArgs({
|
||||
args: process.argv.slice(2),
|
||||
options: {
|
||||
wait: { type: "boolean" },
|
||||
w: { type: "boolean" },
|
||||
url: { type: "string" },
|
||||
u: { type: "string" },
|
||||
url: { type: "boolean" },
|
||||
u: { type: "boolean" },
|
||||
edit: { type: "boolean" },
|
||||
e: { type: "boolean" },
|
||||
},
|
||||
strict: true,
|
||||
allowPositionals: false,
|
||||
allowPositionals: true,
|
||||
});
|
||||
|
||||
const wait = args.wait ?? args.w;
|
||||
const url = args.url ?? args.u;
|
||||
const edit = args.edit ?? args.e;
|
||||
|
||||
let input = "";
|
||||
let input = positionals.join(" ").trim();
|
||||
|
||||
if (url?.trim()) {
|
||||
const result = await fetch(url.trim());
|
||||
const window = new Window({ url: url.trim() });
|
||||
const doc = window.document;
|
||||
doc.write(await result.text());
|
||||
await window.happyDOM.waitUntilComplete();
|
||||
const readable = new Readability(doc).parse();
|
||||
if (!readable?.textContent) {
|
||||
throw "Unable to read web page.";
|
||||
}
|
||||
input = readable.textContent.trim();
|
||||
} else {
|
||||
if (!input) {
|
||||
process.stdin.setEncoding("utf8");
|
||||
process.stdin.on("data", (data) => {
|
||||
input += data;
|
||||
|
|
@ -55,7 +48,24 @@ async function main() {
|
|||
}
|
||||
|
||||
if (!input) {
|
||||
throw "No input given.";
|
||||
throw new InputError(
|
||||
"No input was given. Use positional arguments or stdin.",
|
||||
);
|
||||
}
|
||||
|
||||
if (url) {
|
||||
const result = await fetch(input);
|
||||
const window = new Window({ url: input });
|
||||
const doc = window.document;
|
||||
doc.write(await result.text());
|
||||
await window.happyDOM.waitUntilComplete();
|
||||
const readable = new Readability(doc).parse();
|
||||
if (!readable?.textContent) {
|
||||
throw new InputError(
|
||||
"Unable to read webpage. This usually means it's blocking scrapers.",
|
||||
);
|
||||
}
|
||||
input = readable.textContent.trim();
|
||||
}
|
||||
|
||||
const home = os.homedir();
|
||||
|
|
@ -63,7 +73,7 @@ async function main() {
|
|||
|
||||
if (edit) {
|
||||
const file = path.join(cwd, "input.txt");
|
||||
await fs.writeFile(file, input);
|
||||
await fs.writeFile(file, input, "utf8");
|
||||
const editor = process.env.EDITOR || "nano";
|
||||
await execa({
|
||||
cwd,
|
||||
|
|
@ -154,11 +164,11 @@ async function main() {
|
|||
} catch (err) {
|
||||
if (err instanceof Object && "code" in err && err.code === "ENOENT") {
|
||||
console.error("Sox not found. Unable to combine outputs.");
|
||||
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
||||
return;
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
console.log(chalk.yellow(`Audio available in ${cwd}`));
|
||||
return;
|
||||
}
|
||||
|
||||
const [columns] = process.stdout.getWindowSize();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue