@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
75 lines • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.migratePortals = migratePortals;
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("node:path"));
const ng_morph_1 = require("ng-morph");
const constants_1 = require("../../../constants");
const add_unique_import_1 = require("../../../utils/add-unique-import");
const push_to_array_property_1 = require("../../../utils/push-to-array-property");
const TUI_PORTALS = 'TuiPortals';
const TUI_VCR_CLASS = 'TuiVCR';
const TUI_VCR_DIRECTIVE = 'tuiVCR';
const TAIGA_CDK = '@taiga-ui/cdk';
const VIEW_CONTAINER_REF = '#viewContainer';
function migratePortals(tree, _options) {
(0, ng_morph_1.getSourceFiles)(constants_1.ALL_TS_FILES).forEach((sourceFile) => {
migrateSourceFile(sourceFile, tree);
});
}
function migrateSourceFile(sourceFile, tree) {
var _a;
const hasTuiPortalsImport = sourceFile
.getImportDeclarations()
.some((imp) => imp.getModuleSpecifierValue() === TAIGA_CDK &&
imp.getNamedImports().some((ni) => ni.getName() === TUI_PORTALS));
if (!hasTuiPortalsImport) {
return;
}
for (const classDecl of sourceFile.getClasses()) {
const extendsClause = classDecl.getExtends();
if ((extendsClause === null || extendsClause === void 0 ? void 0 : extendsClause.getExpression().getText()) !== TUI_PORTALS) {
continue;
}
const decorator = classDecl.getDecorator('Component');
if (!decorator) {
continue;
}
const [metadata] = decorator.getArguments();
if (!ng_morph_1.Node.isObjectLiteralExpression(metadata)) {
continue;
}
const templateProp = metadata.getProperty('template');
const templateUrlProp = metadata.getProperty('templateUrl');
if (ng_morph_1.Node.isPropertyAssignment(templateProp)) {
const initializer = templateProp.getInitializer();
if (!initializer) {
continue;
}
const text = initializer.getText();
if (!text.includes(VIEW_CONTAINER_REF)) {
continue;
}
initializer.replaceWithText(text.replaceAll(VIEW_CONTAINER_REF, TUI_VCR_DIRECTIVE));
(0, push_to_array_property_1.pushToObjectArrayProperty)(metadata, 'imports', TUI_VCR_CLASS, { unique: true });
(0, add_unique_import_1.addUniqueImport)(sourceFile.getFilePath(), TUI_VCR_CLASS, TAIGA_CDK);
}
else if (ng_morph_1.Node.isPropertyAssignment(templateUrlProp)) {
const templateUrlInit = templateUrlProp.getInitializer();
if (!templateUrlInit) {
continue;
}
const templateUrl = templateUrlInit.getText().replaceAll(/['"`]/g, '');
const componentDir = path.dirname(sourceFile.getFilePath());
const templatePath = path.join(componentDir, templateUrl);
const content = (_a = tree.read(templatePath)) === null || _a === void 0 ? void 0 : _a.toString();
if (!(content === null || content === void 0 ? void 0 : content.includes(VIEW_CONTAINER_REF))) {
continue;
}
tree.overwrite(templatePath, content.replaceAll(VIEW_CONTAINER_REF, TUI_VCR_DIRECTIVE));
(0, push_to_array_property_1.pushToObjectArrayProperty)(metadata, 'imports', TUI_VCR_CLASS, { unique: true });
(0, add_unique_import_1.addUniqueImport)(sourceFile.getFilePath(), TUI_VCR_CLASS, TAIGA_CDK);
}
}
}
//# sourceMappingURL=migrate-portals.js.map