Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(demo): replace all hard-coded documentation relative links with DemoRoute #7642

Merged
merged 5 commits into from
Jun 7, 2024

Conversation

nsbarsukov
Copy link
Member

Closes #7186

import type {Rule, Tree} from '@angular-devkit/schematics';
import {type DevkitFileSystem, getNgComponents, saveActiveProject, Scope} from 'ng-morph';

import {DemoRoute} from '../../../../demo/src/modules/app/demo-routes';
import {ALL_TS_FILES} from '../../constants';
import {addUniqueImport} from '../../utils/add-unique-import';
import {findElementsWithAttributeOnTag} from '../../utils/templates/elements';
import {getComponentTemplates} from '../../utils/templates/get-component-templates';
import {
    getPathFromTemplateResource,
    getTemplateFromTemplateResource,
    getTemplateOffset,
} from '../../utils/templates/template-resource';
import {getFileSystem} from '../utils/get-file-system';

const TAG_NAME = 'a';
const ATTR_NAME = 'routerLink';

function migrate(fileSystem: DevkitFileSystem): void {
    const componentWithTemplatesPaths = getComponentTemplates(ALL_TS_FILES);

    componentWithTemplatesPaths.forEach(resource => {
        const path = fileSystem.resolve(getPathFromTemplateResource(resource));
        const recorder = fileSystem.edit(path);

        const template = getTemplateFromTemplateResource(resource, fileSystem);
        const templateOffset = getTemplateOffset(resource);

        const elements = findElementsWithAttributeOnTag(
            template,
            [ATTR_NAME],
            [TAG_NAME],
            element =>
                Boolean(
                    element.attrs
                        .find(attr => attr.name === ATTR_NAME.toLowerCase())
                        ?.value.startsWith('/'),
                ),
        );

        elements.forEach(element => {
            const {name, value} =
                element.attrs.find(attr => ATTR_NAME.toLowerCase() === attr.name) || {};

            if (!name || !value) {
                return;
            }

            const {startOffset, endOffset} = element.sourceCodeLocation?.attrs?.[
                name
            ] || {startOffset: 0, endOffset: 0};
            const dictionaryKey =
                Object.entries(DemoRoute).find(
                    ([_, dictValue]) => dictValue === value,
                )?.[0] || 'TODO';

            recorder.remove(templateOffset + startOffset, endOffset - startOffset);
            recorder.insertRight(
                templateOffset + startOffset,
                `[${ATTR_NAME}]="docRoutes.${dictionaryKey}"`,
            );

            const [ngComponent] = getNgComponents(resource.componentPath);

            addUniqueImport(
                ngComponent.getSourceFile().getFilePath(),
                'DemoRoute',
                '@demo/routes',
            );

            !ngComponent.getProperty('docRoutes') &&
                ngComponent.addProperty({
                    name: 'docRoutes',
                    initializer: 'DemoRoute',
                    isReadonly: true,
                    scope: Scope.Protected,
                });
        });
    });
}

export function run(): Rule {
    return (tree: Tree) => {
        const fileSystem = getFileSystem(tree);

        migrate(fileSystem);

        fileSystem.commitEdits();
        saveActiveProject();
    };
}

@nsbarsukov nsbarsukov self-assigned this Jun 5, 2024
Copy link

lumberjack-bot bot commented Jun 5, 2024

Pull request was closed ✔️

All saved screenshots (for current PR) were deleted 🗑️

Copy link
Contributor

github-actions bot commented Jun 5, 2024

Visit the preview URL for this PR (updated for commit e7a7997):

https://taiga-ui--pr7642-migrate-demo-routes-io71vjm7.web.app

(expires Sat, 08 Jun 2024 13:03:04 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 4b5ece1e114386f6a105425ef799091475b249eb

Copy link

bundlemon bot commented Jun 5, 2024

BundleMon

Files updated (3)
Status Path Size Limits
demo/browser/main.(hash).js
299.04KB (+44B +0.01%) +10%
demo/browser/runtime.(hash).js
41.39KB (-16B -0.04%) +10%
demo/browser/styles.(hash).css
13.75KB (-158B -1.11%) +10%
Unchanged files (2)
Status Path Size Limits
demo/browser/vendor.(hash).js
243.07KB +10%
demo/browser/polyfills.(hash).js
11.21KB +10%

Total files change -130B -0.02%

Groups updated (1)
Status Path Size Limits
demo/browser/*..js
7.42MB (+1.96KB +0.03%) -

Final result: ✅

View report in BundleMon website ➡️


Current branch size history | Target branch size history

@nsbarsukov nsbarsukov merged commit 943f9f9 into main Jun 7, 2024
25 of 26 checks passed
@nsbarsukov nsbarsukov deleted the migrate-demo-routes branch June 7, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

📚 - Replace all hard-coded documentation relative links with DemoRoute
3 participants