hotshot-mod-manager
Version:
97 lines (82 loc) • 2.66 kB
JavaScript
import inquirer from "inquirer";
import fs from "node:fs";
import path from "node:path";
import { cleanupString, configPath, getConfig, nameFixer } from "../main.js";
export async function generateOpenApiSpecTrigger() {
const { openAPISepcsName } = await inquirer.prompt([
{
type: "input",
name: "openAPISepcsName",
message: "Enter the name of the OpenAPI Specs:",
validate: (input) =>
input.trim() ? true : "OpenAPI Specs name cannot be empty.",
},
]);
if (openAPISepcsName) {
await generateOpenApiSpecContent(cleanupString(openAPISepcsName));
}
}
export async function generateOpenApiSpecContent(openAPISepcsName) {
const config = await getConfig();
if (
config?.openAPISpecs?.find((m) => m.name === openAPISepcsName)
) {
throw new Error(
`Error: Api Specs '${openAPISepcsName}' already exists in config.`,
);
}
const openAPISpecContent = openAPISpecContents(openAPISepcsName);
/*
|-----------------------------------------------------------------------
| File System IO
|-----------------------------------------------------------------------
*/
const openAPISpecDirPath = path.join(
process.cwd(),
"src",
"open_apis",
);
fs.mkdirSync(openAPISpecDirPath, { recursive: true });
fs.writeFileSync(
path.join(openAPISpecDirPath, `${openAPISepcsName}_openapi.ts`),
openAPISpecContent,
);
/*
|-----------------------------------------------------------------------
| Config Update
|-----------------------------------------------------------------------
*/
config?.openAPISpecs?.push({
name: openAPISepcsName,
path: `./src/open_apis/${openAPISepcsName}_openapi`,
});
const updatedConfigContent = JSON.stringify(config, null, 2);
fs.writeFileSync(configPath, updatedConfigContent);
}
/*
|-----------------------------------------------------------------------
| OpenAPI Spec Contents
|-----------------------------------------------------------------------
*/
function openAPISpecContents(openAPISepcsName) {
const openAPISpecClassName = `${nameFixer(openAPISepcsName, true)}ApiSpecs`;
return `
import type { ApiSpecs, UseOpenApi } from "#libs/open_api"
export class ${openAPISpecClassName} implements UseOpenApi {
public readonly specs: ApiSpecs[]
private readonly routeGroup = "${openAPISepcsName}"
constructor() {
this.specs = [
{
group: this.routeGroup,
method: "GET",
secure: false,
path: "/${openAPISepcsName}",
summary: "Get Request",
description: "No description...",
}
]
}
}
`;
}