From 86115f0263a4ba929e69479a7ec31b613520f6f5 Mon Sep 17 00:00:00 2001 From: AlexSserb Date: Thu, 13 Mar 2025 10:48:45 +0400 Subject: [PATCH] feat: kwargs for modules file generation --- src/modules/modulesFileGen/modulesFileGen.ts | 55 ++++++++++++++------ 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/modules/modulesFileGen/modulesFileGen.ts b/src/modules/modulesFileGen/modulesFileGen.ts index 9e55300..1bbd604 100644 --- a/src/modules/modulesFileGen/modulesFileGen.ts +++ b/src/modules/modulesFileGen/modulesFileGen.ts @@ -3,21 +3,7 @@ import * as path from "path"; import * as handlebars from "handlebars"; import axios, { AxiosResponse } from "axios"; -const HOST_AND_PORT = process.argv.length > 2 ? process.argv[2] : "127.0.0.1:8000"; -const ENDPOINT = `http://${HOST_AND_PORT}/project/modules`; - -const TEMPLATE_PATH = path.join(__dirname, "templates", "modulesFileTemplate.hbs"); -const OUTPUT_PATH = path.join(__dirname, "..", "modules.tsx"); - -const templateSource = fs.readFileSync(TEMPLATE_PATH, "utf8"); -const template = handlebars.compile(templateSource); - -handlebars.registerHelper("uppercase", (text) => { - return text - .replace(/([a-z])([A-Z])/g, "$1_$2") - .toUpperCase(); -}); - +// region types type Module = { id: number; key: string; @@ -30,8 +16,47 @@ type ModulesResponse = { modules: Module[]; } +type Args = { + [key: string]: string | boolean; +} // endregion +// region utils +const getArgs = (): Args => + process.argv.slice(2).reduce((args: Args, arg: string) => { + if (arg.startsWith("--")) { + // Handle long arguments like --port=8000 + const [key, value] = arg.slice(2).split("="); + args[key] = value !== undefined ? value : true; + } else if (arg.startsWith("-") && arg.length > 1) { + // Handle short arguments like -p=8000 + const [key, value] = arg.slice(1).split("="); + args[key] = value !== undefined ? value : true; + } + return args; + }, {}); +// endregion + +const kwargs = getArgs(); + +// region constants +const HOST = kwargs["host"] ?? kwargs["h"] ?? "127.0.0.1"; +const PORT = kwargs["port"] ?? kwargs["p"] ?? "8000"; +const ENDPOINT = `http://${HOST}:${PORT}/project/modules`; + +const TEMPLATE_PATH = path.join(__dirname, "templates", "modulesFileTemplate.hbs"); +const OUTPUT_PATH = path.join(__dirname, "..", "modules.tsx"); +// endregion + +const templateSource = fs.readFileSync(TEMPLATE_PATH, "utf8"); +const template = handlebars.compile(templateSource); + +handlebars.registerHelper("uppercase", (text) => { + return text + .replace(/([a-z])([A-Z])/g, "$1_$2") + .toUpperCase(); +}); + const generateRows = (modules: Module[]) => { try { const data = {