import plugin , { StartFile , StartFilesProps , StartPlugin } from '@start/plugin/src/' ; export default ( glob , userOptions ? ) => ( target ) => plugin ( 'watch' , ( utils ) => async ( ) => { const chokidar = await import ( 'chokidar' ) ; const targetRunner = await target ; const events = [ 'add' , 'change' ] ; const options = { cwd : process . cwd ( ) , persistent : true , ... userOptions } ; await new Promise < StartFile [ ] > ( ( resolve , reject ) => { const initialFiles = [ ] ; const initialListener = ( file ) => { initialFiles . push ( { path : file } ) ; } ; const watcher = chokidar . watch ( glob , options ) ; watcher . on ( 'add' , initialListener ) ; watcher . once ( 'error' , reject ) ; watcher . once ( 'ready' , async ( ) => { watcher . removeListener ( 'add' , initialListener ) ; const watchForChanges = ( ) => { events . forEach ( ( event ) => { watcher . once ( event , async ( file ) => { try { await targetRunner ( utils . reporter ) ( { files : [ { path : file } ] } ) ; } finally { watchForChanges ( ) ; } } ) ; } ) ; } ; try { await targetRunner ( utils . reporter ) ( { files : initialFiles } ) ; } finally { watchForChanges ( ) ; utils . logMessage ( 'watching for changes, press ctrl-c to exit' ) ; } } ) ; } ) ; } ) ;