Skip to content

Commit

Permalink
feat(schematics): add migration for @tbank/tui-editor
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode committed Jun 5, 2024
1 parent 9125a62 commit 9d6bf85
Show file tree
Hide file tree
Showing 7 changed files with 1,726 additions and 1,804 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"**/addon-commerce/utils/get-currency-symbol.ts"
],
"ignoreWords": [
"tbank",
"Acpekt",
"avatarurl",
"benji",
Expand Down
3,325 changes: 1,523 additions & 1,802 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@
"@taiga-ui/prettier-config": "0.7.0",
"@taiga-ui/stylelint-config": "0.12.2",
"@taiga-ui/tsconfig": "0.11.0",
"@tbank/tui-editor": "1.33.0",
"@testing-library/cypress": "10.0.1",
"@tinkoff/ng-event-plugins": "3.2.0",
"@tinkoff/tui-editor": "1.25.1",
"@types/glob": "8.1.0",
"@types/node": "20.10.4",
"@types/parse5": "6.0.3",
Expand Down
5 changes: 5 additions & 0 deletions projects/cdk/schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
"version": "3.78.0",
"factory": "./ng-update/v3-78/index#updateToV3_78"
},
"updateToV3_83": {
"description": "Migrate to @tbank/tui-editor",
"version": "3.83.0",
"factory": "./ng-update/v3-83/index#updateToV3_83"
},
"updateToV4": {
"description": "Updates Taiga UI packages to v4",
"version": "4.0.0",
Expand Down
53 changes: 53 additions & 0 deletions projects/cdk/schematics/ng-update/v3-83/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {NodePackageInstallTask} from '@angular-devkit/schematics/tasks';
import {
addPackageJsonDependency,
getPackageJsonDependency,
removePackageJsonDependency,
saveActiveProject,
} from 'ng-morph';

import {ALL_TS_FILES} from '../../constants';
import {TuiSchema} from '../../ng-add/schema';
import {
FINISH_SYMBOL,
infoLog,
REPLACE_SYMBOL,
SMALL_TAB_SYMBOL,
titleLog,
} from '../../utils/colored-log';
import {getFileSystem} from '../utils/get-file-system';
import {replaceText} from '../utils/replace-text';

const OLD_PACKAGE = '@tinkoff/tui-editor';
const NEW_PACKAGE = '@tbank/tui-editor';
const NEW_PACKAGE_VERSION = '^1.33.0';

export function updateToV3_83(options: TuiSchema): Rule {
return (tree: Tree, context: SchematicContext): void => {
if (!getPackageJsonDependency(tree, OLD_PACKAGE)) {
!options['skip-logs'] &&
titleLog(`${FINISH_SYMBOL} No migrations required for ${OLD_PACKAGE}\n`);

return;
}

const fileSystem = getFileSystem(tree);

!options['skip-logs'] &&
infoLog(
`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing imports for ${OLD_PACKAGE}...`,
);

replaceText([{from: OLD_PACKAGE, to: NEW_PACKAGE}], ALL_TS_FILES);
removePackageJsonDependency(tree, OLD_PACKAGE);

addPackageJsonDependency(tree, {name: NEW_PACKAGE, version: NEW_PACKAGE_VERSION});
context.addTask(new NodePackageInstallTask());

fileSystem.commitEdits();
saveActiveProject();

!options['skip-logs'] && titleLog(`${FINISH_SYMBOL} successfully migrated \n`);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema';
import {
createProject,
createSourceFile,
resetActiveProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph';
import {join} from 'path';

import {createAngularJson} from '../../../utils/create-angular-json';

const collectionPath = join(__dirname, '../../../migration.json');

const COMPONENT_BEFORE = `import { TUI_EDITOR_EXTENSIONS, TUI_ATTACH_FILES_LOADER } from '@tinkoff/tui-editor';
@Component({
selector: 'test',
templateUrl: './test.template.html',
styleUrls: ['./index.less'],
providers: [
{
provide: TUI_EDITOR_EXTENSIONS,
useValue: [
import('@tinkoff/tui-editor/extensions/starter-kit').then(
({StarterKit}) => StarterKit,
),
import('@tiptap/extension-text-style').then(({TextStyle}) => TextStyle),
import('@tinkoff/tui-editor/extensions/link').then(
({TuiLink}) => TuiLink,
),
import('@tinkoff/tui-editor/extensions/jump-anchor').then(
({TuiJumpAnchor}) => TuiJumpAnchor,
),
import('@tinkoff/tui-editor/extensions/file-link').then(
({TuiFileLink}) => TuiFileLink,
),
],
},
{
provide: TUI_ATTACH_FILES_LOADER,
deps: [FileIoService],
useFactory: fileLoader,
},
],
})
export class TestComponent {
}`;

const COMPONENT_AFTER = `import { TUI_EDITOR_EXTENSIONS, TUI_ATTACH_FILES_LOADER } from '@tbank/tui-editor';
@Component({
selector: 'test',
templateUrl: './test.template.html',
styleUrls: ['./index.less'],
providers: [
{
provide: TUI_EDITOR_EXTENSIONS,
useValue: [
import('@tbank/tui-editor/extensions/starter-kit').then(
({StarterKit}) => StarterKit,
),
import('@tiptap/extension-text-style').then(({TextStyle}) => TextStyle),
import('@tbank/tui-editor/extensions/link').then(
({TuiLink}) => TuiLink,
),
import('@tbank/tui-editor/extensions/jump-anchor').then(
({TuiJumpAnchor}) => TuiJumpAnchor,
),
import('@tbank/tui-editor/extensions/file-link').then(
({TuiFileLink}) => TuiFileLink,
),
],
},
{
provide: TUI_ATTACH_FILES_LOADER,
deps: [FileIoService],
useFactory: fileLoader,
},
],
})
export class TestComponent {
}`;

const PACKAGE_BEFORE = `{
"dependencies": {
"@angular/core": "~13.0.0",
"@tinkoff/tui-editor": "3.35.0"
}
}`;

const PACKAGE_AFTER = `{
"dependencies": {
"@angular/core": "~13.0.0",
"@tbank/tui-editor": "^1.33.0"
}
}`;

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
runner = new SchematicTestRunner('schematics', collectionPath);

setActiveProject(createProject(host));

createMainFiles();

saveActiveProject();
});

it('should migrate addon-editor', async () => {
const tree = await runner
.runSchematicAsync(
'updateToV3_83',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
)
.toPromise();

expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER);
expect(tree.readContent('package.json')).toEqual(PACKAGE_AFTER);
});

afterEach(() => {
resetActiveProject();
});
});

function createMainFiles(): void {
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE);

createSourceFile('test/app/test.template.html', '<tui-editor></tui-editor>');

createAngularJson();

createSourceFile('package.json', PACKAGE_BEFORE);
}
2 changes: 1 addition & 1 deletion projects/demo/src/modules/icons/icons.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TuiSvgModule,
} from '@taiga-ui/core';
import {TuiCheckboxLabeledModule} from '@taiga-ui/kit';
import {TuiColorSelectorModule} from '@tinkoff/tui-editor';
import {TuiColorSelectorModule} from '@tbank/tui-editor';

import {IconsComponent} from './icons.component';
import {IconsGroupModule} from './icons-group/icons-group.module';
Expand Down

0 comments on commit 9d6bf85

Please sign in to comment.