gadl/main.ts

143 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-11-19 02:25:32 +09:00
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",
},
2023-11-20 01:22:15 +09:00
"out-dir": {
type: "string",
default: "dist",
},
2023-11-19 02:25:32 +09:00
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 });
2023-11-19 20:47:49 +09:00
const platform = createPlatform({ db, browser });
2023-11-19 02:25:32 +09:00
await platform.login();
},
},
2023-11-19 16:18:00 +09:00
logout: {
type: "boolean",
async run() {
const db = await createDatabase(args.values.db!);
const browser = await chromium.launch();
2023-11-19 20:47:49 +09:00
const platform = createPlatform({ db, browser });
2023-11-19 16:18:00 +09:00
await platform.logout();
},
},
2023-11-19 02:25:32 +09:00
add: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.add(args.values.add!);
},
},
2023-11-19 20:51:29 +09:00
delete: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
await library.delete(Number(args.values.delete));
},
},
2023-11-19 02:25:32 +09:00
list: {
type: "boolean",
short: "l",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
const books = await library.getBooks();
2023-11-20 22:56:09 +09:00
console.dir(books, {
depth: null,
maxArrayLength: null,
maxStringLength: null,
});
2023-11-19 02:25:32 +09:00
},
},
2023-11-19 22:04:19 +09:00
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);
}
2023-11-20 22:56:09 +09:00
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);
}
2023-11-19 22:04:19 +09:00
},
},
2023-11-19 20:47:49 +09:00
download: {
type: "string",
async run() {
const db = await createDatabase(args.values.db!);
const library = createLibrary(db);
2023-11-19 22:04:19 +09:00
const book = await library.get(Number(args.values.download!));
if (!book) {
process.exit(1);
}
2023-11-19 20:47:49 +09:00
const browser = await chromium.launch();
const platform = createPlatform({ db, browser });
2023-11-20 01:22:15 +09:00
const dir = `${args.values["out-dir"]!}/${book.id}`;
await platform.download(dir, book);
2023-11-20 01:37:55 +09:00
await library.archive(args.values["out-dir"]!);
},
},
2023-11-19 02:25:32 +09:00
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();
}
}