1+ import { dirname } from 'node:path'
2+
3+ import * as vscode from 'vscode'
4+
15import { log } from '../utils/logger.js'
26import { normalizePath } from '../utils/normalize.js'
3- import { FileWatcherManager } from '../utils/watcher.js'
7+ import { FileWatcherManager , type WatchPattern } from '../utils/watcher.js'
48
5- import type * as vscode from 'vscode'
69import type { RepositoryManager } from './manager.js'
7- import type { ExtensionConfigManager } from '../config/index.js'
810
911export class TestfileWatcher extends FileWatcherManager {
1012 constructor (
11- public readonly configManager : ExtensionConfigManager ,
1213 private readonly repositoryManager : RepositoryManager
1314 ) {
1415 super ( )
1516 }
1617
1718 public enable ( ) {
1819 this . createWatchers ( )
19- this . configManager . on ( 'update:testFilePattern' , ( ) => this . refreshWatchers ( ) )
2020 }
2121
22- protected getFilePatterns ( ) : string [ ] {
23- return this . configManager . globalConfig . testFilePattern || [ ]
22+ protected getFilePatterns ( ) : WatchPattern [ ] {
23+ return this . repositoryManager . repos . reduce ( ( patterns , repo ) => {
24+ const configDirPath = dirname ( repo . wdioConfigPath )
25+ for ( const pattern of repo . specPatterns ) {
26+ patterns . push ( {
27+ base : vscode . Uri . file ( configDirPath ) ,
28+ pattern
29+ } )
30+ }
31+ return patterns
32+ } , [ ] as WatchPattern [ ] )
2433 }
34+
2535 protected async handleFileCreate ( uri : vscode . Uri ) : Promise < void > {
2636 await this . _handleFileCreateAndChange ( uri , true )
2737 }
38+
2839 protected async handleFileChange ( uri : vscode . Uri ) : Promise < void > {
2940 await this . _handleFileCreateAndChange ( uri , false )
3041 }
@@ -38,12 +49,15 @@ export class TestfileWatcher extends FileWatcherManager {
3849
3950 // If a Spec file is newly created, attempt to read in all repositories,
4051 // as it is unclear which configuration file should be reflected.
41- const repos = isCreated
42- ? this . repositoryManager . repos
43- : this . repositoryManager . repos . filter ( ( repo ) => repo . getSpecByFilePath ( uri . fsPath ) )
44-
45- log . debug ( `Affected repository are ${ repos . length } repositories` )
46- await Promise . all ( repos . map ( async ( repo ) => await repo . reloadSpecFiles ( [ normalizePath ( specFilePath ) ] ) ) )
52+ const promises = this . repositoryManager . repos . reduce ( ( repos , repo ) => {
53+ if ( ! isCreated && ! repo . getSpecByFilePath ( specFilePath ) ) {
54+ return repos
55+ }
56+ repos . push ( repo . reloadSpecFiles ( [ normalizePath ( specFilePath ) ] ) )
57+ return repos
58+ } , [ ] as Promise < void > [ ] )
59+ log . debug ( `Affected repository are ${ promises . length } repositories` )
60+ await Promise . all ( promises )
4761 }
4862
4963 /**
@@ -52,12 +66,13 @@ export class TestfileWatcher extends FileWatcherManager {
5266 protected async handleFileDelete ( uri : vscode . Uri ) : Promise < void > {
5367 const specFilePath = uri . fsPath
5468 log . debug ( `Test file deleted: ${ specFilePath } ` )
55-
56- const repos = this . repositoryManager . repos . filter ( ( repo ) => repo . getSpecByFilePath ( uri . fsPath ) )
57- log . debug ( `Affected repository are ${ repos . length } repositories` )
58-
59- repos . map ( async ( repo ) => {
60- repo . removeSpecFile ( normalizePath ( specFilePath ) )
61- } )
69+ const count = this . repositoryManager . repos . reduce ( ( counter , repo ) => {
70+ if ( ! repo . getSpecByFilePath ( specFilePath ) ) {
71+ return counter
72+ }
73+ repo . removeSpecFile ( specFilePath )
74+ return ++ counter
75+ } , 0 )
76+ log . debug ( `Affected repository are ${ count } repositories` )
6277 }
6378}
0 commit comments