|
| 1 | +import type { |
| 2 | + FileVisitor, |
| 3 | + Rule, |
| 4 | + SchematicContext, |
| 5 | + Tree |
| 6 | +} from '@angular-devkit/schematics'; |
| 7 | +import * as ts from 'typescript'; |
| 8 | +import { IG_PACKAGE_NAME, IG_LICENSED_PACKAGE_NAME, igNamedImportFilter } from '../common/tsUtils'; |
| 9 | + |
| 10 | +const version = '21.1.0'; |
| 11 | + |
| 12 | +// Entry point mapping for moved exports in 21.1.0 |
| 13 | +const ENTRY_POINT_MAP = new Map<string, string>([ |
| 14 | + // IgxGridGroupByAreaComponent moved from grids/core to grids/grid |
| 15 | + ['IgxGridGroupByAreaComponent', 'grids/grid'] |
| 16 | +]); |
| 17 | + |
| 18 | +function migrateImportDeclaration(node: ts.ImportDeclaration, sourceFile: ts.SourceFile): { start: number, end: number, replacement: string } | null { |
| 19 | + if (!igNamedImportFilter(node)) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + const importPath = node.moduleSpecifier.text; |
| 24 | + const namedBindings = node.importClause.namedBindings; |
| 25 | + |
| 26 | + // Only process grids/core imports for this migration |
| 27 | + if (importPath !== `${IG_PACKAGE_NAME}/grids/core` && importPath !== `${IG_LICENSED_PACKAGE_NAME}/grids/core`) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + // Group imports by entry point |
| 32 | + const entryPointGroups = new Map<string, string[]>(); |
| 33 | + const remainingInCore: string[] = []; |
| 34 | + |
| 35 | + for (const element of namedBindings.elements) { |
| 36 | + const name = element.name.text; |
| 37 | + const alias = element.propertyName?.text; |
| 38 | + const importName = alias || name; |
| 39 | + |
| 40 | + const fullImport = alias ? `${importName} as ${name}` : importName; |
| 41 | + |
| 42 | + // Check if this import needs to be moved |
| 43 | + if (ENTRY_POINT_MAP.has(importName)) { |
| 44 | + const targetEntryPoint = ENTRY_POINT_MAP.get(importName)!; |
| 45 | + if (!entryPointGroups.has(targetEntryPoint)) { |
| 46 | + entryPointGroups.set(targetEntryPoint, []); |
| 47 | + } |
| 48 | + entryPointGroups.get(targetEntryPoint)!.push(fullImport); |
| 49 | + } else { |
| 50 | + // Keep in grids/core |
| 51 | + remainingInCore.push(fullImport); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // If nothing changed, return null |
| 56 | + if (entryPointGroups.size === 0) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + // Generate new import statements |
| 61 | + const newImports: string[] = []; |
| 62 | + |
| 63 | + // Add remaining grids/core imports first |
| 64 | + if (remainingInCore.length > 0) { |
| 65 | + const sortedImports = remainingInCore.sort(); |
| 66 | + newImports.push(`import { ${sortedImports.join(', ')} } from '${importPath}';`); |
| 67 | + } |
| 68 | + |
| 69 | + // Add moved imports |
| 70 | + for (const [entryPoint, imports] of entryPointGroups) { |
| 71 | + const sortedImports = imports.sort(); |
| 72 | + const basePackage = importPath.replace('/grids/core', ''); |
| 73 | + newImports.push(`import { ${sortedImports.join(', ')} } from '${basePackage}/${entryPoint}';`); |
| 74 | + } |
| 75 | + |
| 76 | + return { |
| 77 | + start: node.getStart(sourceFile), |
| 78 | + end: node.getEnd(), |
| 79 | + replacement: newImports.join('\n') |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +function migrateFile(filePath: string, content: string): string { |
| 84 | + const sourceFile = ts.createSourceFile( |
| 85 | + filePath, |
| 86 | + content, |
| 87 | + ts.ScriptTarget.Latest, |
| 88 | + true |
| 89 | + ); |
| 90 | + |
| 91 | + const changes: { start: number, end: number, replacement: string }[] = []; |
| 92 | + |
| 93 | + function visit(node: ts.Node) { |
| 94 | + if (ts.isImportDeclaration(node)) { |
| 95 | + const change = migrateImportDeclaration(node, sourceFile); |
| 96 | + if (change) { |
| 97 | + changes.push(change); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + ts.forEachChild(node, visit); |
| 102 | + } |
| 103 | + |
| 104 | + visit(sourceFile); |
| 105 | + |
| 106 | + // Apply changes in reverse order to maintain positions |
| 107 | + changes.sort((a, b) => b.start - a.start); |
| 108 | + |
| 109 | + let result = content; |
| 110 | + for (const change of changes) { |
| 111 | + result = result.substring(0, change.start) + change.replacement + result.substring(change.end); |
| 112 | + } |
| 113 | + |
| 114 | + return result; |
| 115 | +} |
| 116 | + |
| 117 | +export default function migrate(): Rule { |
| 118 | + return async (host: Tree, context: SchematicContext) => { |
| 119 | + context.logger.info(`Applying optional import migration for Ignite UI for Angular to version ${version}`); |
| 120 | + context.logger.info('Migrating imports to new entry points...'); |
| 121 | + |
| 122 | + const visit: FileVisitor = (filePath) => { |
| 123 | + // Only process TypeScript files |
| 124 | + if (!filePath.endsWith('.ts')) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + // Skip node_modules and dist |
| 129 | + if (filePath.includes('node_modules') || filePath.includes('dist')) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + const content = host.read(filePath); |
| 134 | + if (!content) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const originalContent = content.toString(); |
| 139 | + |
| 140 | + // Check if file has grids/core imports |
| 141 | + if (!originalContent.includes(`from '${IG_PACKAGE_NAME}/grids/core'`) && !originalContent.includes(`from "${IG_PACKAGE_NAME}/grids/core"`) && |
| 142 | + !originalContent.includes(`from '${IG_LICENSED_PACKAGE_NAME}/grids/core'`) && !originalContent.includes(`from "${IG_LICENSED_PACKAGE_NAME}/grids/core"`)) { |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + const migratedContent = migrateFile(filePath, originalContent); |
| 147 | + |
| 148 | + if (migratedContent !== originalContent) { |
| 149 | + host.overwrite(filePath, migratedContent); |
| 150 | + context.logger.info(` ✓ Migrated ${filePath}`); |
| 151 | + } |
| 152 | + }; |
| 153 | + |
| 154 | + host.visit(visit); |
| 155 | + |
| 156 | + context.logger.info('Migration complete!'); |
| 157 | + context.logger.info('Breaking changes:'); |
| 158 | + context.logger.info(' - IgxGridGroupByAreaComponent moved from igniteui-angular/grids/core to igniteui-angular/grids/grid'); |
| 159 | + }; |
| 160 | +} |
0 commit comments