@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
76 lines • 2.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_ADD_METHOD_CLASS_OPTIONS = void 0;
exports.CoerceMethodClassLegacy = CoerceMethodClassLegacy;
exports.AddMethodClass = AddMethodClass;
const ts_morph_1 = require("ts-morph");
const schematics_utilities_1 = require("@rxap/schematics-utilities");
const coerce_imports_1 = require("./ts-morph/coerce-imports");
exports.DEFAULT_ADD_METHOD_CLASS_OPTIONS = {
structures: [],
returnType: 'any',
parameterType: 'any',
isAsync: false,
statements: null,
implements: [],
typeParameters: [],
decorators: [],
ctors: [],
};
function CoerceMethodClassLegacy(sourceFile, name, options = {}) {
name = (0, schematics_utilities_1.CoerceSuffix)(name, 'Method');
const hasClass = !!sourceFile.getClass(name);
if (!hasClass) {
AddMethodClass(sourceFile, name, options);
}
}
function AddMethodClass(sourceFile, name, options = {}) {
var _a, _b;
const parameters = Object.assign({}, exports.DEFAULT_ADD_METHOD_CLASS_OPTIONS, options);
name = (0, schematics_utilities_1.CoerceSuffix)(name, 'Method');
const decorators = (_a = options.decorators) !== null && _a !== void 0 ? _a : [];
if (!decorators.some(d => d.name === 'Injectable')) {
decorators.push({
name: 'Injectable',
arguments: [],
});
}
sourceFile.addClass({
name: name,
isExported: true,
decorators,
typeParameters: parameters.typeParameters,
implements: [
`Method<${parameters.returnType}, ${parameters.parameterType}>`,
...parameters.implements,
],
ctors: parameters.ctors,
methods: [
{
name: 'call',
isAsync: parameters.isAsync,
scope: ts_morph_1.Scope.Public,
parameters: [
{
name: 'parameters',
type: parameters.parameterType,
},
],
returnType: parameters.isAsync ? `Promise<${parameters.returnType}>` : parameters.returnType,
statements: (_b = parameters.statements) !== null && _b !== void 0 ? _b : [],
},
],
});
(0, coerce_imports_1.CoerceImports)(sourceFile, [
{
namedImports: ['Injectable'],
moduleSpecifier: '@angular/core',
},
{
namedImports: ['Method'],
moduleSpecifier: '@rxap/rxjs',
},
]);
(0, coerce_imports_1.CoerceImports)(sourceFile, parameters.structures);
}
//# sourceMappingURL=add-method-class.js.map