Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions generate_ts_schema_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('node:fs');
const path = require('node:path');
const { compile } = require('json-schema-to-typescript');

const SOURCE_ROOT = path.resolve(__dirname, 'spec');
const SOURCE_ROOT = path.resolve(__dirname, 'source');
const OUTPUT_FILE = path.resolve(__dirname, './generated/schema-types.ts');
const WRAPPER_NAME = 'SCHEMA_WRAPPER';

Expand All @@ -16,31 +16,49 @@ async function generate() {

const properties = {};

// Add shopping schemas
const shoppingDir = path.join(SOURCE_ROOT, 'schemas/shopping');
if (fs.existsSync(shoppingDir)) {
for (const file of fs.readdirSync(shoppingDir)) {
if (file.endsWith('.json')) {
properties[path.basename(file, '.json')] = {
$ref: path.join(shoppingDir, file)
};
const addSchemasFromDir = (dir, prefix = '') => {
if (fs.existsSync(dir)) {
for (const file of fs.readdirSync(dir)) {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// Skip types directory as they are pulled in by refs
continue;
} else if (file.endsWith('.json')) {
const name = (prefix + path.basename(file, '.json')).replace(/[-.]/g, '_');
properties[name] = { $ref: fullPath };
}
}
}
}
};

// Add core schemas
addSchemasFromDir(path.join(SOURCE_ROOT, 'schemas'));
// Add transport schemas
addSchemasFromDir(path.join(SOURCE_ROOT, 'schemas/transports'), 'transport_');
// Add shopping schemas
addSchemasFromDir(path.join(SOURCE_ROOT, 'schemas/shopping'));
// Add discovery schemas
addSchemasFromDir(path.join(SOURCE_ROOT, 'discovery'));

// Add handler schemas
const handlersDir = path.join(SOURCE_ROOT, 'handlers');
if (fs.existsSync(handlersDir)) {
for (const handler of fs.readdirSync(handlersDir)) {
const handlerPath = path.join(handlersDir, handler);
if (fs.statSync(handlerPath).isDirectory()) {
for (const file of fs.readdirSync(handlerPath)) {
if (file.endsWith('.json')) {
const name =
`${handler}_${path.basename(file, '.json')}`.replace(/-/g, '_');
properties[name] = {$ref: path.join(handlerPath, file)};
}
}
addSchemasFromDir(handlerPath, `${handler}_`);
}
}
}

// Add service schemas
const servicesDir = path.join(SOURCE_ROOT, 'services');
if (fs.existsSync(servicesDir)) {
for (const service of fs.readdirSync(servicesDir)) {
const servicePath = path.join(servicesDir, service);
if (fs.statSync(servicePath).isDirectory()) {
addSchemasFromDir(servicePath, `${service}_`);
}
}
}
Expand Down
Loading
Loading