@@ -2,6 +2,8 @@ const directoryTree = require('directory-tree');
22const codecrumbs = require ( './codecrumbs/codecrumbs' ) ;
33const file = require ( './utils/file' ) ;
44const madge = require ( 'madge' ) ;
5+ const chokidar = require ( 'chokidar' ) ;
6+ const debounce = require ( 'lodash.debounce' ) ;
57
68const getDirFiles = projectDir => {
79 const filesList = [ ] ;
@@ -21,7 +23,7 @@ const getDependenciesList = (projectDir, entryPoint) => {
2123 return madge ( projectDir + entryPoint ) . then ( res => res . obj ( ) ) ;
2224} ;
2325
24- const getProjectSourceStats = ( projectDir , entryPoint ) => {
26+ const grabProjectSourceState = ( projectDir , entryPoint ) => {
2527 const dirFiles = getDirFiles ( projectDir ) ;
2628
2729 return Promise . all ( [
@@ -41,11 +43,22 @@ const getProjectSourceStats = (projectDir, entryPoint) => {
4143 } ) ) ;
4244} ;
4345
44- const subscribeOnChange = ( projectDir , entryPoint , fn ) => {
45- //first response on registration
46- getProjectSourceStats ( projectDir , entryPoint ) . then ( fn ) ;
46+ const createWatcher = ( dir , fn ) => {
47+ const DELAY = 500 ;
48+
49+ const watcher = chokidar . watch ( dir ) ;
50+ watcher . on ( 'all' , debounce ( fn , DELAY ) ) ;
4751
48- //watch changes here and trigger fn()
52+ return watcher ;
53+ } ;
54+
55+ const subscribeOnChange = ( projectDir , entryPoint , fn ) => {
56+ //use watcher.close(); to stop watching
57+ return createWatcher ( projectDir , ( ) => {
58+ console . log ( 'Update project source state...' ) ;
59+ //TODO: add more specific handling, to re-draw only changed files
60+ grabProjectSourceState ( projectDir , entryPoint ) . then ( fn ) ;
61+ } ) ;
4962} ;
5063
5164module . exports = {
0 commit comments