66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9- import { Rule , SchematicsException , Tree } from '@angular-devkit/schematics' ;
9+ import { Rule , SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics' ;
1010import { join , relative } from 'path' ;
11+ import ts from 'typescript' ;
1112
1213import { normalizePath } from '../../utils/change_tracker' ;
1314import { canMigrateFile , createMigrationProgram } from '../../utils/typescript/compiler_host' ;
@@ -21,49 +22,54 @@ interface Options extends MigrationOptions {
2122}
2223
2324export function migrate ( options : Options ) : Rule {
24- return async ( tree : Tree ) => {
25- const { buildPaths, testPaths} = await getProjectTsConfigPaths ( tree ) ;
25+ return async ( tree : Tree , context : SchematicContext ) => {
2626 const basePath = process . cwd ( ) ;
27+ let pathToMigrate : string | undefined ;
28+ if ( options . path ) {
29+ if ( options . path . startsWith ( '..' ) ) {
30+ throw new SchematicsException (
31+ 'Cannot run inject migration outside of the current project.' ,
32+ ) ;
33+ }
34+ pathToMigrate = normalizePath ( join ( basePath , options . path ) ) ;
35+ }
36+
37+ const { buildPaths, testPaths} = await getProjectTsConfigPaths ( tree ) ;
2738 const allPaths = [ ...buildPaths , ...testPaths ] ;
28- const pathToMigrate = normalizePath ( join ( basePath , options . path ) ) ;
2939
3040 if ( ! allPaths . length ) {
31- throw new SchematicsException (
32- 'Could not find any tsconfig file. Cannot run the inject migration.' ,
33- ) ;
41+ context . logger . warn ( 'Could not find any tsconfig file. Cannot run the inject migration.' ) ;
42+ return ;
3443 }
3544
45+ let sourceFilesCount = 0 ;
46+
3647 for ( const tsconfigPath of allPaths ) {
37- runInjectMigration ( tree , tsconfigPath , basePath , pathToMigrate , options ) ;
48+ const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
49+ const sourceFiles = program
50+ . getSourceFiles ( )
51+ . filter (
52+ ( sourceFile ) =>
53+ ( pathToMigrate ? sourceFile . fileName . startsWith ( pathToMigrate ) : true ) &&
54+ canMigrateFile ( basePath , sourceFile , program ) ,
55+ ) ;
56+
57+ sourceFilesCount += runInjectMigration ( tree , sourceFiles , basePath , options ) ;
58+ }
59+
60+ if ( sourceFilesCount === 0 ) {
61+ context . logger . warn ( 'Inject migration did not find any files to migrate' ) ;
3862 }
3963 } ;
4064}
4165
4266function runInjectMigration (
4367 tree : Tree ,
44- tsconfigPath : string ,
68+ sourceFiles : ts . SourceFile [ ] ,
4569 basePath : string ,
46- pathToMigrate : string ,
4770 schematicOptions : Options ,
48- ) : void {
49- if ( schematicOptions . path . startsWith ( '..' ) ) {
50- throw new SchematicsException ( 'Cannot run inject migration outside of the current project.' ) ;
51- }
52-
53- const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
54- const sourceFiles = program
55- . getSourceFiles ( )
56- . filter (
57- ( sourceFile ) =>
58- sourceFile . fileName . startsWith ( pathToMigrate ) &&
59- canMigrateFile ( basePath , sourceFile , program ) ,
60- ) ;
61-
62- if ( sourceFiles . length === 0 ) {
63- throw new SchematicsException (
64- `Could not find any files to migrate under the path ${ pathToMigrate } . Cannot run the inject migration.` ,
65- ) ;
66- }
71+ ) : number {
72+ let migratedFiles = 0 ;
6773
6874 for ( const sourceFile of sourceFiles ) {
6975 const changes = migrateFile ( sourceFile , schematicOptions ) ;
@@ -79,6 +85,8 @@ function runInjectMigration(
7985 }
8086
8187 tree . commitUpdate ( update ) ;
88+ migratedFiles ++ ;
8289 }
8390 }
91+ return migratedFiles ;
8492}
0 commit comments