gadl/main.ts

174 lines
4.2 KiB
TypeScript

import util from "node:util";
import { chromium } from "./browser";
import { createDatabase } from "./database";
import { type Book, createLibrary } from "./library";
import { createPlatform } from "./platform";
const options = {
db: {
type: "string",
default: "gadl.db",
},
"out-dir": {
type: "string",
default: "dist",
},
"out-authors-limit": {
type: "string",
default: "3",
},
login: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch({ headless: false });
const platform = createPlatform({ db, browser });
await platform.login();
await browser.close();
},
},
logout: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch();
const platform = createPlatform({ db, browser });
await platform.logout();
await browser.close();
},
},
add: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.add(args.values.add!);
},
},
delete: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.delete(Number(args.values.delete));
},
},
list: {
type: "boolean",
short: "l",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
const books = await library.getBooks();
console.dir(books, {
depth: null,
maxArrayLength: null,
maxStringLength: null,
});
},
},
view: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
const book = await library.get(Number(args.values.view!));
if (!book) {
process.exit(1);
}
console.dir(book, {
depth: null,
maxArrayLength: null,
maxStringLength: null,
});
},
},
pull: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
const browser = await chromium.launch();
const platform = createPlatform({ db, browser });
for await (const book of platform.pull()) {
await library.add(book);
}
await browser.close();
},
},
download: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
const books: Array<Book> = [];
if (args.values.download === "all") {
books.push(...(await library.getBooks()));
} else {
if (URL.canParse(args.values.download!)) {
await library.add(args.values.download!);
}
const book = await library.get(Number(args.values.download!));
if (!book) {
process.exit(1);
}
books.push(book);
}
for (const book of books) {
const browser = await chromium.launch();
const platform = createPlatform({ db, browser });
const dir = `${args.values["out-dir"]!}/${book.id}`;
await platform.download(dir, book);
await library.archive(dir, book, {
outDir: args.values["out-dir"]!,
outAuthorsLimit: Number(args.values["out-authors-limit"]!),
});
await browser.close();
}
},
},
json: {
type: "boolean",
},
help: {
type: "boolean",
short: "h",
run() {
console.log(
[
"Available options:",
...Object.keys(options).map((option) => ` --${option}`),
].join("\n"),
);
},
},
} as const;
const args = util.parseArgs({ options });
if (args.values.json) {
console.dir = function dir(arrayOrObject) {
for (const obj of [arrayOrObject].flat()) {
console.log(JSON.stringify(obj));
}
};
}
for (const option of Object.keys(options)) {
if (args.values[option] && typeof options[option].run === "function") {
await options[option].run();
process.exit();
}
}
options.help.run();