Skip to content

Commit

Permalink
Add database option to cli + fix db.setup() timing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
browningluke committed Jul 26, 2024
1 parent 70ec260 commit 446d8a8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/commands/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CHAPTER_DELAY_TIME } from "@core/constants";

import { Command as Commander } from 'commander';

export function initDownloadCommand(program: Commander, db: Database) {
export function initDownloadCommand(program: Commander) {
const downloadFunction = async (plugin: string, query: string, options: any) => {
let parsedPlugin = await parsePlugin(plugin);
let download = await createDownload(parsedPlugin, query);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const Commands = {
'manage': handleManageDialog,
'update': handleUpdateDialog
}
export function initCommands(program: Commander, db: Database) {
export function initCommands(program: Commander) {
for (const a of initFunctions) {
a(program, db)
a(program)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/commands/manage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { MangaPlugin } from "plugin";
import { Command as Commander } from "commander";
import Table from "cli-table3";

export function initManageCommand(program: Commander, db: Database) {
export function initManageCommand(program: Commander) {
const deleteFunction = async (plugin: string | undefined, id: string | undefined, options: any) => {
let db = program.opts().database as Database;

let target: { plugin: MangaPlugin, id: string } | undefined;

if (id && plugin) {
Expand All @@ -25,6 +27,7 @@ export function initManageCommand(program: Commander, db: Database) {
}

const listFunction = async (options: any) => {
let db = program.opts().database as Database;
await printListFromDatabase(db, options.showChapters);
await db.close();
}
Expand Down
4 changes: 3 additions & 1 deletion src/commands/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import { getUserConfirmation } from "@helpers/cli";

import { Command as Commander } from "commander";

export function initRegisterCommand(program: Commander, db: Database) {
export function initRegisterCommand(program: Commander) {
const registerFunction = async (plugin: string, query: string, options: any) => {
let db = program.opts().database as Database;

let parsedPlugin = await parsePlugin(plugin);
let manga = await getManga(parsedPlugin, query);

Expand Down
3 changes: 2 additions & 1 deletion src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { UPDATE_CHAPTER_DELAY_TIME } from "@core/constants";

import { Command as Commander } from "commander";

export function initUpdateCommand(program: Commander, db: Database) {
export function initUpdateCommand(program: Commander) {
let updateFunction = async () => {
let db = program.opts().database as Database;
await runUpdate(db);
await db.close();
}
Expand Down
29 changes: 25 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import SQLite from "@databases/sqlite";
import Config from "@core/config";

import { handleDialog, initCommands } from "./commands";

import { Command as Commander } from 'commander';
import { ALL_PLUGIN_NAMES } from "./plugins";
import { Database } from "database";
import SQLite from "@databases/sqlite";
import Postgres from "@databases/postgres";

const program = new Commander();

let db = new SQLite();
let db: Database;

export async function run() {
program
.description("A CLI utility to download manga chapters from various online platforms.")
.addHelpText('after', `\nExample:\n $ mangathr --help`)
.hook('preAction', async () => await db.setup() );
.hook('preAction', async (thisCommand, _) => {
// Pass option up to file-wide variable
db = (thisCommand.opts().database as Database);
await db.setup();
} );

program
.option('--db-path <path>', "specify path to database",
Expand All @@ -23,6 +30,20 @@ export async function run() {
(v: string) => Config.CONFIG.PSQL_CONNECTION_STRING = v)
.option('--dest <path>', "specify path to save files",
(v: string) => Config.CONFIG.DOWNLOAD_DIR = v)
.option<Database>('--database <driver>', "database driver to use (default sqlite)",
(v: string) => {
switch (v.toLowerCase().trim()) {
case "postgres":
case "psql":
return new Postgres();

default:
console.warn(`\x1b[33mUnknown driver '${v}', defaulting to sqlite...\x1b[0m`);
case "sqlite":
case "sqlite3":
return new SQLite();
}
}, new SQLite());

program
.option('--list-plugins', "prints all available plugin names")
Expand All @@ -37,7 +58,7 @@ export async function run() {
await handleDialog(db);
});

initCommands(program, db);
initCommands(program);
await program.parseAsync(process.argv);
}

Expand Down

0 comments on commit 446d8a8

Please sign in to comment.