From de2644352d1492978cda9bbf0341aa120d8cf453 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Mon, 20 Nov 2023 01:22:15 +0900 Subject: [PATCH] --out-dir --- .gitignore | 1 + database.ts | 3 +++ main.ts | 7 ++++++- platforms/dmm-books.ts | 4 +++- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 21d1c1c..cf4faf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules *.db +dist diff --git a/database.ts b/database.ts index 4c296f2..a42ec5e 100644 --- a/database.ts +++ b/database.ts @@ -1,10 +1,13 @@ import fs from "node:fs/promises"; +import path from "node:path"; import sqlite3 from "sqlite3"; import { Database, open } from "sqlite"; export { Database }; export async function createDatabase(file: string): Promise { + await fs.mkdir(path.dirname(file), { recursive: true }); + const db = await open({ filename: file, driver: sqlite3.cached.Database, diff --git a/main.ts b/main.ts index f0954ea..8e0b699 100644 --- a/main.ts +++ b/main.ts @@ -10,6 +10,10 @@ const options = { type: "string", default: "gadl.db", }, + "out-dir": { + type: "string", + default: "dist", + }, reset: { type: "boolean", async run() { @@ -87,7 +91,8 @@ const options = { const browser = await chromium.launch(); const platform = createPlatform({ db, browser }); - await platform.download(args.values.download!, book); + const dir = `${args.values["out-dir"]!}/${book.id}`; + await platform.download(dir, book); }, }, help: { diff --git a/platforms/dmm-books.ts b/platforms/dmm-books.ts index 6f0f1cb..f3d7c08 100644 --- a/platforms/dmm-books.ts +++ b/platforms/dmm-books.ts @@ -1,4 +1,5 @@ import fs from "node:fs/promises"; +import path from "node:path"; import type { Book } from "../library"; import { userAgent, type Browser, type BrowserContext } from "../browser"; import type { Database } from "../database"; @@ -155,7 +156,8 @@ export function DmmBooks({ db, browser }: { db: Database; browser: Browser }) { const page = await ctx.newPage(); // TODO: --all - // await fs.mkdir(dir); + await fs.mkdir(path.dirname(dir), { recursive: true }); + await fs.mkdir(dir); await page.goto(book.readerUrl); const files = await page.evaluate(getFiles);