UNPKG

prisma-zod-generator

Version:

Prisma 2+ generator to emit Zod schemas from your Prisma schema

113 lines 4.7 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; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.writeIndexFile = exports.addIndexExport = void 0; const path_1 = __importDefault(require("path")); const writeFileSafely_1 = require("./writeFileSafely"); const indexExports = new Set(); const addIndexExport = (filePath) => { indexExports.add(filePath); }; exports.addIndexExport = addIndexExport; function normalizePath(path) { if (typeof path !== 'string') { throw new TypeError('Expected argument path to be a string'); } if (path === '\\' || path === '/') return '/'; const len = path.length; if (len <= 1) return path; let prefix = ''; if (len > 4 && path[3] === '\\') { const ch = path[2]; if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') { path = path.slice(2); prefix = '//'; } } const segs = path.split(/[/\\]+/); return prefix + segs.join('/'); } const writeIndexFile = async (indexPath, importExtension = '') => { const rows = Array.from(indexExports).map((filePath) => { let relativePath = path_1.default.relative(path_1.default.dirname(indexPath), filePath); if (relativePath.endsWith('.ts')) { relativePath = relativePath.slice(0, relativePath.lastIndexOf('.ts')); } const normalized = normalizePath(relativePath); return `export * from './${normalized}${importExtension}'`; }); // Additionally, ensure directory-level exports for common folders if their index.ts was not added explicitly const dirExports = []; const baseDir = path_1.default.dirname(indexPath); for (const dir of ['enums', 'objects', 'models']) { const dirIndex = path_1.default.join(baseDir, dir, 'index.ts'); // If an index.ts exists, ensure it's exported try { const fs = await Promise.resolve().then(() => __importStar(require('fs'))); if (fs.existsSync(dirIndex)) { const rel = normalizePath(path_1.default.relative(baseDir, dirIndex).replace(/\.ts$/, '')); const line = `export * from './${rel}${importExtension}'`; if (!rows.includes(line)) dirExports.push(line); } } catch { // ignore } } // If variants/index.ts exists under schemas, ensure it's exported as well try { const fs = await Promise.resolve().then(() => __importStar(require('fs'))); const variantsIndex = path_1.default.join(baseDir, 'variants', 'index.ts'); if (fs.existsSync(variantsIndex)) { const rel = normalizePath(path_1.default.relative(baseDir, variantsIndex).replace(/\.ts$/, '')); const line = `export * from './${rel}${importExtension}'`; if (!rows.includes(line) && !dirExports.includes(line)) dirExports.push(line); } } catch { // ignore } const content = [...rows, ...dirExports].join('\n'); await (0, writeFileSafely_1.writeFileSafely)(indexPath, content, false); }; exports.writeIndexFile = writeIndexFile; //# sourceMappingURL=writeIndexFile.js.map