@@ -108,10 +108,21 @@ async function runWriteSucceed({
108108 let stderr = '' ;
109109 const stdout = [ ] ;
110110
111+ let watchedFiles = [ ] ;
112+ let currentWatchedFileIndex = 0 ;
113+ const isWatchingMultpileFiles = Array . isArray ( watchedFile ) && watchedFile . length > 1 ;
114+ const isWatchingSingleFile = ! isWatchingMultpileFiles ;
115+
116+ if ( isWatchingMultpileFiles ) {
117+ watchedFiles = watchedFile ;
118+ restarts = watchedFiles . length + 1 ;
119+ }
120+
111121 child . stderr . on ( 'data' , ( data ) => {
112122 stderr += data ;
113123 } ) ;
114124
125+
115126 try {
116127 // Break the chunks into lines
117128 for await ( const data of createInterface ( { input : child . stdout } ) ) {
@@ -120,14 +131,16 @@ async function runWriteSucceed({
120131 }
121132 if ( data . startsWith ( completed ) ) {
122133 completes ++ ;
123- if ( completes === restarts ) {
134+ if ( completes === restarts )
124135 break ;
125- }
126- if ( completes === 1 ) {
136+ if ( isWatchingSingleFile && completes === 1 )
127137 cancelRestarts = restart ( watchedFile ) ;
138+ if ( isWatchingMultpileFiles && completes < restarts ) {
139+ cancelRestarts ( ) ;
140+ const currentlyWatchedFile = watchedFiles [ currentWatchedFileIndex ++ ] ;
141+ cancelRestarts = restart ( currentlyWatchedFile ) ;
128142 }
129143 }
130-
131144 if ( ! shouldFail && data . startsWith ( 'Failed running' ) ) {
132145 break ;
133146 }
@@ -884,4 +897,50 @@ process.on('message', (message) => {
884897 await done ( ) ;
885898 }
886899 } ) ;
900+
901+ it ( 'should watch files from a given glob pattern --watch-path=./**/*.js' , async ( ) => {
902+
903+ const tmpDirForGlobTest = tmpdir . resolve ( 'glob-test-dir' ) ;
904+ mkdirSync ( tmpDirForGlobTest ) ;
905+
906+ const globPattern = path . resolve ( tmpDirForGlobTest , '**/*.js' ) ;
907+
908+ const directory1 = path . join ( tmpDirForGlobTest , 'directory1' ) ;
909+ const directory2 = path . join ( tmpDirForGlobTest , 'directory2' ) ;
910+
911+ mkdirSync ( directory1 ) ;
912+ mkdirSync ( directory2 ) ;
913+
914+ const tmpJsFile1 = createTmpFile ( '' , '.js' , directory1 ) ;
915+ const tmpJsFile2 = createTmpFile ( '' , '.js' , directory1 ) ;
916+ const tmpJsFile3 = createTmpFile ( '' , '.js' , directory2 ) ;
917+ const tmpJsFile4 = createTmpFile ( '' , '.js' , directory2 ) ;
918+ const tmpJsFile5 = createTmpFile ( '' , '.js' , directory2 ) ;
919+
920+ const mainJsFile = createTmpFile ( 'console.log(\'running\')' , '.js' , tmpDirForGlobTest ) ;
921+
922+ const args = [ '--watch-path' , globPattern , mainJsFile ] ;
923+ const watchedFiles = [ tmpJsFile1 , tmpJsFile2 , tmpJsFile3 , tmpJsFile4 , tmpJsFile5 ] ;
924+
925+ const { stderr, stdout } = await runWriteSucceed ( {
926+ args,
927+ watchedFile : watchedFiles ,
928+ } ) ;
929+
930+ function expectRepeatedCompletes ( n ) {
931+ const expectedStdout = [ ] ;
932+ for ( let i = 0 ; i < n ; i ++ ) {
933+ if ( i !== 0 ) {
934+ expectedStdout . push ( `Restarting ${ inspect ( ( mainJsFile ) ) } ` ) ;
935+ }
936+ expectedStdout . push ( 'running' ) ;
937+ expectedStdout . push ( `Completed running ${ inspect ( mainJsFile ) } . Waiting for file changes before restarting...` ) ;
938+ }
939+ return expectedStdout ;
940+ }
941+
942+ assert . strictEqual ( stderr , '' ) ;
943+ assert . deepStrictEqual ( stdout , expectRepeatedCompletes ( 6 ) ) ;
944+
945+ } ) ;
887946} ) ;
0 commit comments