@rxap/schematics-ts-morph
Version:
This package provides utilities for manipulating TypeScript code using ts-morph, particularly for Angular and NestJS projects. It offers functions to add, coerce, and modify code elements like classes, methods, decorators, and imports. The package also in
79 lines • 3.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CoerceOperation = CoerceOperation;
const schematics_1 = require("@angular-devkit/schematics");
const ts_morph_1 = require("@rxap/ts-morph");
const utilities_1 = require("@rxap/utilities");
const path_1 = require("path");
const ts_morph_transform_1 = require("../ts-morph-transform");
const build_nest_controller_name_1 = require("./build-nest-controller-name");
const coerce_nest_controller_1 = require("./coerce-nest-controller");
function CoerceOperation(options) {
const { controllerName, project, paramList = [], feature, shared, operationName, skipCoerce, backend, } = options;
let { nestModule, directory, path } = options;
/**
* If the module is not specified. This controller has an own module. Else the
* module is originated by another controller.
*
* **Example**
* true:
* The controller ReportDetailsController should be extended with getById Operation.
* And the controller is used in the module ReportDetailsModule
*
* name = "report-details"
* module = undefined
*
* false:
* The controller ReportDetailsNotificationController should be extend with getById Operation.
* And the controller ise used in the module ReportDetailsModule
*
* name = "notification"
* module = "report-details"
*/
const isFirstBornSibling = !nestModule || nestModule === controllerName;
const nestController = (0, build_nest_controller_name_1.BuildNestControllerName)({
controllerName,
nestModule,
});
if (!nestModule || isFirstBornSibling) {
nestModule = nestController;
}
directory = (0, path_1.join)(directory !== null && directory !== void 0 ? directory : '', nestModule);
const operationPathParameters = paramList.filter(p => !p.fromParent).map(p => p.name);
if (operationPathParameters.length) {
if (!path) {
path = operationPathParameters.map(p => `:${p}`).join('/');
}
else {
const notFound = operationPathParameters.filter(p => !path.includes(p));
path += '/' + notFound.map(p => `:${p}`).join('/');
}
}
return (0, schematics_1.chain)([
() => console.log(`Coerce Operation '${operationName}' with path '${path !== null && path !== void 0 ? path : '<empty>'}' in the controller '${nestController}' in the module '${nestModule}'`.blue),
() => console.log(`Operation path parameters: ${paramList.map(p => (p.fromParent ? '^' : '') + p.name).join(', ')}`.grey),
(0, coerce_nest_controller_1.CoerceNestController)({
project,
feature,
shared,
backend,
directory,
coerceModule: !skipCoerce,
name: nestController,
nestModule,
}),
(0, ts_morph_transform_1.TsMorphNestProjectTransformRule)({
project,
feature,
shared,
directory,
// must be set to false are some specializations of this function need to access more files then only the
// controller and module source file
filter: false,
backend,
}, (project, [controllerSourceFile, moduleSourceFile]) => {
(0, ts_morph_1.CoerceNestOperation)(controllerSourceFile, options, moduleSourceFile);
}, [`${nestController}.controller.ts?`, `${(0, utilities_1.dasherize)(nestModule)}.module.ts?`]),
]);
}
//# sourceMappingURL=coerce-operation.js.map