@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
50 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddToGlobalHealthEndpoint = AddToGlobalHealthEndpoint;
const utilities_1 = require("@rxap/utilities");
const ts_morph_1 = require("ts-morph");
const coerce_class_method_1 = require("../coerce-class-method");
const coerce_imports_1 = require("../coerce-imports");
function AddToGlobalHealthEndpoint(sourceFile, name) {
var _a, _b;
const classDeclaration = sourceFile.getClassOrThrow('HealthController');
const healthIndicatorClass = `${(0, utilities_1.classify)(name)}HealthIndicator`;
const methodDeclaration = (0, coerce_class_method_1.CoerceClassMethod)(classDeclaration, 'healthCheck', {
scope: ts_morph_1.Scope.Public,
returnType: 'Promise<HealthCheckResult>',
statements: [
w => {
w.writeLine('return this.health.check([');
w.write(']);');
},
],
decorators: [
{
name: 'Get',
arguments: [],
},
{
name: 'HealthCheck',
arguments: [],
},
],
});
(0, coerce_imports_1.CoerceImports)(sourceFile, [
{
moduleSpecifier: '@nestjs/terminus',
namedImports: ['HealthCheck', 'HealthCheckResult'],
},
{
moduleSpecifier: '@nestjs/common',
namedImports: ['Get'],
},
]);
const statement = methodDeclaration.getStatements().find(e => e.getText().match(/return this\.health\.check/));
const array = (_b = (_a = statement === null || statement === void 0 ? void 0 : statement.getChildAtIndex(1)) === null || _a === void 0 ? void 0 : _a.getChildAtIndex(2)) === null || _b === void 0 ? void 0 : _b.getFirstChild();
if (array === null || array === void 0 ? void 0 : array.isKind(ts_morph_1.SyntaxKind.ArrayLiteralExpression)) {
array.addElement(w => {
w.write(`() => this.${(0, utilities_1.camelize)(healthIndicatorClass)}.isHealthy()`);
});
}
}
//# sourceMappingURL=add-to-global-health-endpoint.js.map