1
1
import type { CommandsSchema , Files } from '@tutorialkit/types' ;
2
2
import type { IFSWatcher , WebContainer , WebContainerProcess } from '@webcontainer/api' ;
3
+ import picomatch from 'picomatch' ;
3
4
import { newTask , type Task , type TaskCancelled } from '../tasks.js' ;
4
5
import { MultiCounter } from '../utils/multi-counter.js' ;
5
6
import { clearTerminal , escapeCodes , type ITerminal } from '../utils/terminal.js' ;
@@ -65,7 +66,7 @@ export class TutorialRunner {
65
66
66
67
private _ignoreFileEvents = new MultiCounter ( ) ;
67
68
private _watcher : IFSWatcher | undefined ;
68
- private _watchContentFromWebContainer = false ;
69
+ private _watchContentFromWebContainer : string [ ] | boolean = false ;
69
70
private _readyToWatch = false ;
70
71
71
72
private _packageJsonDirty = false ;
@@ -82,7 +83,7 @@ export class TutorialRunner {
82
83
private _stepController : StepsController ,
83
84
) { }
84
85
85
- setWatchFromWebContainer ( value : boolean ) {
86
+ setWatchFromWebContainer ( value : boolean | string [ ] ) {
86
87
this . _watchContentFromWebContainer = value ;
87
88
88
89
if ( this . _readyToWatch && this . _watchContentFromWebContainer ) {
@@ -654,19 +655,39 @@ export class TutorialRunner {
654
655
return ;
655
656
}
656
657
657
- // for now we only care about 'change' event
658
- if ( eventType !== 'change' ) {
658
+ if (
659
+ Array . isArray ( this . _watchContentFromWebContainer ) &&
660
+ ! this . _watchContentFromWebContainer . some ( ( pattern ) => picomatch . isMatch ( filePath , pattern ) )
661
+ ) {
659
662
return ;
660
663
}
661
664
662
- // we ignore all paths that aren't exposed in the `_editorStore`
663
- const file = this . _editorStore . documents . get ( ) [ filePath ] ;
665
+ if ( eventType === 'change' ) {
666
+ // we ignore all paths that aren't exposed in the `_editorStore`
667
+ const file = this . _editorStore . documents . get ( ) [ filePath ] ;
664
668
665
- if ( ! file ) {
666
- return ;
667
- }
669
+ if ( ! file ) {
670
+ return ;
671
+ }
672
+
673
+ scheduleReadFor ( filePath , typeof file . value === 'string' ? 'utf-8' : null ) ;
674
+ } else if ( eventType === 'rename' && Array . isArray ( this . _watchContentFromWebContainer ) ) {
675
+ const segments = filePath . split ( '/' ) ;
676
+ segments . forEach ( ( _ , index ) => {
677
+ if ( index == segments . length - 1 ) {
678
+ return ;
679
+ }
668
680
669
- scheduleReadFor ( filePath , typeof file . value === 'string' ? 'utf-8' : null ) ;
681
+ const folderPath = segments . slice ( 0 , index + 1 ) . join ( '/' ) ;
682
+
683
+ if ( ! this . _editorStore . documents . get ( ) [ folderPath ] ) {
684
+ this . _editorStore . addFileOrFolder ( { path : folderPath , type : 'folder' } ) ;
685
+ }
686
+ } ) ;
687
+ this . _editorStore . addFileOrFolder ( { path : filePath , type : 'file' } ) ;
688
+ this . _updateCurrentFiles ( { [ filePath ] : 'test' } ) ;
689
+ scheduleReadFor ( filePath , 'utf-8' ) ;
690
+ }
670
691
} ) ;
671
692
}
672
693
0 commit comments