typescript-runtime-schemas
Version:
A TypeScript schema generation tool that extracts Zod schemas from TypeScript source files with runtime validation support. Generate validation schemas directly from your existing TypeScript types with support for computed types and constraint-based valid
127 lines • 5.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractCommand = void 0;
const commander_1 = require("commander");
const schema_extractor_1 = require("../../core/schema-extractor");
const config_1 = require("../config");
const formatters_1 = require("../formatters");
const utils_1 = require("../utils");
exports.extractCommand = new commander_1.Command('extract')
.description('Extract and display schemas from TypeScript types')
.argument('<input>', 'Input file, directory, or source code')
.option('--source', 'Treat input as source code string instead of file path')
.option('--type <name>', 'Extract only a specific type')
.option('--format <format>', 'Output format (json, yaml, table)', 'table')
.option('--include-source-info', 'Include source file information')
.option('--glob <pattern>', 'File pattern to match')
.option('--exclude <pattern>', 'File pattern to exclude')
.option('--config <file>', 'Configuration file path')
.option('--concurrency <number>', 'Number of concurrent workers', parseInt)
.option('--no-parallel', 'Disable parallel processing')
.option('--chunk-size <number>', 'Chunk size for parallel processing', parseInt)
.option('--progress', 'Show progress information')
.action(async (input, options) => {
try {
// Load configuration
const config = options.config
? await Promise.resolve().then(() => __importStar(require('../config'))).then((m) => m.loadConfig(options.config))
: await (0, config_1.findAndLoadConfig)();
const mergedOptions = (0, config_1.mergeConfig)(config, {
...options,
input,
});
// Build parallel processing options
const parallelOptions = {
concurrency: mergedOptions.concurrency,
useParallelProcessing: !mergedOptions.noParallel,
chunkSize: mergedOptions.chunkSize,
enableProgressTracking: mergedOptions.progress,
onProgress: mergedOptions.progress
? (progress) => {
const percentage = progress.percentage.toFixed(1);
const rate = progress.rate.toFixed(1);
const eta = Math.round(progress.eta / 1000);
process.stderr.write(`\râš¡ Processing: ${progress.completed}/${progress.total} (${percentage}%) - ${rate} types/sec - ETA: ${eta}s`);
}
: undefined,
};
// Extract schemas
let result;
if (mergedOptions.type) {
// Extract single type
const schema = await schema_extractor_1.SchemaExtractor.extractSingleSchema(input, mergedOptions.type, {
includeSourceInfo: mergedOptions.includeSourceInfo,
glob: mergedOptions.glob,
exclude: mergedOptions.exclude,
parallel: parallelOptions,
});
if (!schema) {
console.error(`Type '${mergedOptions.type}' not found or does not extend SupportsRuntimeValidation`);
process.exit(1);
}
result = { schemas: [schema] };
}
else {
// Extract all schemas
result = await schema_extractor_1.SchemaExtractor.extract(input, {
includeSourceInfo: mergedOptions.includeSourceInfo,
glob: mergedOptions.glob,
exclude: mergedOptions.exclude,
parallel: parallelOptions,
});
}
// Clear progress line if it was shown
if (mergedOptions.progress) {
process.stderr.write('\n');
}
// Format and display output
const output = (0, formatters_1.formatOutput)(result.schemas, mergedOptions.format || 'table');
console.log(output);
// Show summary if not in quiet mode
if (!process.argv.includes('--quiet')) {
console.error(`\n✅ Extracted ${result.schemas.length} schemas`);
if (result.totalTypesFound !== undefined) {
console.error(`📊 ${result.totalTypesFound} total types found`);
console.error(`🎯 ${result.typesWithSupportsRuntimeValidation} types with validation`);
console.error(`âš¡ Processing time: ${result.processingTime}ms`);
}
}
}
catch (error) {
(0, utils_1.handleError)(error, 'Failed to extract schemas');
}
});
//# sourceMappingURL=extract.js.map