@@ -2,8 +2,11 @@ import { exec } from "node:child_process";
22import fs from "node:fs" ;
33import path from "node:path" ;
44import { promisify } from "node:util" ;
5- import { injectable } from "inversify" ;
5+ import { inject , injectable } from "inversify" ;
6+ import { MAIN_TOKENS } from "../../di/tokens.js" ;
67import { logger } from "../../lib/logger.js" ;
8+ import { FileWatcherEvent } from "../file-watcher/schemas.js" ;
9+ import type { FileWatcherService } from "../file-watcher/service.js" ;
710import { getChangedFilesForRepo } from "../git.js" ;
811import type { FileEntry } from "./schemas.js" ;
912
@@ -15,10 +18,31 @@ export class FsService {
1518 private static readonly CACHE_TTL = 30000 ;
1619 private cache = new Map < string , { files : FileEntry [ ] ; timestamp : number } > ( ) ;
1720
21+ constructor (
22+ @inject ( MAIN_TOKENS . FileWatcherService )
23+ private fileWatcher : FileWatcherService ,
24+ ) {
25+ this . fileWatcher . on ( FileWatcherEvent . FileChanged , ( { repoPath } ) => {
26+ this . invalidateCache ( repoPath ) ;
27+ } ) ;
28+
29+ this . fileWatcher . on ( FileWatcherEvent . FileDeleted , ( { repoPath } ) => {
30+ this . invalidateCache ( repoPath ) ;
31+ } ) ;
32+
33+ this . fileWatcher . on ( FileWatcherEvent . DirectoryChanged , ( { repoPath } ) => {
34+ this . invalidateCache ( repoPath ) ;
35+ } ) ;
36+
37+ this . fileWatcher . on ( FileWatcherEvent . GitStateChanged , ( { repoPath } ) => {
38+ this . invalidateCache ( repoPath ) ;
39+ } ) ;
40+ }
41+
1842 async listRepoFiles (
1943 repoPath : string ,
2044 query ?: string ,
21- limit = 50 ,
45+ limit ?: number ,
2246 ) : Promise < FileEntry [ ] > {
2347 if ( ! repoPath ) return [ ] ;
2448
@@ -32,20 +56,28 @@ export class FsService {
3256
3357 const cached = this . cache . get ( repoPath ) ;
3458 if ( cached && Date . now ( ) - cached . timestamp < FsService . CACHE_TTL ) {
35- return cached . files . slice ( 0 , limit ) ;
59+ return limit ? cached . files . slice ( 0 , limit ) : cached . files ;
3660 }
3761
3862 const files = await this . gitLsFiles ( repoPath ) ;
3963 const entries = this . toFileEntries ( files , changedFiles ) ;
4064 this . cache . set ( repoPath , { files : entries , timestamp : Date . now ( ) } ) ;
4165
42- return entries . slice ( 0 , limit ) ;
66+ return limit ? entries . slice ( 0 , limit ) : entries ;
4367 } catch ( error ) {
4468 log . error ( "Error listing repo files:" , error ) ;
4569 return [ ] ;
4670 }
4771 }
4872
73+ invalidateCache ( repoPath ?: string ) : void {
74+ if ( repoPath ) {
75+ this . cache . delete ( repoPath ) ;
76+ } else {
77+ this . cache . clear ( ) ;
78+ }
79+ }
80+
4981 async readRepoFile (
5082 repoPath : string ,
5183 filePath : string ,
@@ -73,7 +105,7 @@ export class FsService {
73105 content ,
74106 "utf-8" ,
75107 ) ;
76- this . cache . delete ( repoPath ) ;
108+ this . invalidateCache ( repoPath ) ;
77109 }
78110
79111 private resolvePath ( repoPath : string , filePath : string ) : string {
0 commit comments