@@ -21,7 +21,7 @@ program
2121 . description ( "RtBot code generator" )
2222 . option (
2323 "-s, --sources [sources...]" ,
24- "A list of markdown files where the documentation and schema of the operators are stored"
24+ "A list of markdown files where the documentation and schema of the operators are stored" ,
2525 )
2626 . option ( "-o, --output <string>" , "Output directory" )
2727 . addOption (
@@ -31,7 +31,7 @@ program
3131 "cpp" ,
3232 "python" ,
3333 "markdown" ,
34- ] )
34+ ] ) ,
3535 )
3636 . action ( async ( { output, sources, target } ) => {
3737 if ( sources . length === 1 ) sources = sources [ 0 ] . split ( " " ) ;
@@ -61,16 +61,16 @@ program
6161 } catch ( e ) {
6262 console . log ( chalk . red ( `Error: ${ e . message } , file ${ f } ` ) ) ;
6363 }
64- } )
64+ } ) ,
6565 ) ;
6666 schemas = schemas
6767 . filter ( ( s ) => s )
6868 // flatten the array of schemas
6969 . reduce ( ( acc , s ) => acc . concat ( s ) , [ ] ) ;
7070 console . log (
7171 `${ chalk . cyan ( "[schemas] found operators:\n - " ) } ${ chalk . yellow (
72- schemas . map ( ( s ) => s . properties . type . enum [ 0 ] ) . reduce ( ( acc , s ) => `${ acc } \n - ${ s } ` )
73- ) } `
72+ schemas . map ( ( s ) => s . properties . type . enum [ 0 ] ) . reduce ( ( acc , s ) => `${ acc } \n - ${ s } ` ) ,
73+ ) } `,
7474 ) ;
7575
7676 const programJsonschema = JSON . parse ( jsonschemaTemplate ( { schemas : schemas . map ( ( s ) => JSON . stringify ( s ) ) } ) ) ;
@@ -85,62 +85,88 @@ program
8585 if ( fileContent . indexOf ( "---" ) > - 1 ) {
8686 const frontMatterStr = fileContent . split ( "---" ) [ 1 ] ;
8787 const frontMatter = parse ( frontMatterStr ) ;
88- const schema = frontMatter . jsonschema ;
89- // cook up an example parameter object
90- const getExampleParameter = ( k : string ) => {
91- if ( k === "id" ) return '"id"' ;
92- if ( schema . properties [ k ] . examples ) {
93- if ( schema . properties [ k ] . type === "array" ) return `[${ schema . properties [ k ] . examples [ 0 ] } ]` ;
94- return schema . properties [ k ] . examples [ 0 ] ;
95- }
96- if ( schema . properties [ k ] . type === "integer" ) return 2 ;
97- if ( schema . properties [ k ] . type === "numeric" ) return 2.0 ;
98- if ( schema . properties [ k ] . type === "string" ) return "some" ;
99- if ( schema . properties [ k ] . type === "boolean" ) return true ;
100- } ;
101- const exampleParameters = Object . keys ( schema . properties ) . reduce (
102- ( acc , k ) => ( { ...acc , [ `${ k } ` ] : getExampleParameter ( k ) } ) ,
103- { }
104- ) ;
105- const opParams = Object . keys ( exampleParameters ) . join ( ", " ) ;
106- const opParamsDef = Object . keys ( exampleParameters ) . reduce (
107- ( acc , k ) => `${ acc } const ${ k } = ${ exampleParameters [ k ] } ;\n` ,
108- ""
109- ) ;
110- const opParamsDefPy = Object . keys ( exampleParameters ) . reduce (
111- ( acc , k ) => `${ acc } ${ k } = ${ exampleParameters [ k ] } ;\n` ,
112- ""
113- ) ;
114- const opParamsDefCpp = Object . keys ( exampleParameters )
115- . reduce ( ( acc , k ) => `${ acc } auto ${ k } = ${ exampleParameters [ k ] } ;\n` , "" )
116- . replaceAll ( "[" , "{" )
117- . replaceAll ( "]" , "}" ) ;
118- const opParamsYaml = Object . keys ( exampleParameters ) . reduce (
119- ( acc , k ) => `${ acc } ${ k } : ${ exampleParameters [ k ] } \n` ,
120- ""
121- ) ;
122- const opParamsJson = Object . keys ( exampleParameters )
123- . reduce ( ( acc , k ) => [ ...acc , `"${ k } ": ${ exampleParameters [ k ] } ` ] , [ ] )
124- . join ( ", " ) ;
88+ const schemas = frontMatter . jsonschemas || [ frontMatter . jsonschema ] ;
89+
90+ // Determine if this file has multiple schemas
91+ const hasMultipleSchemas = Array . isArray ( schemas ) && schemas . length > 1 ;
92+
93+ let newFileContent = fileContent ;
94+
95+ schemas . forEach ( ( schema : any ) => {
96+ if ( ! schema ) return ;
97+
98+ // Use the filename as the operator type if `type` is not specified
99+ const op = schema . properties ?. type ?. enum ?. [ 0 ] || path . basename ( f ) . replace ( ".md" , "" ) ;
100+
101+ // Generate the parameters section
102+ const parameters = {
103+ parameters : Object . entries ( schema . properties ) . map ( ( [ k , v ] : [ string , any ] ) => ( { name : k , ...v } ) ) ,
104+ } ;
105+ const parametersSection = parametersMdTemplate ( parameters ) ;
125106
126- const op = path . basename ( f ) . replace ( ".md" , "" ) ;
127- const usageSection = usageMdTemplate ( {
128- op,
129- opParams,
130- opParamsDef,
131- opParamsDefPy,
132- opParamsDefCpp,
133- opParamsYaml,
134- opParamsJson,
107+ // Append the parameters section to the file content
108+ // deprecated
109+ // newFileContent += "\n\n" + parametersSection;
110+
111+ // For single schema files, inject the usage section
112+ if ( ! hasMultipleSchemas ) {
113+ // cook up an example parameter object
114+ const getExampleParameter = ( k : string ) => {
115+ if ( k === "id" ) return '"id"' ;
116+ if ( schema . properties [ k ] . examples ) {
117+ if ( schema . properties [ k ] . type === "array" ) return `[${ schema . properties [ k ] . examples [ 0 ] } ]` ;
118+ return schema . properties [ k ] . examples [ 0 ] ;
119+ }
120+ if ( schema . properties [ k ] . type === "integer" ) return 2 ;
121+ if ( schema . properties [ k ] . type === "numeric" ) return 2.0 ;
122+ if ( schema . properties [ k ] . type === "string" ) return "some" ;
123+ if ( schema . properties [ k ] . type === "boolean" ) return true ;
124+ } ;
125+
126+ const exampleParameters = Object . keys ( schema . properties ) . reduce (
127+ ( acc , k ) => ( { ...acc , [ `${ k } ` ] : getExampleParameter ( k ) } ) ,
128+ { } ,
129+ ) ;
130+
131+ const opParams = Object . keys ( exampleParameters ) . join ( ", " ) ;
132+ const opParamsDef = Object . keys ( exampleParameters ) . reduce (
133+ ( acc , k ) => `${ acc } const ${ k } = ${ exampleParameters [ k ] } ;\n` ,
134+ "" ,
135+ ) ;
136+ const opParamsDefPy = Object . keys ( exampleParameters ) . reduce (
137+ ( acc , k ) => `${ acc } ${ k } = ${ exampleParameters [ k ] } ;\n` ,
138+ "" ,
139+ ) ;
140+ const opParamsDefCpp = Object . keys ( exampleParameters )
141+ . reduce ( ( acc , k ) => `${ acc } auto ${ k } = ${ exampleParameters [ k ] } ;\n` , "" )
142+ . replaceAll ( "[" , "{" )
143+ . replaceAll ( "]" , "}" ) ;
144+ const opParamsYaml = Object . keys ( exampleParameters ) . reduce (
145+ ( acc , k ) => `${ acc } ${ k } : ${ exampleParameters [ k ] } \n` ,
146+ "" ,
147+ ) ;
148+ const opParamsJson = Object . keys ( exampleParameters )
149+ . reduce ( ( acc , k ) => [ ...acc , `"${ k } ": ${ exampleParameters [ k ] } ` ] , [ ] )
150+ . join ( ", " ) ;
151+
152+ const usageSection = usageMdTemplate ( {
153+ op,
154+ opParams,
155+ opParamsDef,
156+ opParamsDefPy,
157+ opParamsDefCpp,
158+ opParamsYaml,
159+ opParamsJson,
160+ } ) ;
161+
162+ // deprecated
163+ //newFileContent += "\n\n" + usageSection;
164+ }
135165 } ) ;
136- const parameters = {
137- parameters : Object . entries ( schema . properties ) . map ( ( [ k , v ] : [ string , any ] ) => ( { name : k , ...v } ) ) ,
138- } ;
139- const parametersSection = parametersMdTemplate ( parameters ) ;
140- fileContent += "\n\n" + parametersSection ;
141- fileContent += "\n\n" + usageSection ;
142166
143- fs . writeFileSync ( `${ output } /${ path . basename ( f ) } x` , fileContent ) ;
167+ // Write the file with the same name as the original
168+ const outputFileName = `${ output } /${ path . basename ( f ) } x` ;
169+ fs . writeFileSync ( outputFileName , newFileContent ) ;
144170 }
145171 } catch ( e ) {
146172 console . log ( chalk . red ( `Error: ${ e . message } , file ${ f } ` ) , e . stack ) ;
@@ -164,7 +190,7 @@ program
164190 . map ( ( l ) => '"' + l . replaceAll ( '"' , '\\"' ) + '"' )
165191 . join ( "\n" ) }
166192 ""_json;
167- #endif` . replace ( / + / g, "" )
193+ #endif` . replace ( / + / g, "" ) ,
168194 ) ;
169195 }
170196
@@ -230,7 +256,7 @@ program
230256 } ;
231257
232258 const nonPrototypeSchemas = programJsonschema . properties . operators . items . oneOf . filter (
233- ( schema : any ) => ! schema . properties ?. prototype
259+ ( schema : any ) => ! schema . properties ?. prototype ,
234260 ) ;
235261
236262 const typescriptContent = typescriptTemplate ( {
0 commit comments