Fix reported issues and add template identifier generation logic

This commit is contained in:
Kuldeep
2026-01-15 15:18:29 +00:00
parent 3d79e4869a
commit 237ddc2d28
13 changed files with 5690 additions and 5618 deletions

View File

@@ -11,53 +11,44 @@ const rootDir = join(__dirname, '..');
// exec helper
function exec(command, options = {}) {
const defaultOptions = {
stdio: 'inherit',
cwd: rootDir,
shell: true,
...options,
};
return execSync(command, defaultOptions);
const defaultOptions = {
stdio: 'inherit',
cwd: rootDir,
shell: true,
...options,
};
return execSync(command, defaultOptions);
}
try {
const schemaPath = join(rootDir, 'src/parser/xo-template.schema.json');
const validatorPath = join(rootDir, 'src/parser/ajv/validate-xo-template.js');
const tsconfigPath = join(rootDir, 'tsconfig.json');
const templatePath = join(rootDir, 'node_modules/@xo-cash/types/build/template.d.ts');
const schemaPath = join(rootDir, 'src/parser/xo-template.schema.json');
const validatorPath = join(rootDir, 'src/parser/ajv/validate-xo-template.js');
const tsconfigPath = join(rootDir, 'tsconfig.json');
const templatePath = join(
rootDir,
'node_modules/@xo-cash/types/build/template.d.ts',
);
// Generate schema JSON (write to file directly instead of shell redirection)
const schemaContent = execSync(
`ts-json-schema-generator --no-ref-encode --tsconfig "${tsconfigPath}" --path "${templatePath}" --type "XOTemplate"`,
{ encoding: 'utf8', cwd: rootDir },
);
writeFileSync(schemaPath, schemaContent, 'utf8');
// Generate schema JSON (write to file directly instead of shell redirection)
const schemaContent = execSync(
`ts-json-schema-generator --no-ref-encode --tsconfig "${tsconfigPath}" --path "${templatePath}" --type "XOTemplate"`,
{ encoding: 'utf8', cwd: rootDir },
);
writeFileSync(schemaPath, schemaContent, 'utf8');
// Compile validator with AJV
exec(`ajv compile -s "${schemaPath}" --allowUnionTypes --all-errors -o "${validatorPath}"`);
// Compile validator with AJV
exec(
`ajv compile -s "${schemaPath}" --allowUnionTypes -o "${validatorPath}"`,
);
// Transform the generated validator code
let code = readFileSync(validatorPath, 'utf8');
// Transform the generated validator code
let code = readFileSync(validatorPath, 'utf8');
code = code
.replace(/"use strict";module\.exports = [^;]+;module\.exports\.default = [^;]+;/, 'export default validate20;')
.replace(/;const /g, ';\nconst ')
.replace(/;function /g, ';\nfunction ')
.replace(/\}function /g, '}\nfunction ');
code = code
.replace(
/"use strict";module\.exports = [^;]+;module\.exports\.default = [^;]+;/,
'export default validate20;',
)
.replace(/;const /g, ';\nconst ')
.replace(/;function /g, ';\nfunction ')
.replace(/\}function /g, '}\nfunction ');
writeFileSync(validatorPath, code, 'utf8');
writeFileSync(validatorPath, code, 'utf8');
console.log('Schema generation complete!');
console.log('Schema generation complete!');
} catch (error) {
console.error('Error generating schema:', error.message);
process.exit(1);
console.error('Error generating schema:', error.message);
process.exit(1);
}