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
@@ -29,6 +30,12 @@ export function migrate(options: Options): Rule {
2930 const basePath = process . cwd ( ) ;
3031 let pathToMigrate : string | undefined ;
3132 if ( options . path ) {
33+ if ( options . path . startsWith ( '..' ) ) {
34+ throw new SchematicsException (
35+ 'Cannot run control flow migration outside of the current project.' ,
36+ ) ;
37+ }
38+
3239 pathToMigrate = normalizePath ( join ( basePath , options . path ) ) ;
3340 if ( pathToMigrate . trim ( ) !== '' ) {
3441 allPaths . push ( pathToMigrate ) ;
@@ -39,60 +46,45 @@ export function migrate(options: Options): Rule {
3946 }
4047
4148 if ( ! allPaths . length ) {
42- throw new SchematicsException (
43- 'Could not find any tsconfig file. Cannot run the http providers migration.' ,
49+ context . logger . warn (
50+ 'Could not find any tsconfig file. Cannot run the control flow migration.' ,
4451 ) ;
52+ return ;
4553 }
4654
4755 let errors : string [ ] = [ ] ;
56+ let sourceFilesCount = 0 ;
4857
4958 for ( const tsconfigPath of allPaths ) {
50- const migrateErrors = runControlFlowMigration (
51- tree ,
52- tsconfigPath ,
53- basePath ,
54- pathToMigrate ,
55- options ,
56- ) ;
59+ const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
60+ const sourceFiles = program
61+ . getSourceFiles ( )
62+ . filter (
63+ ( sourceFile ) =>
64+ ( pathToMigrate ? sourceFile . fileName . startsWith ( pathToMigrate ) : true ) &&
65+ canMigrateFile ( basePath , sourceFile , program ) ,
66+ ) ;
67+
68+ const migrateErrors = runControlFlowMigration ( tree , sourceFiles , basePath , options ) ;
5769 errors = [ ...errors , ...migrateErrors ] ;
70+ sourceFilesCount += sourceFiles . length ;
5871 }
5972
6073 if ( errors . length > 0 ) {
6174 context . logger . warn ( `WARNING: ${ errors . length } errors occurred during your migration:\n` ) ;
62- errors . forEach ( ( err : string ) => {
63- context . logger . warn ( err ) ;
64- } ) ;
75+ errors . forEach ( ( err ) => context . logger . warn ( err ) ) ;
76+ } else if ( sourceFilesCount === 0 ) {
77+ context . logger . warn ( 'Control flow migration did not find any files to migrate' ) ;
6578 }
6679 } ;
6780}
6881
6982function runControlFlowMigration (
7083 tree : Tree ,
71- tsconfigPath : string ,
84+ sourceFiles : ts . SourceFile [ ] ,
7285 basePath : string ,
73- pathToMigrate ?: string ,
7486 schematicOptions ?: Options ,
7587) : string [ ] {
76- if ( schematicOptions ?. path ?. startsWith ( '..' ) ) {
77- throw new SchematicsException (
78- 'Cannot run control flow migration outside of the current project.' ,
79- ) ;
80- }
81-
82- const program = createMigrationProgram ( tree , tsconfigPath , basePath ) ;
83- const sourceFiles = program
84- . getSourceFiles ( )
85- . filter (
86- ( sourceFile ) =>
87- ( pathToMigrate ? sourceFile . fileName . startsWith ( pathToMigrate ) : true ) &&
88- canMigrateFile ( basePath , sourceFile , program ) ,
89- ) ;
90-
91- if ( sourceFiles . length === 0 ) {
92- throw new SchematicsException (
93- `Could not find any files to migrate under the path ${ pathToMigrate } . Cannot run the control flow migration.` ,
94- ) ;
95- }
9688 const analysis = new Map < string , AnalyzedFile > ( ) ;
9789 const migrateErrors = new Map < string , MigrateError [ ] > ( ) ;
9890
0 commit comments