@@ -3,10 +3,12 @@ import {
33 SchematicContext ,
44 Tree
55} from '@angular-devkit/schematics' ;
6+ import { Element } from '@angular/compiler' ;
7+ import * as ts from 'typescript' ;
68import { UpdateChanges } from '../common/UpdateChanges' ;
7- import { FileChange , findElementNodes , getAttribute , getSourceOffset , hasAttribute , parseFile } from '../common/util' ;
89import { nativeImport } from '../common/import-helper.js' ;
9- import { Element } from '@angular/compiler' ;
10+ import { namedImportFilter } from '../common/tsUtils' ;
11+ import { FileChange , findElementNodes , getAttribute , getSourceOffset , hasAttribute , parseFile } from '../common/util' ;
1012
1113const version = '17.0.0' ;
1214
@@ -21,15 +23,15 @@ export default (): Rule => async (host: Tree, context: SchematicContext) => {
2123 const applyChanges = ( ) => {
2224 for ( const [ path , change ] of changes . entries ( ) ) {
2325 let buffer = host . read ( path ) . toString ( ) ;
24-
26+
2527 change . sort ( ( c , c1 ) => c . position - c1 . position )
2628 . reverse ( )
2729 . forEach ( c => buffer = c . apply ( buffer ) ) ;
28-
30+
2931 host . overwrite ( path , buffer ) ;
3032 }
3133 } ;
32-
34+
3335 const addChange = ( path : string , change : FileChange ) => {
3436 if ( changes . has ( path ) ) {
3537 changes . get ( path ) . push ( change ) ;
@@ -68,6 +70,58 @@ export default (): Rule => async (host: Tree, context: SchematicContext) => {
6870 } ) ;
6971 }
7072
73+ // Not able to import * as animations from ../../animations due to ESM error - migrations are commonjs, while they should be ESM.
74+ var animationsExports = [ "IAnimationParams" , "AnimationUtil" , "EaseIn" , "EaseInOut" , "EaseOut" , "fadeIn" , "fadeOut" ,
75+ "flipTop" , "flipRight" , "flipBottom" , "flipLeft" , "flipHorFwd" , "flipHorBck" , "flipVerFwd" , "flipVerBck" ,
76+ "rotateInCenter" , "rotateInTop" , "rotateInRight" , "rotateInLeft" , "rotateInBottom" , "rotateInTr" , "rotateInBr" ,
77+ "rotateInBl" , "rotateInTl" , "rotateInDiagonal1" , "rotateInDiagonal2" , "rotateInHor" , "rotateInVer" , "rotateOutCenter" ,
78+ "rotateOutTop" , "rotateOutRight" , "rotateOutLeft" , "rotateOutBottom" , "rotateOutTr" , "rotateOutBr" , "rotateOutBl" ,
79+ "rotateOutTl" , "rotateOutDiagonal1" , "rotateOutDiagonal2" , "rotateOutHor" , "rotateOutVer" ,
80+ "scaleInTop" , "scaleInRight" , "scaleInBottom" , "scaleInLeft" ,
81+ "scaleInCenter" , "scaleInTr" , "scaleInBr" , "scaleInBl" , "scaleInTl" , "scaleInVerTop" , "scaleInVerBottom" ,
82+ "scaleInVerCenter" , "scaleInHorCenter" , "scaleInHorLeft" , "scaleInHorRight" , "scaleOutTop" , "scaleOutRight" ,
83+ "scaleOutBottom" , "scaleOutLeft" , "scaleOutCenter" , "scaleOutTr" , "scaleOutBr" , "scaleOutBl" , "scaleOutTl" ,
84+ "scaleOutVerTop" , "scaleOutVerBottom" , "scaleOutVerCenter" , "scaleOutHorCenter" , "scaleOutHorLeft" , "scaleOutHorRight" ,
85+ "slideInTop" , "slideInRight" , "slideInBottom" , "slideInLeft" , "slideInTr" , "slideInBr" , "slideInBl" , "slideInTl" ,
86+ "slideOutTop" , "slideOutBottom" , "slideOutRight" , "slideOutLeft" , "slideOutTr" , "slideOutBr" , "slideOutBl" , "slideOutTl" ,
87+ "swingInTopFwd" , "swingInRightFwd" , "swingInLeftFwd" , "swingInBottomFwd" , "swingInTopBck" , "swingInRightBck" , "swingInBottomBck" ,
88+ "swingInLeftBck" , "swingOutTopFwd" , "swingOutRightFwd" , "swingOutBottomFwd" , "swingOutLefttFwd" , "swingOutTopBck" , "swingOutRightBck" ,
89+ "swingOutBottomBck" , "swingOutLeftBck" , "growVerIn" , "growVerOut" , "shakeHor" , "shakeVer" , "shakeTop" , "shakeBottom" , "shakeRight" ,
90+ "shakeLeft" , "shakeCenter" , "shakeTr" , "shakeBr" , "shakeBl" , "shakeTl" , "pulsateFwd" , "pulsateBck" , "heartbeat" , "blink" ] ;
91+
92+ update . tsFiles . forEach ( ( filePath : string ) => {
93+ var animationsInFile = [ ] ;
94+ var fileContent = host . read ( filePath ) . toString ( ) ;
95+
96+ const source = ts . createSourceFile ( '' , fileContent , ts . ScriptTarget . Latest , true )
97+ const igImports = source . statements . filter ( < ( a : ts . Statement ) => a is ts . ImportDeclaration > namedImportFilter ) ;
98+
99+ // Find all animations imported from 'igniteui-angular' and delete them.
100+ for ( const igImport of igImports ) {
101+ const start = igImport . getStart ( ) ;
102+ const end = igImport . getEnd ( ) ;
103+ const igImportContent = fileContent . substring ( start , end ) ;
104+ animationsExports . forEach ( anime => {
105+ let match : RegExpExecArray ;
106+ const animeSearchTerm = new RegExp ( `(?<=\\{)\\s*${ anime } [\\s,]|[\\s,]\\s*${ anime } ` , 'g' ) ;
107+
108+ while ( ( match = animeSearchTerm . exec ( igImportContent ) ) !== null ) {
109+ addChange ( filePath , new FileChange ( start + match . index , '' , match [ 0 ] , 'replace' ) ) ;
110+ animationsInFile . push ( anime ) ;
111+ }
112+ } ) ;
113+ }
114+
115+ // Build new import for all the animations from 'igniteui-angular/animations'.
116+ // Add the new import at the end of all imports.
117+ if ( animationsInFile . length > 0 ) {
118+
119+ const lastImport = igImports . reduce ( ( a , b ) => a . getEnd ( ) > b . getEnd ( ) ? a : b ) ;
120+ const newAnimeImport = `\nimport { ${ animationsInFile . sort ( ) . join ( ', ' ) } } from '${ ( lastImport ?. moduleSpecifier as ts . StringLiteral ) . text } /animations';` ;
121+ addChange ( filePath , new FileChange ( lastImport ?. getEnd ( ) , newAnimeImport , '' , 'insert' ) ) ;
122+ }
123+ } ) ;
124+
71125 applyChanges ( ) ;
72126 update . applyChanges ( ) ;
73127} ;
0 commit comments