From 63fdbc6855ed1e214b9486a974e910ae76f84270 Mon Sep 17 00:00:00 2001 From: Sam Mayer Date: Mon, 9 Jun 2025 12:04:59 -0500 Subject: [PATCH] Update docstrings --- src/postProcessing.ts | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/postProcessing.ts b/src/postProcessing.ts index c59fe565..14a65ae7 100644 --- a/src/postProcessing.ts +++ b/src/postProcessing.ts @@ -19,6 +19,8 @@ type CRDResult = { type ClassContextResult = { line: string; insideClass: boolean; braceBalance: number }; +type CrdInfo = { name: string; crd: CustomResourceDefinition; version: string }; + const genericKindProperties = getGenericKindProperties(); /** @@ -111,7 +113,6 @@ export function processAndModifySingleFile( opts: GenerateOptions, ) { opts.logFn(`🔍 Processing file: ${filePath}`); - const { name, crd, version } = fileResult; let fileContent; try { @@ -123,7 +124,11 @@ export function processAndModifySingleFile( let modifiedContent; try { - modifiedContent = applyCRDPostProcessing(fileContent, name, crd, version, opts); + modifiedContent = applyCRDPostProcessing( + fileContent, + { name: fileResult.name, crd: fileResult.crd, version: fileResult.version }, + opts, + ); } catch (error) { logError(error, filePath, opts.logFn); return; @@ -141,17 +146,13 @@ export function processAndModifySingleFile( * Processes the TypeScript file content, applying wrapping and property modifications. * * @param content The content of the TypeScript file. - * @param name The name of the schema. - * @param crd The CustomResourceDefinition object. - * @param version The version of the CRD. + * @param crdInfo The CRD information. * @param opts The options for processing. * @returns The processed TypeScript file content. */ export function applyCRDPostProcessing( content: string, - name: string, - crd: CustomResourceDefinition, - version: string, + crdInfo: CrdInfo, opts: GenerateOptions, ): string { try { @@ -159,7 +160,7 @@ export function applyCRDPostProcessing( // Wraps with the fluent client if needed if (opts.language === "ts" && !opts.plain) { - lines = wrapWithFluentClient(lines, name, crd, version, opts.npmPackage); + lines = wrapWithFluentClient(lines, crdInfo, opts.npmPackage); } const foundInterfaces = collectInterfaceNames(lines); @@ -171,7 +172,7 @@ export function applyCRDPostProcessing( return normalizedLines.join("\n"); } catch (error) { - throw new Error(`Error while applying post-processing for ${name}: ${error.message}`); + throw new Error(`Error while applying post-processing for ${crdInfo.name}: ${error.message}`); } } @@ -239,34 +240,30 @@ export function updateBraceBalance(line: string, braceBalance: number): number { * Wraps the generated TypeScript file with fluent client elements (`GenericKind` and `RegisterKind`). * * @param lines The generated TypeScript lines. - * @param name The name of the schema. - * @param crd The CustomResourceDefinition object. - * @param version The version of the CRD. + * @param crdInfo The CRD information. * @param npmPackage The NPM package name for the fluent client. * @returns The processed TypeScript lines. */ export function wrapWithFluentClient( lines: string[], - name: string, - crd: CustomResourceDefinition, - version: string, + crdInfo: CrdInfo, npmPackage: string = "kubernetes-fluent-client", ): string[] { const autoGenNotice = `// This file is auto-generated by ${npmPackage}, do not edit manually`; const imports = `import { GenericKind, RegisterKind } from "${npmPackage}";`; - const classIndex = lines.findIndex(line => line.includes(`export interface ${name} {`)); + const classIndex = lines.findIndex(line => line.includes(`export interface ${crdInfo.name} {`)); if (classIndex !== -1) { - lines[classIndex] = `export class ${name} extends GenericKind {`; + lines[classIndex] = `export class ${crdInfo.name} extends GenericKind {`; } lines.unshift(autoGenNotice, imports); lines.push( - `RegisterKind(${name}, {`, - ` group: "${crd.spec.group}",`, - ` version: "${version}",`, - ` kind: "${name}",`, - ` plural: "${crd.spec.names.plural}",`, + `RegisterKind(${crdInfo.name}, {`, + ` group: "${crdInfo.crd.spec.group}",`, + ` version: "${crdInfo.version}",`, + ` kind: "${crdInfo.name}",`, + ` plural: "${crdInfo.crd.spec.names.plural}",`, `});`, );