UNPKG

@taiga-ui/cdk

Version:

Base library for creating Angular components and applications using Taiga UI principles regarding of actual visual appearance

103 lines (102 loc) 4.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapWithTuiRoot = void 0; 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_component_from_identifier_1 = require("../../utils/get-component-from-identifier"); const get_project_target_options_1 = require("../../utils/get-project-target-options"); const get_projects_1 = require("../../utils/get-projects"); const get_standalone_bootstrap_function_1 = require("../../utils/get-standalone-bootstrap-function"); function addTuiRoot(filePath, context, tree) { const buffer = tree.read(filePath); if (!buffer) { context.logger.error(`Could not read the default template file within the project ${filePath}`); context.logger.info('Consider manually wrapping content of your app with tui-root'); return; } const htmlContent = buffer.toString(); const openTag = '<tui-root>\n'; const closeTag = '\n</tui-root>'; if (htmlContent.includes(openTag)) { return; } const recorder = tree.beginUpdate(filePath); recorder.insertLeft(0, openTag); recorder.insertLeft(templateLength(htmlContent), closeTag); tree.commitUpdate(recorder); context.logger.info(`Content of the app was wrapped with tui-root component in ${filePath}`); } function getAppTemplatePath(mainPath) { const standaloneBootstrapFunction = (0, get_standalone_bootstrap_function_1.getStandaloneBootstrapFunction)(mainPath); if (standaloneBootstrapFunction) { const [componentIdentifier] = standaloneBootstrapFunction.getArguments(); if (componentIdentifier) { const component = (0, get_component_from_identifier_1.getComponentFromIdentifier)(componentIdentifier); if (component) { return getTemplatePathFromComponent(component); } } return ''; } const mainModule = (0, ng_morph_1.getMainModule)(mainPath); if (!mainModule) { return ''; } const mainInitializer = getTemplateInitializer(mainModule, 'NgModule', 'declarations'); if (!mainInitializer || !ng_morph_1.Node.isArrayLiteralExpression(mainInitializer)) { return ''; } const appIdentifier = mainInitializer.getElements()[0]; const appComponent = appIdentifier.getDefinitionNodes()[0]; return getTemplatePathFromComponent(appComponent); } function getTemplatePathFromComponent(component) { const templateInitializer = getTemplateInitializer(component, 'Component', 'templateUrl'); const appComponentPath = component.getSourceFile().getFilePath().split('/'); return `${appComponentPath .splice(0, appComponentPath.length - 1) .join('/')}/${templateInitializer === null || templateInitializer === void 0 ? void 0 : templateInitializer.getText().replaceAll(/['"]/g, '')}`; } function getTemplateInitializer(classDeclaration, decoratorName, propertyName) { var _a; const decorator = classDeclaration.getDecorator(decoratorName); if (!decorator) { return null; } const [metadata] = decorator.getArguments(); if (!ng_morph_1.Node.isObjectLiteralExpression(metadata)) { return null; } const property = metadata.getProperty(propertyName); if (!ng_morph_1.Node.isPropertyAssignment(property)) { return null; } return (_a = property.getInitializer()) !== null && _a !== void 0 ? _a : null; } function wrapWithTuiRoot(options) { return (tree, context) => tslib_1.__awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(tree); const [project] = (0, get_projects_1.getProjects)(options, workspace); if (!project) { return; } const buildOptions = (0, get_project_target_options_1.getProjectTargetOptions)(project, 'build'); (0, ng_morph_1.setActiveProject)((0, ng_morph_1.createProject)(tree, '/', constants_1.ALL_FILES)); const appTemplatePath = getAppTemplatePath(buildOptions.main); (0, ng_morph_1.saveActiveProject)(); if (!appTemplatePath) { context.logger.error('Could not find the default main template file for this project.'); context.logger.info('Consider manually wrapping content of your app with tui-root'); context.logger.info('More information at https://taiga-ui.dev/getting-started'); return; } addTuiRoot(appTemplatePath, context, tree); }); } exports.wrapWithTuiRoot = wrapWithTuiRoot; function templateLength(template) { // utf8 with BOM adds an extra character to the beginning of the string return template.charCodeAt(0) === 0xfeff ? template.length - 1 : template.length; }