|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Rule, SchematicsException, Tree} from '@angular-devkit/schematics'; |
| 10 | +import {existsSync, statSync} from 'fs'; |
| 11 | +import {join, relative} from 'path'; |
| 12 | + |
| 13 | +import {normalizePath} from '../../utils/change_tracker'; |
| 14 | +import {getProjectTsConfigPaths} from '../../utils/project_tsconfig_paths'; |
| 15 | +import {canMigrateFile, createMigrationProgram} from '../../utils/typescript/compiler_host'; |
| 16 | + |
| 17 | +import {RouteMigrationData, migrateFileToLazyRoutes} from './to-lazy-routes'; |
| 18 | + |
| 19 | +interface Options { |
| 20 | + path: string; |
| 21 | +} |
| 22 | + |
| 23 | +export default function (options: Options): Rule { |
| 24 | + return async (tree, context) => { |
| 25 | + const {buildPaths, testPaths} = await getProjectTsConfigPaths(tree); |
| 26 | + const basePath = process.cwd(); |
| 27 | + const allPaths = [...buildPaths, ...testPaths]; |
| 28 | + // TS and Schematic use paths in POSIX format even on Windows. This is needed as otherwise |
| 29 | + // string matching such as `sourceFile.fileName.startsWith(pathToMigrate)` might not work. |
| 30 | + const pathToMigrate = normalizePath(join(basePath, options.path)); |
| 31 | + |
| 32 | + if (!allPaths.length) { |
| 33 | + throw new SchematicsException( |
| 34 | + 'Could not find any tsconfig file. Cannot run the route lazy loading migration.', |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + let migratedRoutes: RouteMigrationData[] = []; |
| 39 | + let skippedRoutes: RouteMigrationData[] = []; |
| 40 | + |
| 41 | + for (const tsconfigPath of allPaths) { |
| 42 | + const {migratedRoutes: migrated, skippedRoutes: skipped} = standaloneRoutesMigration( |
| 43 | + tree, |
| 44 | + tsconfigPath, |
| 45 | + basePath, |
| 46 | + pathToMigrate, |
| 47 | + options, |
| 48 | + ); |
| 49 | + |
| 50 | + migratedRoutes.push(...migrated); |
| 51 | + skippedRoutes.push(...skipped); |
| 52 | + } |
| 53 | + |
| 54 | + if (migratedRoutes.length === 0 && skippedRoutes.length === 0) { |
| 55 | + throw new SchematicsException( |
| 56 | + `Could not find any files to migrate under the path ${pathToMigrate}.`, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + context.logger.info('🎉 Automated migration step has finished! 🎉'); |
| 61 | + |
| 62 | + context.logger.info(`Number of updated routes: ${migratedRoutes.length}`); |
| 63 | + context.logger.info(`Number of skipped routes: ${skippedRoutes.length}`); |
| 64 | + |
| 65 | + if (skippedRoutes.length > 0) { |
| 66 | + context.logger.info( |
| 67 | + `Note: this migration was unable to optimize the following routes, since they use components declared in NgModules:`, |
| 68 | + ); |
| 69 | + |
| 70 | + for (const route of skippedRoutes) { |
| 71 | + context.logger.info(`- \`${route.path}\` path at \`${route.file}\``); |
| 72 | + } |
| 73 | + |
| 74 | + context.logger.info( |
| 75 | + `Consider making those components standalone and run this migration again. More information about standalone migration can be found at https://angular.dev/reference/migrations/standalone`, |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + context.logger.info( |
| 80 | + 'IMPORTANT! Please verify manually that your application builds and behaves as expected.', |
| 81 | + ); |
| 82 | + context.logger.info( |
| 83 | + `See https://angular.dev/reference/migrations/route-lazy-loading for more information.`, |
| 84 | + ); |
| 85 | + }; |
| 86 | +} |
| 87 | + |
| 88 | +function standaloneRoutesMigration( |
| 89 | + tree: Tree, |
| 90 | + tsconfigPath: string, |
| 91 | + basePath: string, |
| 92 | + pathToMigrate: string, |
| 93 | + schematicOptions: Options, |
| 94 | +): {migratedRoutes: RouteMigrationData[]; skippedRoutes: RouteMigrationData[]} { |
| 95 | + if (schematicOptions.path.startsWith('..')) { |
| 96 | + throw new SchematicsException( |
| 97 | + 'Cannot run route lazy loading migration outside of the current project.', |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + if (existsSync(pathToMigrate) && !statSync(pathToMigrate).isDirectory()) { |
| 102 | + throw new SchematicsException( |
| 103 | + `Migration path ${pathToMigrate} has to be a directory. Cannot run the route lazy loading migration.`, |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + const program = createMigrationProgram(tree, tsconfigPath, basePath); |
| 108 | + const sourceFiles = program |
| 109 | + .getSourceFiles() |
| 110 | + .filter( |
| 111 | + (sourceFile) => |
| 112 | + sourceFile.fileName.startsWith(pathToMigrate) && |
| 113 | + canMigrateFile(basePath, sourceFile, program), |
| 114 | + ); |
| 115 | + |
| 116 | + const migratedRoutes: RouteMigrationData[] = []; |
| 117 | + const skippedRoutes: RouteMigrationData[] = []; |
| 118 | + |
| 119 | + if (sourceFiles.length === 0) { |
| 120 | + return {migratedRoutes, skippedRoutes}; |
| 121 | + } |
| 122 | + |
| 123 | + for (const sourceFile of sourceFiles) { |
| 124 | + const { |
| 125 | + pendingChanges, |
| 126 | + skippedRoutes: skipped, |
| 127 | + migratedRoutes: migrated, |
| 128 | + } = migrateFileToLazyRoutes(sourceFile, program); |
| 129 | + |
| 130 | + skippedRoutes.push(...skipped); |
| 131 | + migratedRoutes.push(...migrated); |
| 132 | + |
| 133 | + const update = tree.beginUpdate(relative(basePath, sourceFile.fileName)); |
| 134 | + |
| 135 | + pendingChanges.forEach((change) => { |
| 136 | + if (change.removeLength != null) { |
| 137 | + update.remove(change.start, change.removeLength); |
| 138 | + } |
| 139 | + update.insertRight(change.start, change.text); |
| 140 | + }); |
| 141 | + |
| 142 | + tree.commitUpdate(update); |
| 143 | + } |
| 144 | + |
| 145 | + return {migratedRoutes, skippedRoutes}; |
| 146 | +} |
0 commit comments