mirror of
https://github.com/morten-olsen/homelab-operator.git
synced 2026-02-08 01:36:28 +01:00
more
This commit is contained in:
49
scripts/update-manifests.ts
Executable file
49
scripts/update-manifests.ts
Executable file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env node
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
|
||||
import { compile } from 'json-schema-to-typescript';
|
||||
|
||||
import { K8sService } from '../src/services/k8s/k8s.ts';
|
||||
import { Services } from '../src/utils/service.ts';
|
||||
|
||||
const services = new Services();
|
||||
const k8s = services.get(K8sService);
|
||||
|
||||
const manifests = await k8s.extensionsApi.listCustomResourceDefinition();
|
||||
const root = join(import.meta.dirname, '..', 'src', '__generated__', 'resources');
|
||||
|
||||
await mkdir(root, { recursive: true });
|
||||
|
||||
const firstUpsercase = (input: string) => {
|
||||
const [first, ...rest] = input.split('');
|
||||
return [first.toUpperCase(), ...rest].join('');
|
||||
};
|
||||
|
||||
for (const manifest of manifests.items) {
|
||||
for (const version of manifest.spec.versions) {
|
||||
try {
|
||||
const schema = version.schema?.openAPIV3Schema;
|
||||
if (!schema) {
|
||||
continue;
|
||||
}
|
||||
const cleanedSchema = JSON.parse(JSON.stringify(schema));
|
||||
const kind = manifest.spec.names.kind;
|
||||
const typeName = `K8S${kind}${firstUpsercase(version.name)}`;
|
||||
const jsonLocation = join(root, `${typeName}.json`);
|
||||
await writeFile(jsonLocation, JSON.stringify(schema, null, 2));
|
||||
const file = await compile(cleanedSchema, typeName, {
|
||||
declareExternallyReferenced: true,
|
||||
additionalProperties: false,
|
||||
$refOptions: {
|
||||
continueOnError: true,
|
||||
},
|
||||
});
|
||||
const fileLocation = join(root, `${typeName}.ts`);
|
||||
await writeFile(fileLocation, file, 'utf8');
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
console.error(`${manifest.metadata?.name} ${version.name} failed`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user