@weverson_na/prisma-generator-nestjs-dto
Version:
Advanced Prisma Generator with Smart Merge v2: Creates DTO and Entity classes with AST-based preservation, intelligent import management, and modular architecture for NestJS
93 lines (92 loc) • 4.31 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.generate = exports.stringToBoolean = void 0;
const generator_helper_1 = require("@prisma/generator-helper");
const internals_1 = require("@prisma/internals");
const promises_1 = require("fs/promises");
const node_path_1 = __importDefault(require("node:path"));
const prettier_1 = require("prettier");
const smart_merge_content_v2_1 = require("./commands/smart-merge-content-v2");
const generators_1 = require("./generators");
const stringToBoolean = (input, defaultValue = false) => {
if (input === 'true') {
return true;
}
if (input === 'false') {
return false;
}
return defaultValue;
};
exports.stringToBoolean = stringToBoolean;
const generate = (options) => {
const output = (0, internals_1.parseEnvValue)(options.generator.output);
const { connectDtoPrefix = 'Connect', createDtoPrefix = 'Create', updateDtoPrefix = 'Update', dtoSuffix = 'Dto', entityPrefix = '', entitySuffix = '', fileNamingStyle = 'camel', decoratorConfigPath, } = options.generator.config;
const exportRelationModifierClasses = (0, exports.stringToBoolean)(options.generator.config.exportRelationModifierClasses, true);
const outputToNestJsResourceStructure = (0, exports.stringToBoolean)(options.generator.config.outputToNestJsResourceStructure, false);
const reExport = (0, exports.stringToBoolean)(options.generator.config.reExport, false);
const addExposePropertyDecorator = (0, exports.stringToBoolean)(options.generator.config.addExposePropertyDecorator, false);
const supportedFileNamingStyles = ['kebab', 'camel', 'pascal', 'snake'];
const isSupportedFileNamingStyle = (style) => supportedFileNamingStyles.includes(style);
if (!isSupportedFileNamingStyle(fileNamingStyle)) {
throw new Error(`'${fileNamingStyle}' is not a valid file naming style. Valid options are ${supportedFileNamingStyles
.map((s) => `'${s}'`)
.join(', ')}.`);
}
const results = (0, generators_1.run)({
output,
dmmf: options.dmmf,
exportRelationModifierClasses,
outputToNestJsResourceStructure,
connectDtoPrefix,
createDtoPrefix,
updateDtoPrefix,
dtoSuffix,
entityPrefix,
entitySuffix,
fileNamingStyle,
addExposePropertyDecorator,
decoratorConfigPath,
});
const indexCollections = {};
if (reExport) {
results.forEach(({ fileName }) => {
const dirName = node_path_1.default.dirname(fileName);
const { [dirName]: fileSpec } = indexCollections;
indexCollections[dirName] = {
fileName: (fileSpec === null || fileSpec === void 0 ? void 0 : fileSpec.fileName) || node_path_1.default.join(dirName, 'index.ts'),
content: [
(fileSpec === null || fileSpec === void 0 ? void 0 : fileSpec.content) || '',
`export * from './${node_path_1.default.basename(fileName, '.ts')}';`,
].join('\n'),
};
});
}
const mergeContent = new smart_merge_content_v2_1.SmartMergeContentV2();
return Promise.all(results
.concat(Object.values(indexCollections))
.map(async ({ fileName, content }) => {
await (0, promises_1.mkdir)(node_path_1.default.dirname(fileName), { recursive: true });
const fileExists = await (0, promises_1.access)(fileName)
.then(() => true)
.catch(() => false);
const writeContent = fileExists
? mergeContent.merge(await (0, promises_1.readFile)(fileName, 'utf8'), content)
: content;
const formattedFile = await (0, prettier_1.format)(writeContent, {
parser: 'typescript',
singleQuote: true,
});
return (0, promises_1.writeFile)(fileName, formattedFile);
}));
};
exports.generate = generate;
(0, generator_helper_1.generatorHandler)({
onManifest: () => ({
defaultOutput: '../src/generated/nestjs-dto',
prettyName: 'NestJS DTO Generator',
}),
onGenerate: exports.generate,
});