88
99import { Rule , SchematicContext , SchematicsException , Tree } from '@angular-devkit/schematics' ;
1010import { join , relative } from 'path' ;
11+ import ts from 'typescript' ;
1112
1213import { canMigrateFile , createMigrationProgram } from '../../utils/typescript/compiler_host' ;
1314
@@ -28,6 +29,12 @@ export function migrate(options: Options): Rule {
2829 const basePath = process . cwd ( ) ;
2930 let pathToMigrate : string | undefined ;
3031 if ( options . path ) {
32+ if ( options . path . startsWith ( '..' ) ) {
33+ throw new SchematicsException (
34+ 'Cannot run control flow migration outside of the current project.' ,
35+ ) ;
36+ }
37+
3138 pathToMigrate = normalizePath ( join ( basePath , options . path ) ) ;
3239 if ( pathToMigrate . trim ( ) !== '' ) {
3340 allPaths . push ( pathToMigrate ) ;
@@ -38,60 +45,45 @@ export function migrate(options: Options): Rule {
3845 }
3946
4047 if ( ! allPaths . length ) {
41- throw new SchematicsException (
42- 'Could not find any tsconfig file. Cannot run the http providers migration.' ,
48+ context . logger . warn (
49+ 'Could not find any tsconfig file. Cannot run the control flow migration.' ,
4350 ) ;
51+ return ;
4452 }
4553
4654 let errors : string [ ] = [ ] ;
55+ let sourceFilesCount = 0 ;
4756
4857 for ( const tsconfigPath of allPaths ) {
49- const migrateErrors = runControlFlowMigration (
50- tree ,
51- tsconfigPath ,
52- basePath ,
53- pathToMigrate ,
54- options ,
55- ) ;
58+ const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
59+ const sourceFiles = program
60+ . getSourceFiles ( )
61+ . filter (
62+ ( sourceFile ) =>
63+ ( pathToMigrate ? sourceFile . fileName . startsWith ( pathToMigrate ) : true ) &&
64+ canMigrateFile ( basePath , sourceFile , program ) ,
65+ ) ;
66+
67+ const migrateErrors = runControlFlowMigration ( tree , sourceFiles , basePath , options ) ;
5668 errors = [ ...errors , ...migrateErrors ] ;
69+ sourceFilesCount += sourceFiles . length ;
5770 }
5871
5972 if ( errors . length > 0 ) {
6073 context . logger . warn ( `WARNING: ${ errors . length } errors occurred during your migration:\n` ) ;
61- errors . forEach ( ( err : string ) => {
62- context . logger . warn ( err ) ;
63- } ) ;
74+ errors . forEach ( ( err ) => context . logger . warn ( err ) ) ;
75+ } else if ( sourceFilesCount === 0 ) {
76+ context . logger . warn ( 'Control flow migration did not find any files to migrate' ) ;
6477 }
6578 } ;
6679}
6780
6881function runControlFlowMigration (
6982 tree : Tree ,
70- tsconfigPath : string ,
83+ sourceFiles : ts . SourceFile [ ] ,
7184 basePath : string ,
72- pathToMigrate ?: string ,
7385 schematicOptions ?: Options ,
7486) : string [ ] {
75- if ( schematicOptions ?. path ?. startsWith ( '..' ) ) {
76- throw new SchematicsException (
77- 'Cannot run control flow migration outside of the current project.' ,
78- ) ;
79- }
80-
81- const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
82- const sourceFiles = program
83- . getSourceFiles ( )
84- . filter (
85- ( sourceFile ) =>
86- ( pathToMigrate ? sourceFile . fileName . startsWith ( pathToMigrate ) : true ) &&
87- canMigrateFile ( basePath , sourceFile , program ) ,
88- ) ;
89-
90- if ( sourceFiles . length === 0 ) {
91- throw new SchematicsException (
92- `Could not find any files to migrate under the path ${ pathToMigrate } . Cannot run the control flow migration.` ,
93- ) ;
94- }
9587 const analysis = new Map < string , AnalyzedFile > ( ) ;
9688 const migrateErrors = new Map < string , MigrateError [ ] > ( ) ;
9789
0 commit comments