|
1 | 1 | import { readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import { join } from 'node:path'; |
| 3 | + |
2 | 4 | import { |
3 | | - ERRORS, |
4 | 5 | ADD_OPTION_REGEX, |
| 6 | + ERRORS, |
5 | 7 | OPTION_HEADER_KEY_REGEX, |
6 | 8 | } from './constants.mjs'; |
7 | | -import { join } from 'node:path'; |
8 | 9 | import schema from './schema.json' with { type: 'json' }; |
| 10 | +import { formatErrorMessage, getTypeSchema } from './utilities.mjs'; |
9 | 11 |
|
10 | 12 | /** |
11 | 13 | * This generator generates the `node.config.json` schema. |
@@ -55,6 +57,7 @@ export default { |
55 | 57 | } |
56 | 58 |
|
57 | 59 | const headerKey = config.match(OPTION_HEADER_KEY_REGEX)?.[1]; |
| 60 | + |
58 | 61 | // If there's no header key, it's either a V8 option or a no-op |
59 | 62 | if (!headerKey) { |
60 | 63 | continue; |
@@ -91,47 +94,3 @@ export default { |
91 | 94 | return schema; |
92 | 95 | }, |
93 | 96 | }; |
94 | | - |
95 | | -/** |
96 | | - * Helper function to replace placeholders in error messages with dynamic values. |
97 | | - * @param {string} message - The error message with placeholders. |
98 | | - * @param {Object} params - The values to replace the placeholders. |
99 | | - * @returns {string} - The formatted error message. |
100 | | - */ |
101 | | -function formatErrorMessage(message, params) { |
102 | | - return message.replace(/{{(\w+)}}/g, (_, key) => params[key] || `{{${key}}}`); |
103 | | -} |
104 | | - |
105 | | -/** |
106 | | - * Returns the JSON Schema definition for a given C++ type. |
107 | | - * |
108 | | - * @param {string} type - The type to get the schema for. |
109 | | - * @returns {object} JSON Schema definition for the given type. |
110 | | - */ |
111 | | -function getTypeSchema(type) { |
112 | | - switch (type) { |
113 | | - case 'std::vector<std::string>': |
114 | | - return { |
115 | | - oneOf: [ |
116 | | - { type: 'string' }, |
117 | | - { |
118 | | - type: 'array', |
119 | | - items: { type: 'string' }, |
120 | | - minItems: 1, |
121 | | - }, |
122 | | - ], |
123 | | - }; |
124 | | - case 'uint64_t': |
125 | | - case 'int64_t': |
126 | | - case 'HostPort': |
127 | | - return { type: 'number' }; |
128 | | - case 'std::string': |
129 | | - return { type: 'string' }; |
130 | | - case 'bool': |
131 | | - return { type: 'boolean' }; |
132 | | - default: |
133 | | - throw new Error( |
134 | | - formatErrorMessage(ERRORS.missingTypeDefinition, { type }) |
135 | | - ); |
136 | | - } |
137 | | -} |
0 commit comments