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 platform = createPlatform({ db, browser }); await platform.login(); }, }, 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(); }, }, 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 }); }, }, download: { type: "string", async run() { const db = await createDatabase(args.values.db!); const library = createLibrary(db); const books = await library.getBooks(); const browser = await chromium.launch(); const platform = createPlatform({ db, browser }); await platform.download(args.values.download!, books); }, }, 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(); } }