kubernetes-fluent-client 3.5.6 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/cli.js +9 -16
  2. package/dist/fetch.js +5 -8
  3. package/dist/fluent/index.d.ts +2 -1
  4. package/dist/fluent/index.d.ts.map +1 -1
  5. package/dist/fluent/index.js +27 -29
  6. package/dist/fluent/shared-types.d.ts +49 -0
  7. package/dist/fluent/shared-types.d.ts.map +1 -0
  8. package/dist/fluent/shared-types.js +23 -0
  9. package/dist/fluent/types.d.ts +2 -37
  10. package/dist/fluent/types.d.ts.map +1 -1
  11. package/dist/fluent/types.js +1 -14
  12. package/dist/fluent/utils.d.ts +1 -1
  13. package/dist/fluent/utils.d.ts.map +1 -1
  14. package/dist/fluent/utils.js +25 -35
  15. package/dist/fluent/watch.d.ts +1 -8
  16. package/dist/fluent/watch.d.ts.map +1 -1
  17. package/dist/fluent/watch.js +23 -43
  18. package/dist/generate.js +22 -65
  19. package/dist/helpers.js +5 -10
  20. package/dist/index.js +13 -60
  21. package/dist/kinds.js +2 -7
  22. package/dist/normalization.d.ts +97 -0
  23. package/dist/normalization.d.ts.map +1 -0
  24. package/dist/normalization.js +152 -0
  25. package/dist/patch.js +1 -2
  26. package/dist/postProcessing.d.ts +6 -127
  27. package/dist/postProcessing.d.ts.map +1 -1
  28. package/dist/postProcessing.js +24 -279
  29. package/dist/types.d.ts +3 -2
  30. package/dist/types.d.ts.map +1 -1
  31. package/dist/types.js +1 -5
  32. package/dist/upstream.js +2 -53
  33. package/package.json +11 -8
  34. package/src/cli.ts +1 -3
  35. package/src/fluent/index.ts +12 -11
  36. package/src/fluent/shared-types.ts +55 -0
  37. package/src/fluent/types.ts +3 -54
  38. package/src/fluent/utils.ts +2 -2
  39. package/src/fluent/watch.ts +2 -21
  40. package/src/normalization.ts +181 -0
  41. package/src/postProcessing.ts +11 -239
  42. package/src/types.ts +3 -4
  43. package/dist/fileSystem.d.ts +0 -11
  44. package/dist/fileSystem.d.ts.map +0 -1
  45. package/dist/fileSystem.js +0 -52
  46. package/src/fileSystem.ts +0 -25
@@ -6,7 +6,7 @@ import * as path from "path";
6
6
  import { GenerateOptions } from "./generate";
7
7
  import { GenericKind } from "./types";
8
8
  import { CustomResourceDefinition } from "./upstream";
9
- import { FileSystem, NodeFileSystem } from "./fileSystem";
9
+ import { modifyAndNormalizeClassProperties, normalizeIndentationAndSpacing } from "./normalization";
10
10
 
