|
| 1 | +import { spawn } from "child_process" |
| 2 | +import os from "os" |
| 3 | +import path from "path" |
| 4 | +import { fileURLToPath } from "url" |
| 5 | + |
| 6 | +import { createLogger } from "@crossplane-js/libs" |
| 7 | +import { Command } from "commander" |
| 8 | +import fs from "fs-extra" |
| 9 | +import YAML from "yaml" |
| 10 | + |
| 11 | +import { convertXRDtoCRD, parseAndValidateXRD } from "../xrd2crd/converter.js" |
| 12 | + |
| 13 | +// Create a logger for this module |
| 14 | +const moduleLogger = createLogger("gen-models") |
| 15 | + |
| 16 | +// Resolve CLI package root to run yarn in the right workspace |
| 17 | +const __filename = fileURLToPath(import.meta.url) |
| 18 | +const __dirname = path.dirname(__filename) |
| 19 | +// packages/cli/src/commands/gen-models -> packages/cli |
| 20 | +const cliRoot = path.resolve(__dirname, "../../..") |
| 21 | + |
| 22 | +/** |
| 23 | + * Find all XRD files in the functions directory |
| 24 | + * @returns Promise<string[]> Array of XRD file paths |
| 25 | + */ |
| 26 | +async function findXRDFiles(): Promise<string[]> { |
| 27 | + const functionsDir = "functions" |
| 28 | + |
| 29 | + // Check if functions directory exists |
| 30 | + if (!(await fs.pathExists(functionsDir))) { |
| 31 | + throw new Error(`Functions directory '${functionsDir}' does not exist`) |
| 32 | + } |
| 33 | + |
| 34 | + const dirs = await fs.readdir(functionsDir, { withFileTypes: true }) |
| 35 | + const xrdFiles: string[] = [] |
| 36 | + |
| 37 | + for (const dir of dirs) { |
| 38 | + if (dir.isDirectory()) { |
| 39 | + const xrdPath = path.join(functionsDir, dir.name, "xrd.yaml") |
| 40 | + if (await fs.pathExists(xrdPath)) { |
| 41 | + xrdFiles.push(xrdPath) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (xrdFiles.length === 0) { |
| 47 | + throw new Error("No XRD files found in functions/*/xrd.yaml") |
| 48 | + } |
| 49 | + |
| 50 | + return xrdFiles |
| 51 | +} |
| 52 | + |
| 53 | +async function runCrdGenerate(crdYaml: string, outputPath: string): Promise<void> { |
| 54 | + const tmpBase = path.join(os.tmpdir(), "xfuncjs-") |
| 55 | + const tmpDir = await fs.mkdtemp(tmpBase) |
| 56 | + const inputFile = path.join(tmpDir, "crd.yaml") |
| 57 | + try { |
| 58 | + await fs.writeFile(inputFile, crdYaml, { encoding: "utf8" }) |
| 59 | + const outputAbs = path.resolve(process.cwd(), outputPath) |
| 60 | + await new Promise<void>((resolve, reject) => { |
| 61 | + // we use spawning instead of importing lib, because of this https://github.com/tommy351/kubernetes-models-ts/issues/241 |
| 62 | + const args = [ |
| 63 | + "crd-generate", |
| 64 | + "--customBaseClassImportPath", |
| 65 | + "@crossplane-js/sdk", |
| 66 | + "--modelDecorator", |
| 67 | + "@registerXrdModel", |
| 68 | + "--modelDecoratorPath", |
| 69 | + "@crossplane-js/sdk", |
| 70 | + "--input", |
| 71 | + inputFile, |
| 72 | + "--output", |
| 73 | + outputAbs, |
| 74 | + ] |
| 75 | + const child = spawn("yarn", args, { cwd: cliRoot, stdio: ["ignore", "inherit", "inherit"] }) |
| 76 | + child.on("error", err => reject(err)) |
| 77 | + child.on("exit", code => { |
| 78 | + if (code === 0) resolve() |
| 79 | + else reject(new Error(`crd-generate exited with code ${code}`)) |
| 80 | + }) |
| 81 | + }) |
| 82 | + } finally { |
| 83 | + // Cleanup temp files |
| 84 | + await fs.remove(tmpDir) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Main function for the gen-models command |
| 90 | + * Generates TypeScript models from all XRD files in functions/ |
| 91 | + * @returns Promise<void> |
| 92 | + */ |
| 93 | +async function genModelsAction(): Promise<void> { |
| 94 | + try { |
| 95 | + moduleLogger.info("Starting model generation...") |
| 96 | + |
| 97 | + // Find all XRD files |
| 98 | + const xrdFiles = await findXRDFiles() |
| 99 | + moduleLogger.info(`Found ${xrdFiles.length} XRD file(s): ${xrdFiles.join(", ")}`) |
| 100 | + |
| 101 | + // Ensure models directory exists |
| 102 | + const modelsDir = "models" |
| 103 | + await fs.ensureDir(modelsDir) |
| 104 | + |
| 105 | + // Process each XRD file |
| 106 | + for (const xrdPath of xrdFiles) { |
| 107 | + moduleLogger.info(`Processing ${xrdPath}...`) |
| 108 | + |
| 109 | + try { |
| 110 | + // Read and parse the XRD file |
| 111 | + const xrdContent = await fs.readFile(xrdPath, { encoding: "utf8" }) |
| 112 | + const xrd = parseAndValidateXRD(xrdContent) |
| 113 | + |
| 114 | + // Convert XRD to CRD |
| 115 | + const crd = convertXRDtoCRD(xrd) |
| 116 | + const crdYaml = YAML.stringify(crd) |
| 117 | + |
| 118 | + // Generate models using crd-generate (via child process) |
| 119 | + await runCrdGenerate(crdYaml, modelsDir) |
| 120 | + |
| 121 | + moduleLogger.info(`✓ Generated models for ${xrdPath}`) |
| 122 | + } catch (error) { |
| 123 | + moduleLogger.error(`✗ Failed to process ${xrdPath}: ${error}`) |
| 124 | + throw error |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + moduleLogger.info(`✓ Model generation completed. Models saved to '${modelsDir}/' directory.`) |
| 129 | + } catch (error) { |
| 130 | + moduleLogger.error(`Error generating models: ${error}`) |
| 131 | + process.exit(1) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Register the gen-models command with the CLI |
| 137 | + * @param program The Commander program instance |
| 138 | + */ |
| 139 | +export default function (program: Command): void { |
| 140 | + program |
| 141 | + .command("gen-models") |
| 142 | + .description("Generate TypeScript models from all XRD files in functions/") |
| 143 | + .action(async () => { |
| 144 | + try { |
| 145 | + await genModelsAction() |
| 146 | + } catch (err) { |
| 147 | + moduleLogger.error(`Error running gen-models command: ${err}`) |
| 148 | + process.exit(1) |
| 149 | + } |
| 150 | + }) |
| 151 | +} |
0 commit comments