| 8 | |
| 9 | |
| 10 | function init(logger, events, rootPath) { |
| 11 | var d = Q.defer(); |
| 12 | |
| 13 | // Normalize paths |
| 14 | function normalize(fullPath) { |
| 15 | return path.normalize( |
| 16 | '/' + path.relative(rootPath, fullPath) |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | logger.log('Starting Watch'); |
| 21 | // Construct |
| 22 | watchr.watch({ |
| 23 | paths: [rootPath], |
| 24 | |
| 25 | // Following links causes issues with broken symlinks |
| 26 | // crashing the whole watchr instance and thus codebox |
| 27 | // so disabling link following for now |
| 28 | followLinks: false, |
| 29 | |
| 30 | listeners: { |
| 31 | log: function(logLevel) { |
| 32 | /* |
| 33 | events.emit('watch.log', { |
| 34 | level: logLevel, |
| 35 | args: _.toArray(arguments) |
| 36 | }); |
| 37 | */ |
| 38 | }, |
| 39 | error: function(err) { |
| 40 | events.emit('watch.error', err); |
| 41 | }, |
| 42 | watching: function(err, watcherInstance, isWatching) { |
| 43 | var emitStr = 'watch.watching.' + (err ? 'error' : 'success'); |
| 44 | var info = { |
| 45 | watching: isWatching, |
| 46 | state: watcherInstance.state, |
| 47 | path: normalize(watcherInstance.path), |
| 48 | error: err || null |
| 49 | }; |
| 50 | |
| 51 | events.emit(emitStr, info); |
| 52 | }, |
| 53 | change: batch(function(changeType, filePath, fileCurrentStat, filePreviousStat) { |
| 54 | // Simply queue the data for our batch processor |
| 55 | return { |
| 56 | change: changeType, |
| 57 | path: normalize(filePath), |
| 58 | stats: { |
| 59 | current: fileCurrentStat, |
| 60 | old: filePreviousStat |
| 61 | } |
| 62 | }; |
| 63 | }, function process(eventList) { |
| 64 | // Aggregate events by folder |
| 65 | var folderEvents = _(eventList).reduce(function(context, e) { |
| 66 | // Aggregate by parent folder of changed path |
| 67 | var key = path.dirname(e.path); |