|
| 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} from '@angular-devkit/schematics'; |
| 10 | +import {NgStyleMigration} from './ngstyle-to-style-migration'; |
| 11 | +import {MigrationStage, runMigrationInDevkit} from '../../utils/tsurge/helpers/angular_devkit'; |
| 12 | + |
| 13 | +interface Options { |
| 14 | + path: string; |
| 15 | + analysisDir: string; |
| 16 | + bestEffortMode?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export function migrate(options: Options): Rule { |
| 20 | + return async (tree, context) => { |
| 21 | + await runMigrationInDevkit({ |
| 22 | + tree, |
| 23 | + getMigration: (fs) => |
| 24 | + new NgStyleMigration({ |
| 25 | + bestEffortMode: options.bestEffortMode, |
| 26 | + shouldMigrate: (file) => { |
| 27 | + return ( |
| 28 | + file.rootRelativePath.startsWith(fs.normalize(options.path)) && |
| 29 | + !/(^|\/)node_modules\//.test(file.rootRelativePath) |
| 30 | + ); |
| 31 | + }, |
| 32 | + }), |
| 33 | + beforeProgramCreation: (tsconfigPath: string, stage: MigrationStage) => { |
| 34 | + if (stage === MigrationStage.Analysis) { |
| 35 | + context.logger.info(`Preparing analysis for: ${tsconfigPath}...`); |
| 36 | + } else { |
| 37 | + context.logger.info(`Running migration for: ${tsconfigPath}...`); |
| 38 | + } |
| 39 | + }, |
| 40 | + beforeUnitAnalysis: (tsconfigPath: string) => { |
| 41 | + context.logger.info(`Scanning for component tags: ${tsconfigPath}...`); |
| 42 | + }, |
| 43 | + afterAllAnalyzed: () => { |
| 44 | + context.logger.info(``); |
| 45 | + context.logger.info(`Processing analysis data between targets...`); |
| 46 | + context.logger.info(``); |
| 47 | + }, |
| 48 | + afterAnalysisFailure: () => { |
| 49 | + context.logger.error('Migration failed unexpectedly with no analysis data'); |
| 50 | + }, |
| 51 | + whenDone: ({ |
| 52 | + touchedFilesCount, |
| 53 | + replacementCount, |
| 54 | + }: { |
| 55 | + touchedFilesCount: number; |
| 56 | + replacementCount: number; |
| 57 | + }) => { |
| 58 | + context.logger.info(''); |
| 59 | + context.logger.info(`Successfully migrated to style bindings from ngStyle 🎉`); |
| 60 | + context.logger.info( |
| 61 | + ` -> Migrated ${replacementCount} ngStyle to style bindings in ${touchedFilesCount} files.`, |
| 62 | + ); |
| 63 | + }, |
| 64 | + }); |
| 65 | + }; |
| 66 | +} |
0 commit comments