-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathindex.mjs
More file actions
96 lines (79 loc) · 2.78 KB
/
index.mjs
File metadata and controls
96 lines (79 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import {
ADD_OPTION_REGEX,
ERRORS,
OPTION_HEADER_KEY_REGEX,
} from './constants.mjs';
import schema from './schema.json' with { type: 'json' };
import { formatErrorMessage, getTypeSchema } from './utilities.mjs';
/**
* This generator generates the `node.config.json` schema.
*
* @typedef {Array<ApiDocMetadataEntry>} Input
*
* @type {GeneratorMetadata<Input, string>}
*/
export default {
name: 'node-config-schema',
version: '1.0.0',
description: 'Generates the node.config.json schema.',
/**
* Generates the `node.config.json` schema.
* @param {unknown} _ - Unused parameter
* @param {Partial<GeneratorOptions>} options - Options containing the input file paths
* @throws {Error} If the required files node_options.cc or node_options.h are missing or invalid.
*/
async generate(_, options) {
// Ensure input files are provided and capture the paths
const ccFile = options.input.find(filePath =>
filePath.endsWith('node_options.cc')
);
const hFile = options.input.find(filePath =>
filePath.endsWith('node_options.h')
);
// Error handling if either cc or h file is missing
if (!ccFile || !hFile) {
throw new Error(ERRORS.missingCCandHFiles);
}
// Read the contents of the cc and h files
const ccContent = await readFile(ccFile, 'utf-8');
const hContent = await readFile(hFile, 'utf-8');
const { nodeOptions } = schema.properties;
// Process the cc content and match AddOption calls
for (const [, option, config] of ccContent.matchAll(ADD_OPTION_REGEX)) {
// If config doesn't include 'kAllowedInEnvvar', skip this option
if (!config.includes('kAllowedInEnvvar')) {
continue;
}
const headerKey = config.match(OPTION_HEADER_KEY_REGEX)?.[1];
// If there's no header key, it's either a V8 option or a no-op
if (!headerKey) {
continue;
}
// Try to find the corresponding header type in the hContent
const headerTypeMatch = hContent.match(
new RegExp(`\\s*(.+)\\s${headerKey}[^\\B_]`)
);
if (!headerTypeMatch) {
throw new Error(
formatErrorMessage(ERRORS.headerTypeNotFound, { headerKey })
);
}
// Add the option to the schema after removing the '--' prefix
nodeOptions.properties[option.replace('--', '')] = getTypeSchema(
headerTypeMatch[1].trim()
);
}
nodeOptions.properties = Object.fromEntries(
Object.keys(nodeOptions.properties)
.sort()
.map(key => [key, nodeOptions.properties[key]])
);
await writeFile(
join(options.output, 'node-config-schema.json'),
JSON.stringify(schema, null, 2) + '\n'
);
return schema;
},
};