From 40dd9d864d69a897f53880c991bed7257d290b97 Mon Sep 17 00:00:00 2001 From: AugusDogus <9794679+AugusDogus@users.noreply.github.com> Date: Mon, 10 Feb 2025 10:27:15 -0600 Subject: [PATCH] Respect debug and silent logging configuration for both cli and lib modes --- .changeset/afraid-trains-care.md | 5 +++++ index.ts | 15 ++++++++------- src/commands/generate-templates/configuration.ts | 10 +++++++--- src/commands/generate-templates/index.ts | 3 +++ src/index.ts | 3 +++ src/templates-worker.ts | 2 ++ types/index.ts | 5 ++++- 7 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 .changeset/afraid-trains-care.md diff --git a/.changeset/afraid-trains-care.md b/.changeset/afraid-trains-care.md new file mode 100644 index 00000000..7f104e72 --- /dev/null +++ b/.changeset/afraid-trains-care.md @@ -0,0 +1,5 @@ +--- +"swagger-typescript-api": patch +--- + +Respect debug and silent logging configuration for both cli and lib modes. diff --git a/index.ts b/index.ts index d29e1aed..fa5dc2e0 100644 --- a/index.ts +++ b/index.ts @@ -7,6 +7,7 @@ import { TemplatesGenConfig } from "./src/commands/generate-templates/configurat import { CodeGenConfig } from "./src/configuration.js"; import { HTTP_CLIENT } from "./src/constants.js"; import { generateApi, generateTemplates } from "./src/index.js"; +import type { HttpClientType } from "./types/index.js"; const templateGenBaseConfig = new TemplatesGenConfig({}); @@ -52,18 +53,21 @@ const generateTemplatesCommand = defineCommand({ description: "Output only errors to console", default: templateGenBaseConfig.silent, }, + debug: { + type: "boolean", + description: "additional information about processes inside this tool", + default: templateGenBaseConfig.debug, + }, }, run: async ({ args }) => { - if (args.debug) consola.level = Number.MAX_SAFE_INTEGER; - if (args.silent) consola.level = 0; - await generateTemplates({ cleanOutput: args["clean-output"], - httpClientType: args["http-client"], + httpClientType: args["http-client"] as HttpClientType, modular: args.modular, output: args.output, rewrite: args.rewrite, silent: args.silent, + debug: args.debug, }); }, }); @@ -274,9 +278,6 @@ const generateCommand = defineCommand({ }, }, run: async ({ args }) => { - if (args.debug) consola.level = Number.MAX_SAFE_INTEGER; - if (args.silent) consola.level = 0; - let customConfig; if (args["custom-config"]) { diff --git a/src/commands/generate-templates/configuration.ts b/src/commands/generate-templates/configuration.ts index ab69f957..8e99e6ad 100644 --- a/src/commands/generate-templates/configuration.ts +++ b/src/commands/generate-templates/configuration.ts @@ -1,15 +1,19 @@ -import type { GenerateTemplatesParams } from "../../../types/index.js"; +import type { + GenerateTemplatesParams, + HttpClientType, +} from "../../../types/index.js"; import { HTTP_CLIENT, PROJECT_VERSION } from "../../constants.js"; import { objectAssign } from "../../util/object-assign.js"; export class TemplatesGenConfig { cleanOutput = false; output = undefined; - httpClientType = HTTP_CLIENT.FETCH; + httpClientType: HttpClientType = HTTP_CLIENT.FETCH; modular = false; + rewrite = false; silent = false; + debug = false; version = PROJECT_VERSION; - rewrite = false; constructor(config: GenerateTemplatesParams) { this.update(config); diff --git a/src/commands/generate-templates/index.ts b/src/commands/generate-templates/index.ts index 119c551b..f3c1c80e 100644 --- a/src/commands/generate-templates/index.ts +++ b/src/commands/generate-templates/index.ts @@ -1,7 +1,10 @@ +import { consola } from "consola"; import type { GenerateTemplatesParams } from "../../../types/index.js"; import { TemplatesGenProcess } from "./templates-gen-process.js"; export async function generateTemplates(config: GenerateTemplatesParams) { + if (config.debug) consola.level = Number.MAX_SAFE_INTEGER; + if (config.silent) consola.level = 0; const codeGenProcess = new TemplatesGenProcess(config); return await codeGenProcess.start(); } diff --git a/src/index.ts b/src/index.ts index 85477ac6..4f1a0f93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,12 @@ +import { consola } from "consola"; import type { GenerateApiConfiguration } from "../types/index.js"; import { CodeGenProcess } from "./code-gen-process.js"; export async function generateApi( config: Partial, ) { + if (config.debug) consola.level = Number.MAX_SAFE_INTEGER; + if (config.silent) consola.level = 0; const codeGenProcess = new CodeGenProcess(config); return await codeGenProcess.start(); } diff --git a/src/templates-worker.ts b/src/templates-worker.ts index 6d870fa5..4359c9f5 100644 --- a/src/templates-worker.ts +++ b/src/templates-worker.ts @@ -20,6 +20,8 @@ export class TemplatesWorker { this.config = config; this.fileSystem = fileSystem; this.getRenderTemplateData = getRenderTemplateData; + if (this.config.debug) consola.level = Number.MAX_SAFE_INTEGER; + if (this.config.silent) consola.level = 0; } getTemplatePaths = ( diff --git a/types/index.ts b/types/index.ts index 97204ed8..6ef97731 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,8 +1,9 @@ import type { ComponentTypeNameResolver } from "../src/component-type-name-resolver.js"; +import type { HTTP_CLIENT } from "../src/constants.js"; import type { MonoSchemaParser } from "../src/schema-parser/mono-schema-parser.js"; import type { Translator } from "../src/translators/translator.js"; -type HttpClientType = "axios" | "fetch"; +export type HttpClientType = (typeof HTTP_CLIENT)[keyof typeof HTTP_CLIENT]; interface GenerateApiParamsBase { /** @@ -773,7 +774,9 @@ export interface GenerateTemplatesParams { output?: string; httpClientType?: HttpClientType; modular?: boolean; + rewrite?: boolean; silent?: boolean; + debug?: boolean; } export interface GenerateTemplatesOutput