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
100 lines ⢠4.56 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.generateCommand = void 0;
const commander_1 = require("commander");
const schema_extractor_1 = require("../../core/schema-extractor");
const config_1 = require("../config");
const utils_1 = require("../utils");
exports.generateCommand = new commander_1.Command('generate')
.description('Generate Zod schema files from TypeScript types')
.argument('<input>', 'Input file or directory')
.argument('[output]', "Output directory (default: 'generated')")
.option('--preserve-structure', 'Preserve input directory structure')
.option('--single-file', 'Generate all schemas in a single file')
.option('--output-dir <dir>', 'Output directory')
.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')
.action(async (input, output, options) => {
const progress = new utils_1.ProgressIndicator();
try {
progress.start('Loading configuration...');
// 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,
output: output || options.outputDir || config?.outputDir || 'generated',
});
progress.stop('Configuration loaded');
progress.start('Extracting and generating schemas...');
// Extract and generate Zod schemas
const result = await schema_extractor_1.SchemaExtractor.extractAndWriteZodSchemas(input, {
includeSourceInfo: mergedOptions.includeSourceInfo ?? true,
glob: mergedOptions.glob,
exclude: mergedOptions.exclude,
output: {
outputDir: mergedOptions.output,
preserveStructure: mergedOptions.preserveStructure || !mergedOptions.singleFile,
writeToFile: true,
},
});
progress.stop('Generation completed');
// Show results
(0, utils_1.logInfo)(`\nā
Generated ${result.schemas.length} Zod schemas`);
(0, utils_1.logInfo)(`š Written to ${result.writtenFiles.length} files:`);
result.writtenFiles.forEach((file) => {
(0, utils_1.logInfo)(` - ${file}`);
});
if (result.schemas.length > 0) {
(0, utils_1.logInfo)(`\nš Generated schemas:`);
result.schemas.forEach((schema) => {
const sourceInfo = schema.sourceInfo
? ` (from ${schema.sourceInfo.filePath}:${schema.sourceInfo.line})`
: '';
(0, utils_1.logInfo)(` - ${schema.typeName}${sourceInfo}`);
});
}
}
catch (error) {
progress.stop();
(0, utils_1.handleError)(error, 'Failed to generate schemas');
}
});
//# sourceMappingURL=generate.js.map