UNPKG

@rxap/ts-morph

Version:

Provides utilities for manipulating TypeScript code using the ts-morph library. It offers a fluent API to add, modify, and remove code elements such as classes, decorators, imports, and properties in both Angular and NestJS projects. This package simplifi

91 lines 3.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceNestAppConfig = CoerceNestAppConfig; const ts_morph_1 = require("ts-morph"); const coerce_imports_1 = require("../coerce-imports"); const coerce_variable_declaration_1 = require("../coerce-variable-declaration"); function CoerceNestAppConfig(sourceFile, options) { const { itemList, overwrite } = options; const objVariableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, 'validationSchema', { type: 'SchemaMap', initializer: '{}', }, { isExported: false }); (0, coerce_imports_1.CoerceImports)(sourceFile, [ { namedImports: [ 'SchemaMap', ], moduleSpecifier: 'joi', }, { moduleSpecifier: 'joi', namespaceImport: 'Joi', }, ]); const schemaVariableDeclaration = (0, coerce_variable_declaration_1.CoerceVariableDeclaration)(sourceFile, 'VALIDATION_SCHEMA', { initializer: 'Joi.object(validationSchema)', }); const objIndex = objVariableDeclaration.getVariableStatement().getChildIndex(); const schemaIndex = schemaVariableDeclaration.getVariableStatement().getChildIndex(); const existingNodes = []; for (let index = objIndex + 1; index < schemaIndex; index++) { existingNodes.push(sourceFile.getStatements()[index]); } const existingExpressions = existingNodes.filter(node => node.getKind() === ts_morph_1.SyntaxKind.ExpressionStatement); const validationSchemaExpressions = existingExpressions.filter(node => node.getText().startsWith('validationSchema')); for (const item of itemList) { const existing = validationSchemaExpressions.find(node => node.getText() .startsWith(`validationSchema['${item.name}']`)); if (existing) { if (overwrite) { existing.replaceWithText(writeValidationSchemaExpression(item)); } } else { sourceFile.insertStatements(schemaIndex, writeValidationSchemaExpression(item)); } } } function writeValidationSchemaExpression(item) { return w => { w.write(`validationSchema['${item.name}'] = `); const value = item.builder ? item.builder(item) : buildValidationSchemaExpressionValue(item); if (typeof value === 'string') { w.write(value); } else { value(w); } }; } function buildValidationSchemaExpressionValue(item) { return w => { var _a; w.write(`Joi.${(_a = item.type) !== null && _a !== void 0 ? _a : 'string'}()`); if (item.defaultValue) { if (typeof item.defaultValue === 'string') { if (item.type === 'string') { w.write(`.default(`); // wrap the string in single quotes if it is not already if ((!item.defaultValue.startsWith("'") && !item.defaultValue.endsWith("'")) || (!item.defaultValue.startsWith('"') && !item.defaultValue.endsWith('"'))) { w.quote(item.defaultValue); } else { w.write(item.defaultValue); } w.write(`)`); } else { w.write(`.default(${item.defaultValue})`); } } else { w.write('.default('); item.defaultValue(w); w.write(')'); } } w.write(';'); }; } //# sourceMappingURL=coerce-nest-app-config.js.map