prisma-zod-generator
Version:
Prisma 2+ generator to emit Zod schemas from your Prisma schema
76 lines • 3.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFileSafely = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const transformer_1 = __importDefault(require("../transformer"));
const formatFile_1 = require("./formatFile");
const singleFileAggregator_1 = require("./singleFileAggregator");
const writeIndexFile_1 = require("./writeIndexFile");
const safeOutputManagement_1 = require("./safeOutputManagement");
const writeFileSafely = async (writeLocation, content, addToIndex = true) => {
try {
// In single-file mode, we don't write per-file; we append to aggregator
// BUT skip single file mode for Pro generator files (api-docs, etc.)
const isProGeneratorFile = writeLocation.includes('/generated/pro/') && !writeLocation.includes('/generated/zod/');
if ((0, singleFileAggregator_1.isSingleFileEnabled)() && !isProGeneratorFile) {
(0, singleFileAggregator_1.appendSingleFile)(writeLocation, content);
return;
}
const dir = path_1.default.dirname(writeLocation);
fs_1.default.mkdirSync(dir, { recursive: true });
// Track directory creation in manifest (but skip for Pro generator files)
const manifest = transformer_1.default.getCurrentManifest();
const outputPath = transformer_1.default.getOutputPath();
if (manifest && outputPath && !isProGeneratorFile) {
(0, safeOutputManagement_1.addDirectoryToManifest)(manifest, dir, outputPath);
}
// Control formatting via config, default off for schemas for speed
const cfg = transformer_1.default.getGeneratorConfig();
const isSchemasFile = /[\\\/]schemas[\\\/]/.test(writeLocation);
const shouldFormatSchemas = (cfg === null || cfg === void 0 ? void 0 : cfg.formatGeneratedSchemas) === true;
// For Pro generator files, write directly without formatting or manifest tracking
if (isProGeneratorFile) {
fs_1.default.writeFileSync(writeLocation, content);
}
else {
if (isSchemasFile && !shouldFormatSchemas) {
fs_1.default.writeFileSync(writeLocation, content);
}
else {
fs_1.default.writeFileSync(writeLocation, await (0, formatFile_1.formatFile)(content));
}
}
// Track file creation in manifest (but skip for Pro generator files)
if (manifest && outputPath && !isProGeneratorFile) {
(0, safeOutputManagement_1.addFileToManifest)(manifest, writeLocation, outputPath);
}
if (addToIndex) {
try {
// Avoid bloating index in minimal mode: don't add object schemas or helper files
const isObjectSchema = /\/objects\//.test(writeLocation);
const isHelper = /Include\.schema|Select\.schema|Aggregate|GroupBy/i.test(writeLocation);
const isResult = /\/results\//.test(writeLocation);
if (!(isObjectSchema || isHelper) || isResult) {
(0, writeIndexFile_1.addIndexExport)(writeLocation);
}
}
catch {
// Fallback to adding without filtering if any error occurs
(0, writeIndexFile_1.addIndexExport)(writeLocation);
}
}
}
catch (err) {
console.error(`[writeFileSafely] Failed to write: ${writeLocation}`);
console.error('[writeFileSafely] Content that failed to format:');
console.error(content);
console.error('[writeFileSafely] Error details:');
console.error(err);
}
};
exports.writeFileSafely = writeFileSafely;
//# sourceMappingURL=writeFileSafely.js.map