Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 20 additions & 23 deletions src/postProcessing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

type ClassContextResult = { line: string; insideClass: boolean; braceBalance: number };

type CrdInfo = { name: string; crd: CustomResourceDefinition; version: string };

const genericKindProperties = getGenericKindProperties();

/**
Expand Down Expand Up @@ -111,7 +113,6 @@
opts: GenerateOptions,
) {
opts.logFn(`🔍 Processing file: ${filePath}`);
const { name, crd, version } = fileResult;

let fileContent;
try {
Expand All @@ -123,7 +124,11 @@

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;
Expand All @@ -141,25 +146,21 @@
* 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 {
let lines = content.split("\n");

// 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);

Expand All @@ -171,7 +172,7 @@

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}`);
}
}

Expand Down Expand Up @@ -239,34 +240,30 @@
* 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}",`,
`});`,
);

Expand Down Expand Up @@ -314,7 +311,7 @@
* @param foundInterfaces The set of found interfaces in the file.
* @returns An object containing the updated line, updated insideClass flag, and braceBalance.
*/
export function processClassContext(

Check warning on line 314 in src/postProcessing.ts

View workflow job for this annotation

GitHub Actions / format

Function 'processClassContext' has too many parameters (5). Maximum allowed is 4
line: string,
insideClass: boolean,
braceBalance: number,
Expand Down
Loading