UNPKG

hotshot-mod-manager

Version:

117 lines (99 loc) 3.07 kB
#!/usr/bin/env node import inquirer from "inquirer"; import fs from "node:fs"; import path from "node:path"; import { initKonsole } from "./console.js"; import { cacheDriverCreatorTrigger } from "./contents/cache-driver-creator.js"; import { middlewareGenTrigger } from "./contents/middleware.js"; import { generateOpenApiSpecTrigger } from "./contents/open-api-specs-creator.js"; import { generateServiceTrigger } from "./contents/service-gen.js"; import { othersGenTrigger } from "./others.js"; import { routerModuleGen } from "./routerModuleGen.js"; export const configPath = path.join(process.cwd(), "hotshot.config.json"); export function cleanupString(text) { return text.replace(/[^a-zA-Z0-9_]/g, "_")?.toLowerCase(); } export async function getConfig() { if (!fs.existsSync(configPath)) { throw new Error("hotshot.config.json not found."); } const configFileContent = await fs.promises.readFile(configPath, "utf8"); const config = JSON.parse(configFileContent); return config; } /** * @name nameFixer * @description This function fixes the router name to be compatible with the HotShot CLI. * It converts the module name to lowercase and separates the words with hyphens. * If the isClassName parameter is set to true, it capitalizes the first letter of each word. * * @param {string} moduleName * @param {boolean} isClassName * @returns */ export function nameFixer(moduleName, isClassName = true) { const words = moduleName.split("_"); const fixedName = words .map((word, index) => index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() ) .join(""); return isClassName ? fixedName.charAt(0).toUpperCase() + fixedName.slice(1) : fixedName; } /** * @name getModuleName * @description This function gets the module name from the user input. * * @returns */ export async function hotshotCLI(defaultAction) { initKonsole(); const { action } = await inquirer.prompt([ { type: "list", name: "action", message: "Select an option:", choices: [ "Create Router Module", "Create Service for Router Module", "Create Access Guard", "Create Cache Driver", "Create OpenAPI Specs", "Others", "Exit", ], default: defaultAction ? "Exit" : "Create Router Module", }, ]); if (action === "Create Router Module") { await routerModuleGen(); hotshotCLI(true); } if (action === "Create Service for Router Module") { await generateServiceTrigger(); hotshotCLI(true); } if (action === "Create Access Guard") { await middlewareGenTrigger(); hotshotCLI(true); } if (action === "Create Cache Driver") { await cacheDriverCreatorTrigger(); hotshotCLI(true); } if (action === "Create OpenAPI Specs") { await generateOpenApiSpecTrigger(); hotshotCLI(true); } if (action === "Others") { await othersGenTrigger(); hotshotCLI(true); } if (action === "exit") { process.exit(0); } }