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

87 lines 3.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceComponent = CoerceComponent; const utilities_1 = require("@rxap/utilities"); const ts_morph_1 = require("ts-morph"); const coerce_class_1 = require("../coerce-class"); const coerce_decorator_1 = require("../coerce-decorator"); const coerce_imports_1 = require("../coerce-imports"); function CoerceComponent(sourceFile, name, options = {}, classStructure = {}) { var _a; const className = (0, utilities_1.classify)((0, utilities_1.CoerceSuffix)((0, utilities_1.dasherize)(name), '-component')); const { prefix, changeDetection, } = options; let { selector, template, templateUrl, styles, styleUrls, } = options; if (selector !== false) { selector !== null && selector !== void 0 ? selector : (selector = prefix ? `${prefix}-${(0, utilities_1.dasherize)(name)}` : (0, utilities_1.dasherize)(name)); if (selector.includes('{{prefix}}')) { if (!prefix) { throw new Error(`The selector '${selector}' contains a template expression '{{prefix}}' but no prefix is provided`); } selector = selector.replace('{{prefix}}', prefix); } if (selector.match(/\{\{.*}}/)) { throw new Error(`The selector '${selector}' contains an invalid templates expression`); } } if (templateUrl === true || !template) { template = undefined; templateUrl = `./${(0, utilities_1.dasherize)(name)}.component.html`; } if (styleUrls === true || !styles) { styles = undefined; styleUrls = [`./${(0, utilities_1.dasherize)(name)}.component.scss`]; } if (!templateUrl) { template !== null && template !== void 0 ? template : (template = ''); } if (!styleUrls) { styles !== null && styles !== void 0 ? styles : (styles = '[]'); } if (Array.isArray(styleUrls)) { styleUrls = `['${styleUrls.join('\',\n\'')}']`; } if (styleUrls && !(styleUrls === null || styleUrls === void 0 ? void 0 : styleUrls.startsWith('['))) { styleUrls = `[${styleUrls}]`; } (_a = classStructure.isExported) !== null && _a !== void 0 ? _a : (classStructure.isExported = true); const classDeclaration = (0, coerce_class_1.CoerceClass)(sourceFile, className, classStructure); const componentOptions = { standalone: 'true', }; if (selector) { componentOptions['selector'] = w => w.quote(selector); } if (changeDetection) { componentOptions['changeDetection'] = `ChangeDetectionStrategy.${changeDetection}`; (0, coerce_imports_1.CoerceImports)(sourceFile, { namedImports: ['Component', 'ChangeDetectionStrategy'], moduleSpecifier: '@angular/core', }); } if (template) { componentOptions['template'] = w => w.quote(template); } if (templateUrl) { componentOptions['templateUrl'] = w => w.quote(templateUrl); } if (styles) { componentOptions['styles'] = w => w.quote(styles); } if (styleUrls) { componentOptions['styleUrls'] = styleUrls; } const componentDecoratorDeclaration = (0, coerce_decorator_1.CoerceDecorator)(classDeclaration, 'Component', { arguments: [ ts_morph_1.Writers.object(componentOptions), ] }); const componentDecoratorObject = componentDecoratorDeclaration.getArguments()[0]; if (!componentDecoratorObject) { throw new Error(`Could not find component decorator object for component '${className}'`); } if (!(componentDecoratorObject.isKind(ts_morph_1.SyntaxKind.ObjectLiteralExpression))) { throw new Error(`Component decorator object for component '${className}' is not an object literal expression`); } return { classDeclaration, componentDecoratorObject }; } //# sourceMappingURL=coerce-component.js.map