11
11
  type CRDResult = {
12
12
  name: string;
@@ -14,8 +14,6 @@ type CRDResult = {
14
14
  version: string;
15
15
  };
16
16
 
17
- type CodeLines = string[];
18
-
19
17
  type ClassContextResult = { line: string; insideClass: boolean; braceBalance: number };
20
18
 
21
19
  const genericKindProperties = getGenericKindProperties();
@@ -25,23 +23,18 @@ const genericKindProperties = getGenericKindProperties();
25
23
  *
26
24
  * @param allResults The array of CRD results.
27
25
  * @param opts The options for post-processing.
28
- * @param fileSystem The file system interface for reading and writing files.
29
26
  */
30
- export async function postProcessing(
31
- allResults: CRDResult[],
32
- opts: GenerateOptions,
33
- fileSystem: FileSystem = new NodeFileSystem(),
34
- ) {
27
+ export async function postProcessing(allResults: CRDResult[], opts: GenerateOptions) {
35
28
  if (!opts.directory) {
36
29
  opts.logFn("⚠️ Error: Directory is not defined.");
37
30
  return;
38
31
  }
39
32
 
40
- const files = fileSystem.readdirSync(opts.directory);
33
+ const files = fs.readdirSync(opts.directory);
41
34
  opts.logFn("\n🔧 Post-processing started...");
42
35
 
43
36
  const fileResultMap = mapFilesToCRD(allResults);
44
- await processFiles(files, fileResultMap, opts, fileSystem);
37
+ await processFiles(files, fileResultMap, opts);
45
38
 
46
39
  opts.logFn("🔧 Post-processing completed.\n");
47
40
  }
@@ -73,13 +66,11 @@ export function mapFilesToCRD(allResults: CRDResult[]): Record<string, CRDResult
73
66
  * @param files - The list of file names to process.
74
67
  * @param fileResultMap - A map linking file names to their corresponding CRD results.
75
68
  * @param opts - Options for the generation process.
76
- * @param fileSystem - The file system interface for reading and writing files.
77
69
  */
78
70
  export async function processFiles(
79
71
  files: string[],
80
72
  fileResultMap: Record<string, CRDResult>,
81
73
  opts: GenerateOptions,
82
- fileSystem: FileSystem,
83
74
  ) {
84
75
  for (const file of files) {
85
76
  if (!opts.directory) {
@@ -94,7 +85,7 @@ export async function processFiles(
94
85
  }
95
86
 
96
87
  try {
97
- processAndModifySingleFile(filePath, fileResult, opts, fileSystem);
88
+ processAndModifySingleFile(filePath, fileResult, opts);
98
89
  } catch (error) {
99
90
  logError(error, filePath, opts.logFn);
100
91
  }
@@ -110,20 +101,18 @@ export async function processFiles(
110
101
  * @param fileResult.crd - The CustomResourceDefinition object.
111
102
  * @param fileResult.version - The version of the CRD.
112
103
  * @param opts - Options for the generation process.
113
- * @param fileSystem - The file system interface for reading and writing files.
114
104
  */
115
105
  export function processAndModifySingleFile(
116
106
  filePath: string,
117
107
  fileResult: CRDResult,
118
108
  opts: GenerateOptions,
119
- fileSystem: FileSystem,
120
109
  ) {
121
110
  opts.logFn(`🔍 Processing file: ${filePath}`);
122
111
  const { name, crd, version } = fileResult;
123
112
 
124
113
  let fileContent;
125
114
  try {
126
- fileContent = fileSystem.readFile(filePath);
115
+ fileContent = fs.readFileSync(filePath, "utf8");
127
116
  } catch (error) {
128
117
  logError(error, filePath, opts.logFn);
129
118
  return;
@@ -138,7 +127,7 @@ export function processAndModifySingleFile(
138
127
  }
139
128
 
140
129
  try {
141
- fileSystem.writeFile(filePath, modifiedContent);
130
+ fs.writeFileSync(filePath, modifiedContent);
142
131
  opts.logFn(`✅ Successfully processed and wrote file: ${filePath}`);
143
132
  } catch (error) {
144
133
  logError(error, filePath, opts.logFn);
@@ -166,7 +155,7 @@ export function applyCRDPostProcessing(
166
155
  let lines = content.split("\n");
167
156
 
168
157
  // Wraps with the fluent client if needed
169
- if (shouldWrapWithFluentClient(opts)) {
158
+ if (opts.language === "ts" && !opts.plain) {
170
159
  lines = wrapWithFluentClient(lines, name, crd, version, opts.npmPackage);
171
160
  }
172
161
  const foundInterfaces = collectInterfaceNames(lines);
@@ -183,34 +172,6 @@ export function applyCRDPostProcessing(
183
172
  }
184
173
  }
185
174
 
186
- /**
187
- * Reads the content of a file from disk.
188
- *
189
- * @param filePath The path to the file.
190
- * @returns The file contents as a string.
191
- */
192
- export function readFile(filePath: string): string {
193
- try {
194
- return fs.readFileSync(filePath, "utf8");
195
- } catch (error) {
196
- throw new Error(`Failed to read file at ${filePath}: ${error.message}`);
197
- }
198
- }
199
-
200
- /**
201
- * Writes the modified content back to the file.
202
- *
203
- * @param filePath The path to the file.
204
- * @param content The modified content to write.
205
- */
206
- export function writeFile(filePath: string, content: string): void {
207
- try {
208
- fs.writeFileSync(filePath, content, "utf8");
209
- } catch (error) {
210
- throw new Error(`Failed to write file at ${filePath}: ${error.message}`);
211
- }
212
- }
213
-
214
175
  /**
215
176
  * Retrieves the properties of the `GenericKind` class, excluding dynamic properties like `[key: string]: any`.
216
177
  *
@@ -227,7 +188,7 @@ export function getGenericKindProperties(): string[] {
227
188
  * @param lines The lines of the file content.
228
189
  * @returns A set of found interface names.
229
190
  */
230
- export function collectInterfaceNames(lines: CodeLines): Set<string> {
191
+ export function collectInterfaceNames(lines: string[]): Set<string> {
231
192
  // https://regex101.com/r/S6w8pW/1
232
193
  const interfacePattern = /export interface (?<interfaceName>\w+)/;
233
194
  const foundInterfaces = new Set<string>();
@@ -263,109 +224,6 @@ export function updateBraceBalance(line: string, braceBalance: number): number {
263
224
  return braceBalance + (line.includes("{") ? 1 : 0) - (line.includes("}") ? 1 : 0);
264
225
  }
265
226
 
266
- /**
267
- * Generates a regular expression to match a property pattern in TypeScript.
268
- *
269
- * @param prop The property name to match.
270
- * @returns A regular expression to match the property pattern.
271
- */
272
- export function getPropertyPattern(prop: string): RegExp {
273
- // For prop="kind", the pattern will match "kind ? :" or "kind :"
274
- // https://regex101.com/r/mF8kXn/1
275
- return new RegExp(`\\b${prop}\\b\\s*\\?\\s*:|\\b${prop}\\b\\s*:`);
276
- }
277
-
278
- /**
279
- * Applies ESLint and property modifiers to a line of code.
280
- *
281
- * @param line - The current line of code.
282
- * @param genericKindProperties - The list of properties from `GenericKind`.
283
- * @param foundInterfaces - The set of found interfaces in the file.
284
- * @returns The modified line.
285
- */
286
- export function modifyPropertiesAndAddEslintDirective(
287
- line: string,
288
- genericKindProperties: string[],
289
- foundInterfaces: Set<string>,
290
- ): string {
291
- line = addDeclareAndOptionalModifiersToProperties(line, genericKindProperties, foundInterfaces);
292
- line = processEslintDisable(line, genericKindProperties);
293
- return line;
294
- }
295
-
296
- /**
297
- * Applies property modifiers to a line of code.
298
- *
299
- * @param line The current line of code.
300
- * @param genericKindProperties The list of properties from `GenericKind`.
301
- * @param foundInterfaces The set of found interfaces in the file.
302
- * @returns The modified line.
303
- */
304
- export function addDeclareAndOptionalModifiersToProperties(
305
- line: string,
306
- genericKindProperties: string[],
307
- foundInterfaces: Set<string>,
308
- ): string {
309
- line = addDeclareToGenericKindProperties(line, genericKindProperties);
310
- line = makePropertiesOptional(line, foundInterfaces);
311
- line = normalizeLineIndentation(line);
312
- return line;
313
- }
314
- /**
315
- * Adds the `declare` keyword to `GenericKind` properties.
316
- *
317
- * @param line The current line of code.
318
- * @param genericKindProperties The list of properties from `GenericKind`.
319
- * @returns The modified line with the `declare` keyword, if applicable.
320
- */
321
- export function addDeclareToGenericKindProperties(
322
- line: string,
323
- genericKindProperties: string[],
324
- ): string {
325
- for (const prop of genericKindProperties) {
326
- const propertyPattern = getPropertyPattern(prop);
327
- if (propertyPattern.test(line)) {
328
- return line.replace(prop, `declare ${prop}`);
329
- }
330
- }
331
- return line;
332
- }
333
-
334
- /**
335
- * Makes a property optional if its type matches one of the found interfaces and it is not already optional.
336
- *
337
- * @param line The current line of code.
338
- * @param foundInterfaces The set of found interfaces in the file.
339
- * @returns The modified line with the optional `?` symbol.
340
- */
341
- export function makePropertiesOptional(line: string, foundInterfaces: Set<string>): string {
342
- // https://regex101.com/r/kX8TCj/1
343
- const propertyTypePattern = /:\s*(?<propertyType>\w+)\s*;/;
344
- const match = line.match(propertyTypePattern);
345
-
346
- if (match?.groups?.propertyType) {
347
- const { propertyType } = match.groups;
348
- if (foundInterfaces.has(propertyType) && !line.includes("?")) {
349
- return line.replace(":", "?:");
350
- }
351
- }
352
- return line;
353
- }
354
-
355
- /**
356
- * Adds an ESLint disable comment for `[key: string]: any` if it's not part of `GenericKind`.
357
- *
358
- * @param line The current line of code.
359
- * @param genericKindProperties The list of properties from `GenericKind`.
360
- * @returns The modified line with the ESLint disable comment.
361
- */
362
- export function processEslintDisable(line: string, genericKindProperties: string[]): string {
363
- if (line.includes("[key: string]: any") && !genericKindProperties.includes("[key: string]")) {
364
- return ` // eslint-disable-next-line @typescript-eslint/no-explicit-any\n${line}`;
365
- }
366
- return line;
367
- }
368
-
369
227
  /**
370
228
  * Wraps the generated TypeScript file with fluent client elements (`GenericKind` and `RegisterKind`).
371
229
  *
@@ -377,7 +235,7 @@ export function processEslintDisable(line: string, genericKindProperties: string
377
235
  * @returns The processed TypeScript lines.
378
236
  */
379
237
  export function wrapWithFluentClient(
380
- lines: CodeLines,
238
+ lines: string[],
381
239
  name: string,
382
240
  crd: CustomResourceDefinition,
383
241
  version: string,
@@ -404,61 +262,6 @@ export function wrapWithFluentClient(
404
262
  return lines;
405
263
  }
406
264
 
407
- /**
408
- * Normalizes indentation for TypeScript lines to a consistent format.
409
- *
410
- * @param lines The generated TypeScript lines.
411
- * @returns The lines with normalized indentation.
412
- */
413
- export function normalizeIndentation(lines: CodeLines): string[] {
414
- return lines.map(line => line.replace(/^ {4}/, " "));
415
- }
416
-
417
- /**
418
- * Normalizes the indentation of a single line to use two spaces instead of four.
419
- *
420
- * @param line The line of code to normalize.
421
- * @returns The line with normalized indentation.
422
- */
423
- export function normalizeLineIndentation(line: string): string {
424
- return line.replace(/^ {4}/, " ");
425
- }
426
-
427
- /**
428
- * Normalizes spacing between property names and types in TypeScript lines.
429
- *
430
- * @param lines The generated TypeScript lines.
431
- * @returns The lines with normalized property spacing.
432
- */
433
- export function normalizePropertySpacing(lines: CodeLines): string[] {
434
- // https://regex101.com/r/XEv3pL/1
435
- return lines.map(line => line.replace(/\s*\?\s*:\s*/, "?: "));
436
- }
437
-
438
- /**
439
- * Removes lines containing `[property: string]: any;` from TypeScript files.
440
- *
441
- * @param lines The generated TypeScript lines.
442
- * @param opts The options for processing.
443
- * @returns The lines with `[property: string]: any;` removed.
444
- */
445
- export function removePropertyStringAny(lines: CodeLines, opts: GenerateOptions): string[] {
446
- if (opts.language === "ts" || opts.language === "typescript") {
447
- return lines.filter(line => !line.includes("[property: string]: any;"));
448
- }
449
- return lines;
450
- }
451
-
452
- /**
453
- * Determines if the content should be wrapped with the fluent client.
454
- *
455
- * @param opts The options for generating the content.
456
- * @returns True if the content should be wrapped with the fluent client, false otherwise.
457
- */
458
- export function shouldWrapWithFluentClient(opts: GenerateOptions): boolean {
459
- return opts.language === "ts" && !opts.plain;
460
- }
461
-
462
265
  /**
463
266
  * Processes the lines of the TypeScript file, focusing on classes extending `GenericKind`.
464
267
  *
@@ -468,7 +271,7 @@ export function shouldWrapWithFluentClient(opts: GenerateOptions): boolean {
468
271
  * @returns The processed lines.
469
272
  */
470
273
  export function processLines(
471
- lines: CodeLines,
274
+ lines: string[],
472
275
  genericKindProperties: string[],
473
276
  foundInterfaces: Set<string>,
474
277
  ): string[] {
@@ -524,37 +327,6 @@ export function processClassContext(
524
327
  return { line, insideClass, braceBalance };
525
328
  }
526
329
 
527
- /**
528
- * Processes a single line inside a class extending `GenericKind`.
529
- *
530
- * @param line The current line of code.
531
- * @param genericKindProperties The list of properties from `GenericKind`.
532
- * @param foundInterfaces The set of found interfaces in the file.
533
- * @returns The modified line.
534
- */
535
- export function modifyAndNormalizeClassProperties(
536
- line: string,
537
- genericKindProperties: string[],
538
- foundInterfaces: Set<string>,
539
- ): string {
540
- line = modifyPropertiesAndAddEslintDirective(line, genericKindProperties, foundInterfaces);
541
- line = normalizeLineIndentation(line);
542
- return line;
543
- }
544
-
545
- /**
546
- * Normalizes lines after processing, including indentation, spacing, and removing unnecessary lines.
547
- *
548
- * @param lines The lines of the file content.
549
- * @param opts The options for processing.
550
- * @returns The normalized lines.
551
- */
552
- export function normalizeIndentationAndSpacing(lines: CodeLines, opts: GenerateOptions): string[] {
553
- let normalizedLines = normalizeIndentation(lines);
554
- normalizedLines = normalizePropertySpacing(normalizedLines);
555
- return removePropertyStringAny(normalizedLines, opts);
556
- }
557
-
558
330
  /**
559
331
  * Handles logging for errors with stack trace.
560
332
  *
package/src/types.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
3
3
 
4
- import { KubernetesObject, V1ObjectMeta } from "@kubernetes/client-node";
5
-
6
- export { KubernetesObject, KubernetesListObject } from "@kubernetes/client-node";
7
-
4
+ import { V1ObjectMeta } from "@kubernetes/client-node";
5
+ import type { KubernetesListObject, KubernetesObject } from "@kubernetes/client-node";
6
+ export type { KubernetesListObject, KubernetesObject };
8
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
8
  export type GenericClass = abstract new () => any;
10
9
 
@@ -1,11 +0,0 @@
1
- export interface FileSystem {
2
- readFile(filePath: string): string;
3
- writeFile(filePath: string, content: string): void;
4
- readdirSync(directory: string): string[];
5
- }
6
- export declare class NodeFileSystem implements FileSystem {
7
- readFile(filePath: string): string;
8
- writeFile(filePath: string, content: string): void;
9
- readdirSync(directory: string): string[];
10
- }
11
- //# sourceMappingURL=fileSystem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fileSystem.d.ts","sourceRoot":"","sources":["../src/fileSystem.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACnC,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACnD,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC1C;AAGD,qBAAa,cAAe,YAAW,UAAU;IAC/C,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAIlC,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAIlD,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE;CAGzC"}
@@ -1,52 +0,0 @@
1
- "use strict";
2
- // SPDX-License-Identifier: Apache-2.0
3
- // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
4
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
- if (k2 === undefined) k2 = k;
6
- var desc = Object.getOwnPropertyDescriptor(m, k);
7
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
- desc = { enumerable: true, get: function() { return m[k]; } };
9
- }
10
- Object.defineProperty(o, k2, desc);
11
- }) : (function(o, m, k, k2) {
12
- if (k2 === undefined) k2 = k;
13
- o[k2] = m[k];
14
- }));
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v });
17
- }) : function(o, v) {
18
- o["default"] = v;
19
- });
20
- var __importStar = (this && this.__importStar) || (function () {
21
- var ownKeys = function(o) {
22
- ownKeys = Object.getOwnPropertyNames || function (o) {
23
- var ar = [];
24
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
25
- return ar;
26
- };
27
- return ownKeys(o);
28
- };
29
- return function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
33
- __setModuleDefault(result, mod);
34
- return result;
35
- };
36
- })();
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.NodeFileSystem = void 0;
39
- const fs = __importStar(require("fs"));
40
- /* eslint class-methods-use-this: "off" */
41
- class NodeFileSystem {
42
- readFile(filePath) {
43
- return fs.readFileSync(filePath, "utf8");
44
- }
45
- writeFile(filePath, content) {
46
- fs.writeFileSync(filePath, content, "utf8");
47
- }
48
- readdirSync(directory) {
49
- return fs.readdirSync(directory);
50
- }
51
- }
52
- exports.NodeFileSystem = NodeFileSystem;
package/src/fileSystem.ts DELETED
@@ -1,25 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
3
-
4
- import * as fs from "fs";
5
-
6
- export interface FileSystem {
7
- readFile(filePath: string): string;
8
- writeFile(filePath: string, content: string): void;
9
- readdirSync(directory: string): string[];
10
- }
11
-
12
- /* eslint class-methods-use-this: "off" */
13
- export class NodeFileSystem implements FileSystem {
14
- readFile(filePath: string): string {
15
- return fs.readFileSync(filePath, "utf8");
16
- }
17
-
18
- writeFile(filePath: string, content: string): void {
19
- fs.writeFileSync(filePath, content, "utf8");
20
- }
21
-
22
- readdirSync(directory: string): string[] {
23
- return fs.readdirSync(directory);
24
- }
25
- }