@@ -13,9 +13,27 @@ import { getValueOrDefault } from './helpers/get-value-or-default';
1313export class AssetsManager {
1414 private watchAssetsKeyValue : { [ key : string ] : boolean } = { } ;
1515 private watchers : chokidar . FSWatcher [ ] = [ ] ;
16+ private actionInProgress = false ;
1617
18+ /**
19+ * Using on `nest build` to close file watch or the build process will not end
20+ * Interval like process
21+ * If no action has been taken recently close watchers
22+ * If action has been taken recently flag and try again
23+ */
1724 public closeWatchers ( ) {
18- this . watchers . forEach ( ( watcher ) => watcher . close ( ) ) ;
25+ // Consider adjusting this for larger files
26+ const timeoutMs = 500 ;
27+ const closeFn = ( ) => {
28+ if ( this . actionInProgress ) {
29+ this . actionInProgress = false ;
30+ setTimeout ( closeFn , timeoutMs ) ;
31+ } else {
32+ this . watchers . forEach ( ( watcher ) => watcher . close ( ) ) ;
33+ }
34+ } ;
35+
36+ setTimeout ( closeFn , timeoutMs ) ;
1937 }
2038
2139 public copyAssets (
@@ -78,10 +96,6 @@ export class AssetsManager {
7896 . on ( 'change' , ( path : string ) => this . actionOnFile ( { ...option , path, action : 'change' } ) )
7997 . on ( 'unlink' , ( path : string ) => this . actionOnFile ( { ...option , path, action : 'unlink' } ) ) ;
8098
81- if ( ! isWatchEnabled ) {
82- watcher . on ( 'ready' , ( ) => watcher . close ( ) ) ;
83- }
84-
8599 this . watchers . push ( watcher ) ;
86100 }
87101 } catch ( err ) {
@@ -101,6 +115,8 @@ export class AssetsManager {
101115 }
102116 // Set path value to true for watching the first time
103117 this . watchAssetsKeyValue [ path ] = true ;
118+ // Set action to true to avoid watches getting cutoff
119+ this . actionInProgress = true ;
104120
105121 const dest = copyPathResolve (
106122 path ,
0 commit comments