From 656ac157d36b77b2dc603224c638908cc7812dbd Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sat, 24 Sep 2016 21:39:36 -0400 Subject: [PATCH] refactor watch module --- lib/watch.js | 72 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/lib/watch.js b/lib/watch.js index a9c15b6dd..d753c8f28 100644 --- a/lib/watch.js +++ b/lib/watch.js @@ -8,8 +8,28 @@ var Watch = function(options) { }; Watch.prototype.start = function() { + var self = this; + // TODO: should come from the config object instead of reading the file + // directly var embarkConfig = JSON.parse(fs.readFileSync("embark.json")); + this.watchAssets(embarkConfig, function() { + self.logger.info('ready to watch asset changes'); + }); + + this.watchContracts(embarkConfig, function() { + self.logger.info('ready to watch contract changes'); + }); + + this.watchConfigs(function() { + self.logger.info('ready to watch config changes'); + }); + + this.logger.info("done!"); +}; + +Watch.prototype.watchAssets = function(embarkConfig, callback) { + var self = this; var appConfig = embarkConfig.app; var filesToWatch = []; @@ -17,64 +37,70 @@ Watch.prototype.start = function() { filesToWatch.push(appConfig[targetFile]); } - // TODO: add callback to ready this.logger.trace(filesToWatch); + var assetWatcher = chokidar.watch(filesToWatch, { ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true }); + assetWatcher .on('add', path => { - this.logger.info(`File ${path} has been added`); - this.trigger('rebuildAssets', path); + self.logger.info(`File ${path} has been added`); + self.trigger('rebuildAssets', path); }) .on('change', path => { - this.logger.info(`File ${path} has been changed`); - this.trigger('rebuildAssets', path); + self.logger.info(`File ${path} has been changed`); + self.trigger('rebuildAssets', path); }) .on('unlink', path => { - this.logger.info(`File ${path} has been removed`); - this.trigger('rebuildAssets', path); + self.logger.info(`File ${path} has been removed`); + self.trigger('rebuildAssets', path); }) - .on('ready', () => this.logger.info('ready to watch changes')); + .on('ready', () => callback()); +}; +Watch.prototype.watchContracts = function(embarkConfig, callback) { var contractsToWatch = []; contractsToWatch.push(embarkConfig.contracts); + this.logger.trace(contractsToWatch); + var contractWatcher = chokidar.watch(contractsToWatch, { ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true }); + contractWatcher .on('add', path => { - this.logger.info(`File ${path} has been added`); - this.trigger('redeploy', path); + self.logger.info(`File ${path} has been added`); + self.trigger('redeploy', path); }) .on('change', path => { - this.logger.info(`File ${path} has been changed`); - this.trigger('redeploy', path); + self.logger.info(`File ${path} has been changed`); + self.trigger('redeploy', path); }) .on('unlink', path => this.logger.info(`File ${path} has been removed`)) - .on('ready', () => this.logger.info('ready to watch changes')); - + .on('ready', () => callback()); +}; +Watch.prototype.watchConfigs = function(callback) { var configWatcher = chokidar.watch("config/**/contracts.json", { ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true }); + configWatcher .on('add', path => { - this.logger.info(`File ${path} has been added`); - this.trigger('redeploy', path); + self.logger.info(`File ${path} has been added`); + self.trigger('redeploy', path); }) .on('change', path => { - this.logger.info(`File ${path} has been changed`); - this.trigger('redeploy', path); + self.logger.info(`File ${path} has been changed`); + self.trigger('redeploy', path); }) .on('unlink', path => { - this.logger.info(`File ${path} has been removed`); - this.trigger('redeploy', path); + self.logger.info(`File ${path} has been removed`); + self.trigger('redeploy', path); }) - .on('ready', () => this.logger.info('ready to watch changes')); - - this.logger.info("done!"); + .on('ready', () => callback()); }; Watch.prototype.on = function(eventName, callback) {