Skip to content

Commit 928b380

Browse files
committed
resolve reviews
1 parent 14f3c5f commit 928b380

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

src/generators/node-config-schema/index.mjs

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { readFile, writeFile } from 'node:fs/promises';
2+
import { join } from 'node:path';
3+
24
import {
3-
ERRORS,
45
ADD_OPTION_REGEX,
6+
ERRORS,
57
OPTION_HEADER_KEY_REGEX,
68
} from './constants.mjs';
7-
import { join } from 'node:path';
89
import schema from './schema.json' with { type: 'json' };
10+
import { formatErrorMessage, getTypeSchema } from './utilities.mjs';
911

1012
/**
1113
* This generator generates the `node.config.json` schema.
@@ -55,6 +57,7 @@ export default {
5557
}
5658

5759
const headerKey = config.match(OPTION_HEADER_KEY_REGEX)?.[1];
60+
5861
// If there's no header key, it's either a V8 option or a no-op
5962
if (!headerKey) {
6063
continue;
@@ -91,47 +94,3 @@ export default {
9194
return schema;
9295
},
9396
};
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-
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { ERRORS } from './constants.mjs';
2+
3+
/**
4+
* Helper function to replace placeholders in error messages with dynamic values.
5+
* @param {string} message - The error message with placeholders.
6+
* @param {Object} params - The values to replace the placeholders.
7+
* @returns {string} - The formatted error message.
8+
*/
9+
export function formatErrorMessage(message, params) {
10+
return message.replace(/{{(\w+)}}/g, (_, key) => params[key] || `{{${key}}}`);
11+
}
12+
13+
/**
14+
* Returns the JSON Schema definition for a given C++ type.
15+
*
16+
* @param {string} type - The type to get the schema for.
17+
* @returns {object} JSON Schema definition for the given type.
18+
*/
19+
export function getTypeSchema(type) {
20+
switch (type) {
21+
case 'std::vector<std::string>':
22+
return {
23+
oneOf: [
24+
{ type: 'string' },
25+
{
26+
type: 'array',
27+
items: { type: 'string' },
28+
minItems: 1,
29+
},
30+
],
31+
};
32+
case 'uint64_t':
33+
case 'int64_t':
34+
case 'HostPort':
35+
return { type: 'number' };
36+
case 'std::string':
37+
return { type: 'string' };
38+
case 'bool':
39+
return { type: 'boolean' };
40+
default:
41+
throw new Error(
42+
formatErrorMessage(ERRORS.missingTypeDefinition, { type })
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)