refactor pipeline to use events instead

This commit is contained in:
Iuri Matias 2017-02-24 06:18:57 -05:00
parent 3d240c9d84
commit 3433c75554
3 changed files with 25 additions and 22 deletions

3
lib/core/events.js Normal file
View File

@ -0,0 +1,3 @@
var EventEmitter = require('events');
module.exports = EventEmitter;

View File

@ -15,6 +15,7 @@ var Test = require('./core/test.js');
var Logger = require('./core/logger.js');
var Config = require('./core/config.js');
var ServicesMonitor = require('./core/services.js');
var Events = require('./core/events.js');
var Dashboard = require('./dashboard/dashboard.js');
@ -37,6 +38,7 @@ var Embark = {
},
initConfig: function(env, options) {
this.events = new Events();
this.logger = new Logger({logLevel: 'debug'});
this.config = new Config({env: env, logger: this.logger});
@ -151,16 +153,19 @@ var Embark = {
},
function watchFilesForChanges(callback) {
self.logger.setStatus("Watching for changes");
var watch = new Watch({logger: self.logger});
var watch = new Watch({logger: self.logger, events: self.events});
watch.start();
watch.on('redeploy', function() {
self.events.on('file-event', function(fileType, path) {
if (fileType === 'contract' || fileType === 'config') {
self.logger.info("received redeploy event");
Embark.redeploy();
}
});
watch.on('rebuildAssets', function() {
self.events.on('rebuildAssets', function(fileType, path) {
if (fileType === 'asset') {
self.logger.info("received rebuildAssets event");
// TODO: make this more efficient
Embark.redeploy();
}
});
callback();
},

View File

@ -7,7 +7,7 @@ var fs = require('../core/fs.js');
// embark.json file
var Watch = function(options) {
this.logger = options.logger;
this.events = {};
this.events = options.events;
};
Watch.prototype.start = function() {
@ -44,7 +44,8 @@ Watch.prototype.watchAssets = function(embarkConfig, callback) {
filesToWatch,
function(eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.trigger('rebuildAssets', path);
self.events.emit('file-' + eventName, 'asset', path);
self.events.emit('file-event', 'asset', path);
},
function() {
callback();
@ -58,7 +59,8 @@ Watch.prototype.watchContracts = function(embarkConfig, callback) {
[embarkConfig.contracts],
function(eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.trigger('redeploy', path);
self.events.emit('file-' + eventName, 'contract', path);
self.events.emit('file-event', 'contract', path);
},
function() {
callback();
@ -72,7 +74,8 @@ Watch.prototype.watchConfigs = function(callback) {
"config/**/contracts.json",
function(eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.trigger('redeploy', path);
self.events.emit('file-' + eventName, 'config', path);
self.events.emit('file-event', 'config', path);
},
function() {
callback();
@ -90,17 +93,9 @@ Watch.prototype.watchFiles = function(files, changeCallback, doneCallback) {
configWatcher
.on('add', path => changeCallback('add', path))
.on('change', path => changeCallback('add', path))
.on('unlink', path => changeCallback('add', path))
.on('change', path => changeCallback('change', path))
.on('unlink', path => changeCallback('remove', path))
.on('ready', doneCallback);
};
Watch.prototype.on = function(eventName, callback) {
this.events[eventName] = callback;
};
Watch.prototype.trigger = function(eventName, values) {
this.events[eventName](values);
};
module.exports = Watch;