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' ;
@@ -49,19 +49,20 @@ change detection (a prerequisite for zoneless applications).
4949 ) ,
5050 } ,
5151 factory :
52- ( ) =>
52+ ( { host } ) =>
5353 ( { fileOrDirPath } , requestHandlerExtra ) =>
54- registerZonelessMigrationTool ( fileOrDirPath , requestHandlerExtra ) ,
54+ registerZonelessMigrationTool ( fileOrDirPath , host , requestHandlerExtra ) ,
5555} ) ;
5656
5757export async function registerZonelessMigrationTool (
5858 fileOrDirPath : string ,
59+ host : Host ,
5960 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
6061) {
6162 let filesWithComponents , componentTestFiles , zoneFiles , categorizationErrors ;
6263 try {
6364 ( { filesWithComponents, componentTestFiles, zoneFiles, categorizationErrors } =
64- await discoverAndCategorizeFiles ( fileOrDirPath , extras ) ) ;
65+ await discoverAndCategorizeFiles ( fileOrDirPath , host , extras ) ) ;
6566 } catch ( e ) {
6667 return createResponse (
6768 `Error: Could not access the specified path. Please ensure the following path is correct ` +
@@ -85,15 +86,15 @@ export async function registerZonelessMigrationTool(
8586 : Array . from ( filesWithComponents ) ;
8687
8788 for ( const file of rankedFiles ) {
88- const result = await migrateSingleFile ( file , extras ) ;
89+ const result = await migrateSingleFile ( file , host , extras ) ;
8990 if ( result !== null ) {
9091 return result ;
9192 }
9293 }
9394 }
9495
9596 for ( const file of componentTestFiles ) {
96- const result = await migrateTestFile ( file ) ;
97+ const result = await migrateTestFile ( file , host ) ;
9798 if ( result !== null ) {
9899 return result ;
99100 }
@@ -112,6 +113,7 @@ export async function registerZonelessMigrationTool(
112113
113114async function discoverAndCategorizeFiles (
114115 fileOrDirPath : string ,
116+ host : Host ,
115117 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
116118) {
117119 const filePaths : string [ ] = [ ] ;
@@ -122,19 +124,20 @@ async function discoverAndCategorizeFiles(
122124
123125 let isDirectory : boolean ;
124126 try {
125- isDirectory = statSync ( fileOrDirPath ) . isDirectory ( ) ;
127+ isDirectory = ( await host . stat ( fileOrDirPath ) ) . isDirectory ( ) ;
126128 } catch ( e ) {
127129 // Re-throw to be handled by the main function as a user input error
128130 throw new Error ( `Failed to access path: ${ fileOrDirPath } ` , { cause : e } ) ;
129131 }
130132
131133 if ( isDirectory ) {
132- for await ( const file of glob ( `${ fileOrDirPath } /**/*.ts` ) ) {
133- filePaths . push ( file ) ;
134+ const files = host . glob ( '**/*.ts' , { cwd : fileOrDirPath } ) ;
135+ for await ( const file of files ) {
136+ filePaths . push ( join ( file . parentPath , file . name ) ) ;
134137 }
135138 } else {
136139 filePaths . push ( fileOrDirPath ) ;
137- const maybeTestFile = await getTestFilePath ( fileOrDirPath ) ;
140+ const maybeTestFile = await getTestFilePath ( fileOrDirPath , host ) ;
138141 if ( maybeTestFile ) {
139142 // Eagerly add the test file path for categorization.
140143 filePaths . push ( maybeTestFile ) ;
@@ -147,8 +150,8 @@ async function discoverAndCategorizeFiles(
147150 const batch = filesToProcess . splice ( 0 , CONCURRENCY_LIMIT ) ;
148151 const results = await Promise . allSettled (
149152 batch . map ( async ( filePath ) => {
150- const sourceFile = await createSourceFile ( filePath ) ;
151- await categorizeFile ( sourceFile , extras , {
153+ const sourceFile = await createSourceFile ( filePath , host ) ;
154+ await categorizeFile ( sourceFile , host , extras , {
152155 filesWithComponents,
153156 componentTestFiles,
154157 zoneFiles,
@@ -171,6 +174,7 @@ async function discoverAndCategorizeFiles(
171174
172175async function categorizeFile (
173176 sourceFile : SourceFile ,
177+ host : Host ,
174178 extras : RequestHandlerExtra < ServerRequest , ServerNotification > ,
175179 categorizedFiles : {
176180 filesWithComponents : Set < SourceFile > ;
@@ -202,9 +206,9 @@ async function categorizeFile(
202206 ) ;
203207 }
204208
205- const testFilePath = await getTestFilePath ( sourceFile . fileName ) ;
209+ const testFilePath = await getTestFilePath ( sourceFile . fileName , host ) ;
206210 if ( testFilePath ) {
207- componentTestFiles . add ( await createSourceFile ( testFilePath ) ) ;
211+ componentTestFiles . add ( await createSourceFile ( testFilePath , host ) ) ;
208212 }
209213 } else if ( zoneSpecifier ) {
210214 zoneFiles . add ( sourceFile ) ;
@@ -259,9 +263,9 @@ async function rankComponentFilesForMigration(
259263 return componentFiles ; // Fallback to original order if the response fails
260264}
261265
262- async function getTestFilePath ( filePath : string ) : Promise < string | undefined > {
266+ async function getTestFilePath ( filePath : string , host : Host ) : Promise < string | undefined > {
263267 const testFilePath = filePath . replace ( / \. t s $ / , '.spec.ts' ) ;
264- if ( existsSync ( testFilePath ) ) {
268+ if ( host . existsSync ( testFilePath ) ) {
265269 return testFilePath ;
266270 }
267271
0 commit comments