refactor watch module

This commit is contained in:
Iuri Matias 2016-09-24 21:39:36 -04:00
parent 3a5891810a
commit 656ac157d3
1 changed files with 49 additions and 23 deletions

View File

@ -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) {