-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(schematics): improve editor migration (#8755)
- Loading branch information
Showing
8 changed files
with
414 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
projects/cdk/schematics/ng-update/v4/steps/migrate-editor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/// <reference lib="es2021" /> | ||
import {type DevkitFileSystem, getPackageJsonDependency} from 'ng-morph'; | ||
import {getSourceFiles, saveActiveProject} from 'ng-morph'; | ||
|
||
import {ALL_TS_FILES} from '../../../constants/file-globs'; | ||
import type {TuiSchema} from '../../../ng-add/schema'; | ||
import { | ||
FINISH_SYMBOL, | ||
infoLog, | ||
REPLACE_SYMBOL, | ||
SMALL_TAB_SYMBOL, | ||
titleLog, | ||
} from '../../../utils/colored-log'; | ||
import {replaceIdentifiers} from '../../steps/replace-identifier'; | ||
import {replacePackageName} from '../../steps/replace-package-name'; | ||
|
||
export const TUI_EDITOR_VERSION = '^4.0.0'; | ||
|
||
export function migrateEditor(fileSystem: DevkitFileSystem, options: TuiSchema): void { | ||
const moduleSpecifier = ['@tinkoff/tui-editor', '@taiga-ui/addon-editor']; | ||
const hasEditor = moduleSpecifier.every( | ||
(pkg) => !getPackageJsonDependency(fileSystem.tree, pkg), | ||
); | ||
|
||
if (hasEditor) { | ||
return; | ||
} | ||
|
||
!options['skip-logs'] && | ||
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} migrating editor...`); | ||
|
||
replaceIdentifiers(options, [ | ||
{ | ||
from: {name: 'TuiEditorModule', moduleSpecifier}, | ||
to: [ | ||
{name: 'TuiEditor', moduleSpecifier: '@taiga-ui/editor'}, | ||
{name: 'TuiEditorSocket', moduleSpecifier: '@taiga-ui/editor'}, | ||
], | ||
}, | ||
{ | ||
from: {name: 'TuiEditorSocketModule', moduleSpecifier}, | ||
to: {name: 'TuiEditorSocket', moduleSpecifier: '@taiga-ui/editor'}, | ||
}, | ||
{ | ||
from: {name: 'TuiToolbarModule', moduleSpecifier}, | ||
to: {name: 'TuiToolbar', moduleSpecifier: '@taiga-ui/editor'}, | ||
}, | ||
{ | ||
from: {name: 'defaultEditorExtensions', moduleSpecifier}, | ||
to: { | ||
name: 'TUI_EDITOR_DEFAULT_EXTENSIONS', | ||
moduleSpecifier: '@taiga-ui/editor', | ||
}, | ||
}, | ||
{ | ||
from: {name: 'TUI_EDITOR_DEFAULT_EXTENSIONS', moduleSpecifier}, | ||
to: { | ||
name: 'TUI_EDITOR_DEFAULT_EXTENSIONS', | ||
moduleSpecifier: '@taiga-ui/editor', | ||
}, | ||
}, | ||
{ | ||
from: [ | ||
{name: 'defaultEditorTools', moduleSpecifier}, | ||
{ | ||
name: 'TUI_EDITOR_DEFAULT_TOOLS', | ||
moduleSpecifier: '@tinkoff/tui-editor', | ||
}, | ||
], | ||
to: {name: 'TUI_EDITOR_DEFAULT_TOOLS', moduleSpecifier: '@taiga-ui/editor'}, | ||
}, | ||
{ | ||
from: {name: 'TuiColorPickerModule', moduleSpecifier}, | ||
to: {name: 'TuiColorPickerModule', moduleSpecifier: '@taiga-ui/legacy'}, | ||
}, | ||
{ | ||
from: {name: 'TuiInputColorModule', moduleSpecifier}, | ||
to: {name: 'TuiInputColorModule', moduleSpecifier: '@taiga-ui/legacy'}, | ||
}, | ||
]); | ||
|
||
getSourceFiles(ALL_TS_FILES).forEach((sourceFile) => | ||
sourceFile.replaceWithText( | ||
sourceFile | ||
.getFullText() | ||
.replaceAll( | ||
/import\(['"`](@tinkoff|@taiga-ui)\/(tui-editor|addon-editor)\/(.*)['"`]\)/g, | ||
"import('@taiga-ui/editor')", | ||
), | ||
), | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
moduleSpecifier.forEach((pkg) => | ||
replacePackageName( | ||
pkg, | ||
{name: '@taiga-ui/editor', version: TUI_EDITOR_VERSION}, | ||
fileSystem.tree, | ||
), | ||
); | ||
|
||
saveActiveProject(); | ||
|
||
!options['skip-logs'] && titleLog(`${FINISH_SYMBOL} successfully migrated \n`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-addon-editor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import {join} from 'node:path'; | ||
|
||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import type {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema'; | ||
import { | ||
createProject, | ||
createSourceFile, | ||
resetActiveProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph'; | ||
|
||
const collectionPath = join(__dirname, '../../../migration.json'); | ||
|
||
const COMPONENT_BEFORE = ` | ||
import { | ||
TUI_SANITIZER, | ||
TuiAlertModule, | ||
TuiDialogModule, | ||
TuiRootModule, | ||
} from '@taiga-ui/core'; | ||
import {NgDompurifySanitizer} from '@tinkoff/ng-dompurify'; | ||
import {TUI_EDITOR_DEFAULT_EXTENSIONS, TUI_EDITOR_EXTENSIONS} from '@taiga-ui/addon-editor'; | ||
import {TuiEditorModule, TuiEditorTool} from '@tinkoff/tui-editor'; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
imports: [TuiEditorModule], | ||
providers: [ | ||
{ | ||
provide: TUI_EDITOR_EXTENSIONS, | ||
deps: [INJECTOR], | ||
useFactory: (injector: Injector) => [ | ||
...TUI_EDITOR_DEFAULT_EXTENSIONS, | ||
import('@taiga-ui/addon-editor/extensions/image-editor').then( | ||
({tuiCreateImageEditorExtension}) => | ||
tuiCreateImageEditorExtension({injector}), | ||
), | ||
], | ||
}, | ||
{ | ||
provide: TUI_SANITIZER, | ||
useClass: NgDompurifySanitizer, | ||
} | ||
], | ||
}) | ||
export class Test { | ||
protected readonly builtInTools = [TuiEditorTool.Undo, TuiEditorTool.Img]; | ||
}`; | ||
|
||
const COMPONENT_AFTER = `import { TUI_SANITIZER } from "@taiga-ui/legacy"; | ||
import { TuiEditor, TuiEditorSocket, TUI_EDITOR_DEFAULT_EXTENSIONS } from "@taiga-ui/editor"; | ||
import { TuiRoot, TuiAlert, TuiDialog } from '@taiga-ui/core'; | ||
import {NgDompurifySanitizer} from '@taiga-ui/dompurify'; | ||
import {TUI_EDITOR_EXTENSIONS} from '@taiga-ui/editor'; | ||
import {TuiEditorTool} from '@taiga-ui/editor'; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
imports: [TuiEditor, TuiEditorSocket], | ||
providers: [ | ||
{ | ||
provide: TUI_EDITOR_EXTENSIONS, | ||
deps: [INJECTOR], | ||
useFactory: (injector: Injector) => [ | ||
...TUI_EDITOR_DEFAULT_EXTENSIONS, | ||
import('@taiga-ui/editor').then( | ||
({tuiCreateImageEditorExtension}) => | ||
tuiCreateImageEditorExtension({injector}), | ||
), | ||
], | ||
}, | ||
{ | ||
provide: TUI_SANITIZER, | ||
useClass: NgDompurifySanitizer, | ||
} | ||
], | ||
}) | ||
export class Test { | ||
protected readonly builtInTools = [TuiEditorTool.Undo, TuiEditorTool.Img]; | ||
} | ||
`.trim(); | ||
|
||
const PACKAGE_JSON_BEFORE = `{ | ||
"dependencies": { | ||
"@angular/core": "~13.0.0", | ||
"@taiga-ui/core": "~3.35.0", | ||
"@taiga-ui/cdk": "~3.35.0", | ||
"@taiga-ui/addon-editor": "~3.35.0", | ||
"@tinkoff/ng-polymorpheus": "1.2.3", | ||
"@tinkoff/ng-event-plugins": "4.5.6" | ||
} | ||
}`; | ||
|
||
const PACKAGE_JSON_AFTER = `{ | ||
"dependencies": { | ||
"@angular/core": "~13.0.0", | ||
"@taiga-ui/core": "~3.35.0", | ||
"@taiga-ui/cdk": "~3.35.0", | ||
"@taiga-ui/editor": "^4.0.0", | ||
"@taiga-ui/event-plugins": "^4.0.1", | ||
"@taiga-ui/legacy": "4.4.1", | ||
"@taiga-ui/polymorpheus": "^4.6.4" | ||
} | ||
}`.trim(); | ||
|
||
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 references in files', async () => { | ||
const tree = await runner.runSchematic( | ||
'updateToV4', | ||
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>, | ||
host, | ||
); | ||
|
||
expect(tree.readContent('package.json').trim()).toEqual(PACKAGE_JSON_AFTER); | ||
expect(tree.readContent('test/app/test.component.ts').trim()).toEqual( | ||
COMPONENT_AFTER, | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
resetActiveProject(); | ||
}); | ||
}); | ||
|
||
function createMainFiles(): void { | ||
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE); | ||
createSourceFile('test/app/test.template.html', ''); | ||
createSourceFile('package.json', PACKAGE_JSON_BEFORE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.