@taiga-ui/cdk
Version:
Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance
72 lines (71 loc) • 4.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const workspace_1 = require("@schematics/angular/utility/workspace");
const ng_morph_1 = require("ng-morph");
const constants_1 = require("../constants");
const get_projects_1 = require("../utils/get-projects");
const remove_module_1 = require("../utils/remove-module");
const elements_1 = require("../utils/templates/elements");
const get_component_templates_1 = require("../utils/templates/get-component-templates");
const template_resource_1 = require("../utils/templates/template-resource");
function tuiLetMigrationGenerator(options) {
return (tree) => tslib_1.__awaiter(this, void 0, void 0, function* () {
var _a;
const workspace = yield (0, workspace_1.getWorkspace)(tree);
const [project] = (0, get_projects_1.getProjects)(options, workspace);
const root = (_a = project === null || project === void 0 ? void 0 : project.root) !== null && _a !== void 0 ? _a : project === null || project === void 0 ? void 0 : project.sourceRoot;
if (root === undefined) {
!options['skip-logs'] &&
(0, ng_morph_1.errorLog)('[ERROR]: Target project not found in current workspace');
return;
}
const prj = (0, ng_morph_1.createProject)(tree, root, constants_1.ALL_FILES);
const fileSystem = prj.getFileSystem().fs;
(0, ng_morph_1.setActiveProject)(prj);
(0, remove_module_1.removeModule)('TuiLet', '@taiga-ui/cdk');
const resources = (0, get_component_templates_1.getComponentTemplates)(constants_1.ALL_FILES);
for (const resource of resources) {
const path = fileSystem.resolve((0, template_resource_1.getPathFromTemplateResource)(resource));
const recorder = fileSystem.edit(path);
const template = (0, template_resource_1.getTemplateFromTemplateResource)(resource, fileSystem);
const templateOffset = (0, template_resource_1.getTemplateOffset)(resource);
const attributeName = '*tuiLet';
const elements = (0, elements_1.findElementsWithAttribute)(template, attributeName);
for (const { sourceCodeLocation, attrs, tagName, childNodes } of elements) {
const { name, value } = attrs.find((attr) => attr.name === attributeName.toLowerCase());
const indent = sourceCodeLocation.startOffset -
(template.lastIndexOf('\n', sourceCodeLocation.startOffset) + 1);
const indentStr = ' '.repeat(indent);
const [expr, key] = value.split(' as ').map((c) => c.trim());
if (new RegExp(`@let\\s+${key}\\s+=`).test(template)) {
!options['skip-logs'] &&
(0, ng_morph_1.errorLog)(`The @let with key ${key} is already defined`);
continue;
}
recorder.insertLeft(templateOffset + sourceCodeLocation.startOffset, `@let ${key} = ${expr};\n${indentStr}`);
if (tagName === 'ng-container' && attrs.length === 1) {
if (childNodes.length) {
const firstNode = childNodes.at(0);
const lastNode = childNodes.at(0);
recorder.remove(templateOffset + sourceCodeLocation.startOffset, firstNode.sourceCodeLocation.startOffset -
sourceCodeLocation.startOffset);
recorder.remove(templateOffset + lastNode.sourceCodeLocation.endOffset, sourceCodeLocation.endOffset -
lastNode.sourceCodeLocation.endOffset);
}
else {
recorder.remove(templateOffset + sourceCodeLocation.startOffset, sourceCodeLocation.endOffset -
sourceCodeLocation.startOffset);
}
}
else {
const { startOffset, endOffset } = sourceCodeLocation.attrs[name];
recorder.remove(templateOffset + startOffset - 1, endOffset - startOffset + 1);
}
}
}
fileSystem.commitEdits();
(0, ng_morph_1.saveActiveProject)();
});
}
exports.default = tuiLetMigrationGenerator;