|
| 1 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 2 | +import { |
| 3 | + ERRORS, |
| 4 | + ADD_OPTION_REGEX, |
| 5 | + BASIC_SCHEMA, |
| 6 | + OPTION_HEADER_KEY_REGEX, |
| 7 | + TYPE_DEFINITION_MAP, |
| 8 | +} from './constants.mjs'; |
| 9 | +import { join } from 'node:path'; |
| 10 | + |
| 11 | +/** |
| 12 | + * This generator generates the `node.config.json` schema. |
| 13 | + * |
| 14 | + * @typedef {Array<ApiDocMetadataEntry>} Input |
| 15 | + * |
| 16 | + * @type {GeneratorMetadata<Input, string>} |
| 17 | + */ |
| 18 | +export default { |
| 19 | + name: 'node-config-schema', |
| 20 | + |
| 21 | + version: '1.0.0', |
| 22 | + |
| 23 | + description: 'Generates the node.config.json schema.', |
| 24 | + |
| 25 | + /** |
| 26 | + * Generates the `node.config.json` schema. |
| 27 | + * @param {unknown} _ - Unused parameter |
| 28 | + * @param {Partial<GeneratorOptions>} options - Options containing the input file paths |
| 29 | + * @throws {Error} If the required files node_options.cc or node_options.h are missing or invalid. |
| 30 | + */ |
| 31 | + async generate(_, options) { |
| 32 | + let ccFile, hFile; |
| 33 | + |
| 34 | + // Ensure input files are provided and capture the paths |
| 35 | + for (const filePath of options.input) { |
| 36 | + if (filePath.endsWith('node_options.cc')) { |
| 37 | + ccFile = filePath; |
| 38 | + } else if (filePath.endsWith('node_options.h')) { |
| 39 | + hFile = filePath; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Error handling if either cc or h file is missing |
| 44 | + if (!ccFile || !hFile) { |
| 45 | + throw new Error(ERRORS.missingCCandHFiles); |
| 46 | + } |
| 47 | + |
| 48 | + // Read the contents of the cc and h files |
| 49 | + const ccContent = await readFile(ccFile, 'utf-8'); |
| 50 | + const hContent = await readFile(hFile, 'utf-8'); |
| 51 | + |
| 52 | + // Clone the BASIC_SCHEMA to avoid mutating the original schema object |
| 53 | + /** @type {typeof BASIC_SCHEMA} */ |
| 54 | + const schema = Object.assign({}, BASIC_SCHEMA); |
| 55 | + const { nodeOptions } = schema.properties; |
| 56 | + |
| 57 | + // Process the cc content and match AddOption calls |
| 58 | + for (const [, option, config] of ccContent.matchAll(ADD_OPTION_REGEX)) { |
| 59 | + // If config doesn't include 'kAllowedInEnvvar', skip this option |
| 60 | + if (!config.includes('kAllowedInEnvvar')) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const headerKey = config.match(OPTION_HEADER_KEY_REGEX)?.[1]; |
| 65 | + // If there's no header key, it's either a V8 option or a no-op |
| 66 | + if (!headerKey) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + // Try to find the corresponding header type in the hContent |
| 71 | + const headerTypeMatch = hContent.match( |
| 72 | + new RegExp(`\\s*(.+)\\s${headerKey}[^\\B_]`) |
| 73 | + ); |
| 74 | + |
| 75 | + // Error handling if header type is not found |
| 76 | + if (!headerTypeMatch) { |
| 77 | + throw new Error( |
| 78 | + formatErrorMessage(ERRORS.headerTypeNotFound, { headerKey }) |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const headerType = headerTypeMatch[1].trim(); |
| 83 | + |
| 84 | + // Ensure the headerType exists in the TYPE_DEFINITION_MAP |
| 85 | + const typeDefinition = TYPE_DEFINITION_MAP[headerType]; |
| 86 | + if (!typeDefinition) { |
| 87 | + throw new Error( |
| 88 | + formatErrorMessage(ERRORS.missingTypeDefinition, { headerType }) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + // Add the option to the schema after removing the '--' prefix |
| 93 | + nodeOptions.properties[option.replace('--', '')] = typeDefinition; |
| 94 | + } |
| 95 | + |
| 96 | + nodeOptions.properties = Object.fromEntries( |
| 97 | + Object.keys(nodeOptions.properties) |
| 98 | + .sort() |
| 99 | + .map(key => [key, nodeOptions.properties[key]]) |
| 100 | + ); |
| 101 | + |
| 102 | + await writeFile( |
| 103 | + join(options.output, 'node-config-schema.json'), |
| 104 | + JSON.stringify(schema, null, 2) + '\n' |
| 105 | + ); |
| 106 | + |
| 107 | + return schema; |
| 108 | + }, |
| 109 | +}; |
| 110 | + |
| 111 | +/** |
| 112 | + * Helper function to replace placeholders in error messages with dynamic values. |
| 113 | + * @param {string} message - The error message with placeholders. |
| 114 | + * @param {Object} params - The values to replace the placeholders. |
| 115 | + * @returns {string} - The formatted error message. |
| 116 | + */ |
| 117 | +function formatErrorMessage(message, params) { |
| 118 | + return message.replace(/{{(\w+)}}/g, (_, key) => params[key] || `{{${key}}}`); |
| 119 | +} |
0 commit comments