UNPKG

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

126 lines 4.94 kB
"use strict"; 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.SourceLoader = void 0; const fs = __importStar(require("fs")); const ts_morph_1 = require("ts-morph"); const file_discovery_1 = require("./file-discovery"); const utils_1 = require("./utils"); class SourceLoader { /** * Create a ts-morph project with proper file structure */ static async createProject(input, options = {}) { if ((0, utils_1.isSourceCode)(input)) { // For source code, use in-memory file system (current behavior) return this.createInMemoryProject(input, options.compilerOptions); } // For files/directories, use real file system return this.createFileSystemProject(input, options); } /** * Create project with real file system for proper import resolution */ static async createFileSystemProject(input, options) { const files = await file_discovery_1.FileDiscovery.discoverFiles(input, options); if (files.length === 0) { throw new Error(`No TypeScript files found for input: ${input}`); } // Create project with real file system const project = new ts_morph_1.Project({ compilerOptions: options.compilerOptions || { target: ts_morph_1.ScriptTarget.ES2020, module: ts_morph_1.ModuleKind.CommonJS, strict: true, moduleResolution: ts_morph_1.ModuleResolutionKind.NodeJs, esModuleInterop: true, allowSyntheticDefaultImports: true, skipLibCheck: true, skipDefaultLibCheck: true, }, // Use real file system, not in-memory useInMemoryFileSystem: false, }); // Add discovered files to the project const sourceFiles = files.map((file) => project.addSourceFileAtPath(file.path)); return { project, sourceFiles }; } /** * Create in-memory project for source code strings */ static createInMemoryProject(sourceCode, compilerOptions) { const project = new ts_morph_1.Project({ compilerOptions: compilerOptions || { target: "ES2020", module: "CommonJS", strict: true, }, useInMemoryFileSystem: true, }); const sourceFile = project.createSourceFile("temp.ts", sourceCode); return { project, sourceFiles: [sourceFile] }; } /** * Combine multiple files into a single source content */ static async combineFiles(files) { if (files.length === 0) { throw new Error("No files to combine"); } // Read file contents const fileContents = await Promise.all(files.map(async (file) => ({ path: file.path, content: await fs.promises.readFile(file.path, "utf-8"), }))); // For single file, return as-is if (fileContents.length === 1) { return { content: fileContents[0].content, filePath: fileContents[0].path, files: fileContents, }; } // Combine all file contents with proper separation const combinedContent = fileContents .map((f) => `// File: ${f.path}\n${f.content}`) .join("\n\n"); return { content: combinedContent, files: fileContents, }; } } exports.SourceLoader = SourceLoader; //# sourceMappingURL=source-loader.js.map