88
99import type { RequestHandlerExtra } from '@modelcontextprotocol/sdk/shared/protocol' ;
1010import type { ServerNotification , ServerRequest } from '@modelcontextprotocol/sdk/types' ;
11- import { existsSync , statSync } from 'node:fs' ;
12- import { glob } from 'node:fs/promises' ;
11+ import { join } from 'node:path' ;
1312import type { SourceFile } from 'typescript' ;
1413import { z } from 'zod' ;
14+ import type { Host } from '../../host' ;
1515import { declareTool } from '../tool-registry' ;
1616import { analyzeForUnsupportedZoneUses } from './analyze-for-unsupported-zone-uses' ;
1717import { migrateSingleFile } from './migrate-single-file' ;
@@ -61,19 +61,20 @@ most important action to take in the migration journey.
6161 ) ,
6262 } ,
6363 factory :
64- ( ) =>
64+ ( { host } ) =>
6565 ( { fileOrDirPath } , requestHandlerExtra ) =>
66- registerZonelessMigrationTool ( fileOrDirPath , requestHandlerExtra ) ,
66+ registerZonelessMigrationTool ( fileOrDirPath , host , requestHandlerExtra ) ,
6767} ) ;
6868
6969export async function registerZonelessMigrationTool (
7070 fileOrDirPath : string ,
71+ host : Host ,
7172 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
7273) {
7374 let filesWithComponents , componentTestFiles , zoneFiles , categorizationErrors ;
7475 try {
7576 ( { filesWithComponents, componentTestFiles, zoneFiles, categorizationErrors } =
76- await discoverAndCategorizeFiles ( fileOrDirPath , extras ) ) ;
77+ await discoverAndCategorizeFiles ( fileOrDirPath , host , extras ) ) ;
7778 } catch ( e ) {
7879 return createResponse (
7980 `Error: Could not access the specified path. Please ensure the following path is correct ` +
@@ -97,15 +98,15 @@ export async function registerZonelessMigrationTool(
9798 : Array . from ( filesWithComponents ) ;
9899
99100 for ( const file of rankedFiles ) {
100- const result = await migrateSingleFile ( file , extras ) ;
101+ const result = await migrateSingleFile ( file , host , extras ) ;
101102 if ( result !== null ) {
102103 return result ;
103104 }
104105 }
105106 }
106107
107108 for ( const file of componentTestFiles ) {
108- const result = await migrateTestFile ( file ) ;
109+ const result = await migrateTestFile ( file , host ) ;
109110 if ( result !== null ) {
110111 return result ;
111112 }
@@ -124,6 +125,7 @@ export async function registerZonelessMigrationTool(
124125
125126async function discoverAndCategorizeFiles (
126127 fileOrDirPath : string ,
128+ host : Host ,
127129 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
128130) {
129131 const filePaths : string [ ] = [ ] ;
@@ -134,19 +136,20 @@ async function discoverAndCategorizeFiles(
134136
135137 let isDirectory : boolean ;
136138 try {
137- isDirectory = statSync ( fileOrDirPath ) . isDirectory ( ) ;
139+ isDirectory = ( await host . stat ( fileOrDirPath ) ) . isDirectory ( ) ;
138140 } catch ( e ) {
139141 // Re-throw to be handled by the main function as a user input error
140142 throw new Error ( `Failed to access path: ${ fileOrDirPath } ` , { cause : e } ) ;
141143 }
142144
143145 if ( isDirectory ) {
144- for await ( const file of glob ( `${ fileOrDirPath } /**/*.ts` ) ) {
145- filePaths . push ( file ) ;
146+ const files = host . glob ( '**/*.ts' , { cwd : fileOrDirPath } ) ;
147+ for await ( const file of files ) {
148+ filePaths . push ( join ( file . parentPath , file . name ) ) ;
146149 }
147150 } else {
148151 filePaths . push ( fileOrDirPath ) ;
149- const maybeTestFile = await getTestFilePath ( fileOrDirPath ) ;
152+ const maybeTestFile = await getTestFilePath ( fileOrDirPath , host ) ;
150153 if ( maybeTestFile ) {
151154 // Eagerly add the test file path for categorization.
152155 filePaths . push ( maybeTestFile ) ;
@@ -159,8 +162,8 @@ async function discoverAndCategorizeFiles(
159162 const batch = filesToProcess . splice ( 0 , CONCURRENCY_LIMIT ) ;
160163 const results = await Promise . allSettled (
161164 batch . map ( async ( filePath ) => {
162- const sourceFile = await createSourceFile ( filePath ) ;
163- await categorizeFile ( sourceFile , extras , {
165+ const sourceFile = await createSourceFile ( filePath , host ) ;
166+ await categorizeFile ( sourceFile , host , extras , {
164167 filesWithComponents,
165168 componentTestFiles,
166169 zoneFiles,
@@ -183,6 +186,7 @@ async function discoverAndCategorizeFiles(
183186
184187async function categorizeFile (
185188 sourceFile : SourceFile ,
189+ host : Host ,
186190 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
187191 categorizedFiles : {
188192 filesWithComponents : Set < SourceFile > ;
@@ -214,9 +218,9 @@ async function categorizeFile(
214218 ) ;
215219 }
216220
217- const testFilePath = await getTestFilePath ( sourceFile . fileName ) ;
221+ const testFilePath = await getTestFilePath ( sourceFile . fileName , host ) ;
218222 if ( testFilePath ) {
219- componentTestFiles . add ( await createSourceFile ( testFilePath ) ) ;
223+ componentTestFiles . add ( await createSourceFile ( testFilePath , host ) ) ;
220224 }
221225 } else if ( zoneSpecifier ) {
222226 zoneFiles . add ( sourceFile ) ;
@@ -271,9 +275,9 @@ async function rankComponentFilesForMigration(
271275 return componentFiles ; // Fallback to original order if the response fails
272276}
273277
274- async function getTestFilePath ( filePath : string ) : Promise < string | undefined > {
278+ async function getTestFilePath ( filePath : string , host : Host ) : Promise < string | undefined > {
275279 const testFilePath = filePath . replace ( / \. t s $ / , '.spec.ts' ) ;
276- if ( existsSync ( testFilePath ) ) {
280+ if ( host . existsSync ( testFilePath ) ) {
277281 return testFilePath ;
278282 }
279283
0 commit comments