From 9ad8227445f91f23ff0d2c133653fef7b4d7ab7e Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Mon, 24 Jun 2024 09:40:52 +1200 Subject: [PATCH] feat: update the openapi transformer --- src/docs/openapi.v2.yaml | 2 +- src/index.ts | 3 ++- src/utils/openapi-docs.ts | 15 ++++++++------- src/utils/package.ts | 15 +++++++++++++++ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 src/utils/package.ts diff --git a/src/docs/openapi.v2.yaml b/src/docs/openapi.v2.yaml index a599ab7..d331b2b 100644 --- a/src/docs/openapi.v2.yaml +++ b/src/docs/openapi.v2.yaml @@ -1,7 +1,7 @@ openapi: 3.0.0 info: title: Simple Logging Server API - version: 2.2.0 + version: 0.0.0 description: |- This is a simple API for logging messages. It is intended to be a basic interface for logging messages according to an allowed list of clients. ### Usage diff --git a/src/index.ts b/src/index.ts index fc50ccf..e95968d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,10 +13,11 @@ import v2Router from "@/routers/v2"; import docsRouter from "@/routers/docs"; import { transformOpenapiYmlDoc, openapiYmlVersioner } from "@/utils/openapi-docs"; +import { getPackageInfo } from "@/utils/package"; import { env } from "@/config/env"; import type { ServerContext } from "@/types/hono"; -const packageJson = require("../package.json"); +const packageJson = getPackageInfo(); const app = new Hono(); app.use(cors({ origin: "*" })); diff --git a/src/utils/openapi-docs.ts b/src/utils/openapi-docs.ts index 1cfcdaa..c14f808 100644 --- a/src/utils/openapi-docs.ts +++ b/src/utils/openapi-docs.ts @@ -1,7 +1,7 @@ import { join } from "node:path"; import { readFileSync, writeFileSync } from "node:fs"; -type OpenApiDocTransformer = (dirVersion: string, doc: string) => string; +type OpenApiDocTransformer = (filename: string, source_path: string, dirVersion: string, doc: string) => string; /** * Modify the OpenAPI doc with the provided transformers and write the file to the static directory. @@ -16,25 +16,26 @@ export function transformOpenapiYmlDoc(dir_version: string, transformers: OpenAp let newOpenapiYmlDoc = openapiYmlInputDoc; + console.log(`📖 Starting to update /static/${openapiFilename}`); + for (const transformer of transformers) { - newOpenapiYmlDoc = transformer(dir_version, newOpenapiYmlDoc); + newOpenapiYmlDoc = transformer(openapiFilename, openapiYmlSourcePath, dir_version, newOpenapiYmlDoc); } writeFileSync(openapiYmlOutPath, newOpenapiYmlDoc); - console.log(`📖 Updated /static/${openapiFilename}`); + console.log(`📖 Finishing updating /static/${openapiFilename}`); return; } -const ymlVersionRegex = new RegExp(/^version: (0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/); - /** * Transformer to update the version in the OpenAPI doc. * @param version * @returns */ export function openapiYmlVersioner(version: string): OpenApiDocTransformer { - return (_, doc) => { - return doc.replace(ymlVersionRegex, `version: ${version}`); + return (filename, _, __, doc) => { + console.log(`📦 Updating the version in OpenAPI document "${filename}" to "${version}"`); + return doc.replace(/version:\s*0\.0\.0/g, `version: ${version}`); }; } diff --git a/src/utils/package.ts b/src/utils/package.ts new file mode 100644 index 0000000..0539a2f --- /dev/null +++ b/src/utils/package.ts @@ -0,0 +1,15 @@ +import { join } from "node:path"; +import { readFileSync } from "node:fs"; +import { z } from "zod"; + +const PackageSchema = z.object({ + name: z.string(), + version: z.string(), +}); + +export function getPackageInfo() { + const pathname = join(process.cwd(), "package.json"); + const packageJson = JSON.parse(readFileSync(pathname, "utf8")); + + return PackageSchema.parse(packageJson); +}