Skip to content

Commit

Permalink
chore: add proprieraty-icons migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Jun 27, 2024
1 parent a3887ad commit 38b858e
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
16 changes: 8 additions & 8 deletions projects/cdk/schematics/ng-update/v4/migrate-icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import {
} from '../../../utils/colored-log';
import {getFileSystem} from '../../utils/get-file-system';
import {renameIcons} from './rename-icons';
import {renameProprietaryIcons} from './rename-proprietary-icons';

export function migrateIcons(options: TuiSchema): Rule {
return chain([
(tree: Tree, _: SchematicContext) => {
if (hasProprietaryIcons(tree)) {
// TODO: add proprietary-icons migration

return;
}

const fileSystem = getFileSystem(tree);

!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing strings...`);
renameIcons();
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing icons...`);

if (hasProprietaryIcons(tree)) {
renameProprietaryIcons();
} else {
renameIcons();
}

fileSystem.commitEdits();
saveActiveProject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference lib="es2021" />
import {getSourceFiles} from 'ng-morph';

import {ALL_FILES} from '../../../constants';

export function renameProprietaryIcons(pattern = ALL_FILES): void {
const sourceFiles = getSourceFiles(pattern);

sourceFiles.forEach(file => {
let text = file.getFullText();

const regex = /\btuiIcon(?!Button\b)[A-Z][a-zA-Z0-9]*\b/g;

text = text.replaceAll(regex, match => convertString(match));

file.replaceWithText(text);
});
}

function convertString(input: string): string {
const result = input
.replace(/^tuiIconTds/, '')
.replace(/SmallPragmatic$/, '')
.replace(/MediumPragmatic$/, '')
.replace(/Small$/, '')
.replace(/Medium$/, '')
.replaceAll(/([A-Z0-9])/g, '-$1')
.toLowerCase();

const pack = input.includes('Pragmatic') ? 'pragmatic' : 'fancy';
const size = input.includes('Medium') ? 'medium' : 'small';

return `@tui.${pack}.${size}.${result.startsWith('-') ? result.slice(1) : result}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
from: {name: 'TuiPortalModule', moduleSpecifier: '@taiga-ui/cdk'},
to: {name: 'TuiDropdownPortal', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TuiDropdownModule', moduleSpecifier: '@taiga-ui/core'},
to: {name: 'TuiDropdown', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TuiDroppableModule', moduleSpecifier: '@taiga-ui/cdk'},
to: {name: 'TuiDroppable', moduleSpecifier: '@taiga-ui/cdk'},
Expand Down Expand Up @@ -696,6 +700,20 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
moduleSpecifier: '@taiga-ui/core',
},
},
{
from: {name: 'TUI_ARROW', moduleSpecifier: '@taiga-ui/kit'},
to: {
name: 'TUI_ARROW',
moduleSpecifier: '@taiga-ui/legacy',
},
},
{
from: {name: 'TuiArrowComponent', moduleSpecifier: '@taiga-ui/kit'},
to: {
name: 'TuiArrowComponent',
moduleSpecifier: '@taiga-ui/legacy',
},
},
{
from: {name: 'TuiDialogModule', moduleSpecifier: '@taiga-ui/core'},
to: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,10 @@ export const MIGRATION_WARNINGS: MigrationWarning[] = [
message:
'TUI_INPUT_NUMBER_OPTIONS "precision" and "decimal" have been moved to TUI_FORMAT_NUMBER_OPTIONS. See https://taiga-ui.dev/components/input-number#options ',
},
{
name: 'TUI_ARROW',
moduleSpecifier: '@taiga-ui/kit',
message:
'TUI_ARROW is deprecated, Use tuiChevron directive instead. See example https://taiga-ui.dev/components/data-list#links',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ const TEMPLATE_AFTER = `
<button tuiIconButton icon="@tui.x">Button</button>
`;

const PROPRIETARY_TEMPLATE_BEFORE = `
<tui-avatar
avatarUrl="tuiIconTdsLockSmall"
text="alex inkin"
[rounded]="true"
></tui-avatar>
<tui-avatar
avatarUrl="tuiIconTdsTransportAutoRubleMedium"
></tui-avatar>
<button tuiIconButton icon="tuiIconTdsMoreMediumPragmatic">Button</button>
<button tuiIconButton icon="tuiIconTdsCheckMedium">Button</button>
`;

const PROPRIETARY_TEMPLATE_AFTER = `
<tui-avatar
avatarUrl="@tui.fancy.small.lock"
text="alex inkin"
[rounded]="true"
></tui-avatar>
<tui-avatar
avatarUrl="@tui.fancy.medium.transport-auto-ruble"
></tui-avatar>
<button tuiIconButton icon="@tui.pragmatic.medium.more">Button</button>
<button tuiIconButton icon="@tui.fancy.medium.check">Button</button>
`;

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;
Expand Down Expand Up @@ -94,6 +120,26 @@ describe('ng-update', () => {
expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER);
});

it('should migrate proprietary icons in ts files', async () => {
createSourceFile(
'package.json',
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/proprietary-icons": "~3.42.0"}}',
{overwrite: true},
);

saveActiveProject();

const tree = await runner.runSchematic(
'migrateIconsV4',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
);

expect(tree.readContent('test/app/proprietary-test.template.html')).toEqual(
PROPRIETARY_TEMPLATE_AFTER,
);
});

afterEach(() => {
resetActiveProject();
});
Expand All @@ -104,6 +150,11 @@ function createMainFiles(): void {

createSourceFile('test/app/test.template.html', TEMPLATE_BEFORE);

createSourceFile(
'test/app/proprietary-test.template.html',
PROPRIETARY_TEMPLATE_BEFORE,
);

createSourceFile(
'package.json',
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/addon-commerce": "~3.42.0"}}',
Expand Down

0 comments on commit 38b858e

Please sign in to comment.