@@ -2,9 +2,7 @@ import { readFile, writeFile } from 'node:fs/promises';
22import {
33 ERRORS ,
44 ADD_OPTION_REGEX ,
5- BASIC_SCHEMA ,
65 OPTION_HEADER_KEY_REGEX ,
7- TYPE_DEFINITION_MAP ,
86} from './constants.mjs' ;
97import { join } from 'node:path' ;
108
@@ -48,10 +46,10 @@ export default {
4846 // Read the contents of the cc and h files
4947 const ccContent = await readFile ( ccFile , 'utf-8' ) ;
5048 const hContent = await readFile ( hFile , 'utf-8' ) ;
49+ const schema = JSON . parse (
50+ await readFile ( new URL ( './schema.json' , import . meta. url ) )
51+ ) ;
5152
52- // Clone the BASIC_SCHEMA to avoid mutating the original schema object
53- /** @type {typeof BASIC_SCHEMA } */
54- const schema = Object . assign ( { } , BASIC_SCHEMA ) ;
5553 const { nodeOptions } = schema . properties ;
5654
5755 // Process the cc content and match AddOption calls
@@ -78,18 +76,10 @@ export default {
7876 ) ;
7977 }
8078
81- const headerType = headerTypeMatch [ 1 ] . trim ( ) ;
82-
83- // Ensure the headerType exists in the TYPE_DEFINITION_MAP
84- const typeDefinition = TYPE_DEFINITION_MAP [ headerType ] ;
85- if ( ! typeDefinition ) {
86- throw new Error (
87- formatErrorMessage ( ERRORS . missingTypeDefinition , { headerType } )
88- ) ;
89- }
90-
9179 // Add the option to the schema after removing the '--' prefix
92- nodeOptions . properties [ option . replace ( '--' , '' ) ] = typeDefinition ;
80+ nodeOptions . properties [ option . replace ( '--' , '' ) ] = getTypeSchema (
81+ headerTypeMatch [ 1 ] . trim ( )
82+ ) ;
9383 }
9484
9585 nodeOptions . properties = Object . fromEntries (
@@ -116,3 +106,37 @@ export default {
116106function formatErrorMessage ( message , params ) {
117107 return message . replace ( / { { ( \w + ) } } / g, ( _ , key ) => params [ key ] || `{{${ key } }}` ) ;
118108}
109+
110+ /**
111+ * Returns the JSON Schema definition for a given C++ type.
112+ *
113+ * @param {string } type - The type to get the schema for.
114+ * @returns {object } JSON Schema definition for the given type.
115+ */
116+ function getTypeSchema ( type ) {
117+ switch ( type ) {
118+ case 'std::vector<std::string>' :
119+ return {
120+ oneOf : [
121+ { type : 'string' } ,
122+ {
123+ type : 'array' ,
124+ items : { type : 'string' } ,
125+ minItems : 1 ,
126+ } ,
127+ ] ,
128+ } ;
129+ case 'uint64_t' :
130+ case 'int64_t' :
131+ case 'HostPort' :
132+ return { type : 'number' } ;
133+ case 'std::string' :
134+ return { type : 'string' } ;
135+ case 'bool' :
136+ return { type : 'boolean' } ;
137+ default :
138+ throw new Error (
139+ formatErrorMessage ( ERRORS . missingTypeDefinition , { type } )
140+ ) ;
141+ }
142+ }
0 commit comments