gadl/main.ts
2023-11-19 02:25:32 +09:00

68 lines
1.6 KiB
TypeScript

import fs from "node:fs/promises";
import util from "node:util";
import { chromium } from "./browser";
import { createDatabase } from "./database";
import { createLibrary } from "./library";
import { createPlatform } from "./platform";
const options = {
db: {
type: "string",
default: "gadl.db",
},
reset: {
type: "boolean",
async run() {
await fs.rm(args.values.db!);
},
},
login: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch({ headless: false });
const ctx = await browser.newContext();
const platform = createPlatform(db, ctx);
await platform.login();
},
},
add: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.add(args.values.add!);
},
},
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 });
},
},
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 });
for (const option of Object.keys(options)) {
if (args.values[option] && typeof options[option].run === "function") {
await options[option].run();
process.exit();
}
}