@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
40 lines (39 loc) • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getComponentTemplates = void 0;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("node:path"));
const ng_morph_1 = require("ng-morph");
function decoratorToTemplateResource(decorator) {
var _a, _b, _c;
const [metadata] = decorator.getArguments();
const templateUrl = metadata === null || metadata === void 0 ? void 0 : metadata.getProperty('templateUrl');
const template = metadata === null || metadata === void 0 ? void 0 : metadata.getProperty('template');
const componentPath = decorator.getSourceFile().getFilePath();
if (templateUrl) {
const templatePath = path.parse(((_a = templateUrl === null || templateUrl === void 0 ? void 0 : templateUrl.getInitializer()) === null || _a === void 0 ? void 0 : _a.getText().replaceAll(/['"`]/g, '')) || '');
return {
componentPath,
templatePath: getFullTemplatePath(templatePath, path.parse(componentPath)),
};
}
if (template) {
return {
componentPath,
template: ((_b = template.getInitializer()) === null || _b === void 0 ? void 0 : _b.getText()) || '',
offset: ((_c = template.getInitializer()) === null || _c === void 0 ? void 0 : _c.getStart()) || 0,
};
}
return null;
}
function getFullTemplatePath(templatePath, componentPath) {
return path.join(componentPath.dir, templatePath.dir, templatePath.name + templatePath.ext);
}
function getComponentTemplates(pattern, query) {
return (0, ng_morph_1.getClasses)(pattern, query)
.map((declaration) => declaration.getDecorator('Component'))
.filter((decorator) => !!decorator)
.map(decoratorToTemplateResource)
.filter((x) => Boolean(x));
}
exports.getComponentTemplates = getComponentTemplates;