@@ -4,7 +4,8 @@ import path from "path"
44import { fileURLToPath } from "url"
55
66import { createLogger } from "@crossplane-js/libs"
7- import { Command } from "commander"
7+ import { readInput } from "@kubernetes-models/read-input"
8+ import { type Command } from "commander"
89import fs from "fs-extra"
910import YAML from "yaml"
1011
@@ -19,6 +20,20 @@ const __dirname = path.dirname(__filename)
1920// packages/cli/src/commands/gen-models -> packages/cli
2021const cliRoot = path . resolve ( __dirname , "../../.." )
2122
23+ /**
24+ * Fetch extra CRDs
25+ * @param urls Array of CRD urls
26+ * @returns Promise<string[]> Array of CRD paths
27+ */
28+ async function fetchExtraCRDs ( paths : string [ ] ) : Promise < string [ ] > {
29+ const extraDocuments : string [ ] = [ ]
30+ for ( const path of paths ) {
31+ moduleLogger . info ( "Reading extra CRD: %s" , path )
32+ extraDocuments . push ( await readInput ( path ) )
33+ }
34+ return extraDocuments
35+ }
36+
2237/**
2338 * Find all XRD files in the functions directory
2439 * @returns Promise<string[]> Array of XRD file paths
@@ -72,7 +87,10 @@ async function runCrdGenerate(crdYaml: string, outputPath: string): Promise<void
7287 "--output" ,
7388 outputAbs ,
7489 ]
75- const child = spawn ( "yarn" , args , { cwd : cliRoot , stdio : [ "ignore" , "inherit" , "inherit" ] } )
90+ const child = spawn ( "yarn" , args , {
91+ cwd : cliRoot ,
92+ stdio : [ "ignore" , "inherit" , "inherit" ] ,
93+ } )
7694 child . on ( "error" , err => reject ( err ) )
7795 child . on ( "exit" , code => {
7896 if ( code === 0 ) resolve ( )
@@ -94,6 +112,8 @@ async function genModelsAction(): Promise<void> {
94112 try {
95113 moduleLogger . info ( "Starting model generation..." )
96114
115+ const documents : string [ ] = [ ]
116+
97117 // Find all XRD files
98118 const xrdFiles = await findXRDFiles ( )
99119 moduleLogger . info ( `Found ${ xrdFiles . length } XRD file(s): ${ xrdFiles . join ( ", " ) } ` )
@@ -119,9 +139,7 @@ async function genModelsAction(): Promise<void> {
119139 // Convert XRD to CRD
120140 const crd = convertXRDtoCRD ( xrd )
121141 const crdYaml = YAML . stringify ( crd )
122-
123- // Generate models using crd-generate (via child process)
124- await runCrdGenerate ( crdYaml , modelsDir )
142+ documents . push ( crdYaml )
125143
126144 moduleLogger . info ( `✓ Generated models for ${ xrdPath } ` )
127145 } catch ( error ) {
@@ -130,6 +148,27 @@ async function genModelsAction(): Promise<void> {
130148 }
131149 }
132150
151+ // Get extra CRD urls from config file
152+ if ( await fs . exists ( "config.yaml" ) ) {
153+ moduleLogger . info ( "Config file exists, searching extra CRDs..." )
154+ const configFile = await fs . readFile ( "config.yaml" , "utf8" )
155+ const config = YAML . parse ( configFile )
156+ if ( ! config . extraCrds ) {
157+ moduleLogger . info ( "Config file has no extra CRDs" )
158+ } else if ( ! Array . isArray ( config . extraCrds ) ) {
159+ moduleLogger . warn ( "Config file extraCrds field is not an array!" )
160+ } else {
161+ const crds = await fetchExtraCRDs ( config . extraCrds )
162+ for ( const crd of crds ) {
163+ documents . push ( crd )
164+ }
165+ }
166+ }
167+
168+ // Generate models using crd-generate (via child process)
169+ const allCrds = documents . join ( "\n---\n" )
170+ await runCrdGenerate ( allCrds , modelsDir )
171+
133172 moduleLogger . info ( `✓ Model generation completed. Models saved to '${ modelsDir } /' directory.` )
134173 } catch ( error ) {
135174 moduleLogger . error ( `Error generating models: ${ error } ` )
0 commit comments