@@ -16,7 +16,7 @@ import { declareTool } from '../tool-registry';
1616import { analyzeForUnsupportedZoneUses } from './analyze_for_unsupported_zone_uses' ;
1717import { migrateSingleFile } from './migrate_single_file' ;
1818import { migrateTestFile } from './migrate_test_file' ;
19- import { createTestDebuggingGuideForNonActionableInput } from './prompts' ;
19+ import { createResponse , createTestDebuggingGuideForNonActionableInput } from './prompts' ;
2020import { sendDebugMessage } from './send_debug_message' ;
2121import { createSourceFile , getImportSpecifier } from './ts_utils' ;
2222
@@ -65,16 +65,75 @@ most important action to take in the migration journey.
6565 ( { fileOrDirPath } , requestHandlerExtra ) =>
6666 registerZonelessMigrationTool ( fileOrDirPath , requestHandlerExtra ) ,
6767} ) ;
68+
6869export async function registerZonelessMigrationTool (
6970 fileOrDirPath : string ,
7071 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
72+ ) {
73+ let filesWithComponents , componentTestFiles , zoneFiles ;
74+ try {
75+ ( { filesWithComponents, componentTestFiles, zoneFiles } = await discoverAndCategorizeFiles (
76+ fileOrDirPath ,
77+ extras ,
78+ ) ) ;
79+ } catch ( e ) {
80+ return createResponse (
81+ `Error: Could not access the specified path. Please ensure the following path is correct ` +
82+ `and that you have the necessary permissions:\n${ fileOrDirPath } ` ,
83+ ) ;
84+ }
85+
86+ if ( zoneFiles . size > 0 ) {
87+ for ( const file of zoneFiles ) {
88+ const result = await analyzeForUnsupportedZoneUses ( file ) ;
89+ if ( result !== null ) {
90+ return result ;
91+ }
92+ }
93+ }
94+
95+ if ( filesWithComponents . size > 0 ) {
96+ const rankedFiles =
97+ filesWithComponents . size > 1
98+ ? await rankComponentFilesForMigration ( extras , Array . from ( filesWithComponents ) )
99+ : Array . from ( filesWithComponents ) ;
100+
101+ for ( const file of rankedFiles ) {
102+ const result = await migrateSingleFile ( file , extras ) ;
103+ if ( result !== null ) {
104+ return result ;
105+ }
106+ }
107+ }
108+
109+ for ( const file of componentTestFiles ) {
110+ const result = await migrateTestFile ( file ) ;
111+ if ( result !== null ) {
112+ return result ;
113+ }
114+ }
115+
116+ return createTestDebuggingGuideForNonActionableInput ( fileOrDirPath ) ;
117+ }
118+
119+ async function discoverAndCategorizeFiles (
120+ fileOrDirPath : string ,
121+ extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
71122) {
72123 let files : SourceFile [ ] = [ ] ;
73124 const componentTestFiles = new Set < SourceFile > ( ) ;
74125 const filesWithComponents = new Set < SourceFile > ( ) ;
75126 const zoneFiles = new Set < SourceFile > ( ) ;
76127
77- if ( fs . statSync ( fileOrDirPath ) . isDirectory ( ) ) {
128+ let isDirectory : boolean ;
129+ try {
130+ isDirectory = fs . statSync ( fileOrDirPath ) . isDirectory ( ) ;
131+ } catch ( e ) {
132+ // Re-throw to be handled by the main function as a user input error
133+ throw new Error ( `Failed to access path: ${ fileOrDirPath } ` ) ;
134+ }
135+
136+ if ( isDirectory ) {
78137 const allFiles = glob ( `${ fileOrDirPath } /**/*.ts` ) ;
79138 for await ( const file of allFiles ) {
80139 files . push ( await createSourceFile ( file ) ) ;
@@ -120,37 +179,7 @@ export async function registerZonelessMigrationTool(
120179 }
121180 }
122181
123- if ( zoneFiles . size > 0 ) {
124- for ( const file of zoneFiles ) {
125- const result = await analyzeForUnsupportedZoneUses ( file ) ;
126- if ( result !== null ) {
127- return result ;
128- }
129- }
130- }
131-
132- if ( filesWithComponents . size > 0 ) {
133- const rankedFiles =
134- filesWithComponents . size > 1
135- ? await rankComponentFilesForMigration ( extras , Array . from ( filesWithComponents ) )
136- : Array . from ( filesWithComponents ) ;
137-
138- for ( const file of rankedFiles ) {
139- const result = await migrateSingleFile ( file , extras ) ;
140- if ( result !== null ) {
141- return result ;
142- }
143- }
144- }
145-
146- for ( const file of componentTestFiles ) {
147- const result = await migrateTestFile ( file ) ;
148- if ( result !== null ) {
149- return result ;
150- }
151- }
152-
153- return createTestDebuggingGuideForNonActionableInput ( fileOrDirPath ) ;
182+ return { filesWithComponents, componentTestFiles, zoneFiles } ;
154183}
155184
156185async function rankComponentFilesForMigration (
0 commit comments