@@ -16,7 +16,7 @@ import { declareTool } from '../tool-registry';
16
16
import { analyzeForUnsupportedZoneUses } from './analyze_for_unsupported_zone_uses' ;
17
17
import { migrateSingleFile } from './migrate_single_file' ;
18
18
import { migrateTestFile } from './migrate_test_file' ;
19
- import { createTestDebuggingGuideForNonActionableInput } from './prompts' ;
19
+ import { createResponse , createTestDebuggingGuideForNonActionableInput } from './prompts' ;
20
20
import { sendDebugMessage } from './send_debug_message' ;
21
21
import { createSourceFile , getImportSpecifier } from './ts_utils' ;
22
22
@@ -65,16 +65,75 @@ most important action to take in the migration journey.
65
65
( { fileOrDirPath } , requestHandlerExtra ) =>
66
66
registerZonelessMigrationTool ( fileOrDirPath , requestHandlerExtra ) ,
67
67
} ) ;
68
+
68
69
export async function registerZonelessMigrationTool (
69
70
fileOrDirPath : string ,
70
71
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 > ,
71
122
) {
72
123
let files : SourceFile [ ] = [ ] ;
73
124
const componentTestFiles = new Set < SourceFile > ( ) ;
74
125
const filesWithComponents = new Set < SourceFile > ( ) ;
75
126
const zoneFiles = new Set < SourceFile > ( ) ;
76
127
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 ) {
78
137
const allFiles = glob ( `${ fileOrDirPath } /**/*.ts` ) ;
79
138
for await ( const file of allFiles ) {
80
139
files . push ( await createSourceFile ( file ) ) ;
@@ -120,37 +179,7 @@ export async function registerZonelessMigrationTool(
120
179
}
121
180
}
122
181
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 } ;
154
183
}
155
184
156
185
async function rankComponentFilesForMigration (
0 commit comments