Patched DB creation and added CLI toolchain base

This commit is contained in:
fedyna-k
2025-01-20 08:31:12 +01:00
parent cc9def9c96
commit 282f618386
4 changed files with 66 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
import { parseArgs, type ParseOptions } from "@std/cli/parse-args";
import { createModule } from "$root/toolbox/module.ts";
interface CLI {
[command: string]: CLI | (() => void) | (() => Promise<void>);
}
const argSpec: ParseOptions = {};
const cli: CLI = {
help: () => displayHelp(cli),
module: {
create: createModule,
},
};
function displayHelp(cli: CLI, errorMessage?: string): never {
const loggingFunction = errorMessage ? console.error : console.log;
if (errorMessage) {
console.error(errorMessage);
}
loggingFunction("Commands:");
}
function runCommand(commands: Array<string | number>, cli: CLI): never | void | Promise<void> {
if (commands.length == 0) {
console.error(
`No command provided. Available commands are ${
JSON.stringify(Object.keys(cli))
}.`,
);
Deno.exit(1);
}
const command = commands.shift()!.toString();
if (cli[command] == undefined) {
console.error(
`Command "${command}" doesn't exist. Available commands are ${
JSON.stringify(Object.keys(cli))
}.`,
);
Deno.exit(1);
}
if (typeof cli[command] == "object") {
return runCommand(commands, cli[command]);
}
return cli[command]();
}
function main() {
const args = parseArgs(Deno.args, argSpec);
runCommand(args._, cli);
}
main();