@@ -117,10 +117,21 @@ async function runWriteSucceed({
117117 let stderr = '' ;
118118 const stdout = [ ] ;
119119
120+ let watchedFiles = [ ] ;
121+ let currentWatchedFileIndex = 0 ;
122+ const isWatchingMultpileFiles = Array . isArray ( watchedFile ) && watchedFile . length > 1 ;
123+ const isWatchingSingleFile = ! isWatchingMultpileFiles ;
124+
125+ if ( isWatchingMultpileFiles ) {
126+ watchedFiles = watchedFile ;
127+ restarts = watchedFiles . length + 1 ;
128+ }
129+
120130 child . stderr . on ( 'data' , ( data ) => {
121131 stderr += data ;
122132 } ) ;
123133
134+
124135 try {
125136 // Break the chunks into lines
126137 for await ( const data of createInterface ( { input : child . stdout } ) ) {
@@ -129,14 +140,16 @@ async function runWriteSucceed({
129140 }
130141 if ( data . startsWith ( completed ) ) {
131142 completes ++ ;
132- if ( completes === restarts ) {
143+ if ( completes === restarts )
133144 break ;
134- }
135- if ( completes === 1 ) {
145+ if ( isWatchingSingleFile && completes === 1 )
136146 cancelRestarts = restart ( watchedFile ) ;
147+ if ( isWatchingMultpileFiles && completes < restarts ) {
148+ cancelRestarts ( ) ;
149+ const currentlyWatchedFile = watchedFiles [ currentWatchedFileIndex ++ ] ;
150+ cancelRestarts = restart ( currentlyWatchedFile ) ;
137151 }
138152 }
139-
140153 if ( ! shouldFail && data . startsWith ( 'Failed running' ) ) {
141154 break ;
142155 }
@@ -922,4 +935,50 @@ process.on('message', (message) => {
922935 await done ( ) ;
923936 }
924937 } ) ;
938+
939+ it ( 'should watch files from a given glob pattern --watch-path=./**/*.js' , async ( ) => {
940+
941+ const tmpDirForGlobTest = tmpdir . resolve ( 'glob-test-dir' ) ;
942+ mkdirSync ( tmpDirForGlobTest ) ;
943+
944+ const globPattern = path . resolve ( tmpDirForGlobTest , '**/*.js' ) ;
945+
946+ const directory1 = path . join ( tmpDirForGlobTest , 'directory1' ) ;
947+ const directory2 = path . join ( tmpDirForGlobTest , 'directory2' ) ;
948+
949+ mkdirSync ( directory1 ) ;
950+ mkdirSync ( directory2 ) ;
951+
952+ const tmpJsFile1 = createTmpFile ( '' , '.js' , directory1 ) ;
953+ const tmpJsFile2 = createTmpFile ( '' , '.js' , directory1 ) ;
954+ const tmpJsFile3 = createTmpFile ( '' , '.js' , directory2 ) ;
955+ const tmpJsFile4 = createTmpFile ( '' , '.js' , directory2 ) ;
956+ const tmpJsFile5 = createTmpFile ( '' , '.js' , directory2 ) ;
957+
958+ const mainJsFile = createTmpFile ( 'console.log(\'running\')' , '.js' , tmpDirForGlobTest ) ;
959+
960+ const args = [ '--watch-path' , globPattern , mainJsFile ] ;
961+ const watchedFiles = [ tmpJsFile1 , tmpJsFile2 , tmpJsFile3 , tmpJsFile4 , tmpJsFile5 ] ;
962+
963+ const { stderr, stdout } = await runWriteSucceed ( {
964+ args,
965+ watchedFile : watchedFiles ,
966+ } ) ;
967+
968+ function expectRepeatedCompletes ( n ) {
969+ const expectedStdout = [ ] ;
970+ for ( let i = 0 ; i < n ; i ++ ) {
971+ if ( i !== 0 ) {
972+ expectedStdout . push ( `Restarting ${ inspect ( ( mainJsFile ) ) } ` ) ;
973+ }
974+ expectedStdout . push ( 'running' ) ;
975+ expectedStdout . push ( `Completed running ${ inspect ( mainJsFile ) } . Waiting for file changes before restarting...` ) ;
976+ }
977+ return expectedStdout ;
978+ }
979+
980+ assert . strictEqual ( stderr , '' ) ;
981+ assert . deepStrictEqual ( stdout , expectRepeatedCompletes ( 6 ) ) ;
982+
983+ } ) ;
925984} ) ;
0 commit comments