2016-08-21 16:02:02 +00:00
|
|
|
/*jshint esversion: 6 */
|
2016-08-21 15:24:57 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var chokidar = require('chokidar');
|
|
|
|
|
|
|
|
var Watch = function(options) {
|
2016-09-17 03:56:25 +00:00
|
|
|
this.logger = options.logger;
|
2016-09-23 08:37:15 +00:00
|
|
|
this.events = {};
|
2016-08-21 15:24:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.start = function() {
|
2016-09-25 01:39:36 +00:00
|
|
|
var self = this;
|
|
|
|
// TODO: should come from the config object instead of reading the file
|
|
|
|
// directly
|
2016-08-21 15:24:57 +00:00
|
|
|
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
|
|
|
|
|
2016-09-25 01:39:36 +00:00
|
|
|
this.watchAssets(embarkConfig, function() {
|
2016-09-25 02:37:52 +00:00
|
|
|
self.logger.trace('ready to watch asset changes');
|
2016-09-25 01:39:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.watchContracts(embarkConfig, function() {
|
2016-09-25 02:37:52 +00:00
|
|
|
self.logger.trace('ready to watch contract changes');
|
2016-09-25 01:39:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.watchConfigs(function() {
|
2016-09-25 02:37:52 +00:00
|
|
|
self.logger.trace('ready to watch config changes');
|
2016-09-25 01:39:36 +00:00
|
|
|
});
|
|
|
|
|
2016-09-25 02:37:52 +00:00
|
|
|
this.logger.info("ready to watch changes");
|
2016-09-25 01:39:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.watchAssets = function(embarkConfig, callback) {
|
|
|
|
var self = this;
|
2016-08-21 15:24:57 +00:00
|
|
|
var appConfig = embarkConfig.app;
|
|
|
|
var filesToWatch = [];
|
|
|
|
|
|
|
|
for(var targetFile in appConfig) {
|
|
|
|
filesToWatch.push(appConfig[targetFile]);
|
|
|
|
}
|
|
|
|
|
2016-09-25 02:05:16 +00:00
|
|
|
this.watchFiles(
|
|
|
|
filesToWatch,
|
|
|
|
function(eventName, path) {
|
|
|
|
self.logger.info(`${eventName}: ${path}`);
|
|
|
|
self.trigger('rebuildAssets', path);
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
2016-09-25 01:39:36 +00:00
|
|
|
};
|
2016-09-23 05:18:28 +00:00
|
|
|
|
2016-09-25 01:39:36 +00:00
|
|
|
Watch.prototype.watchContracts = function(embarkConfig, callback) {
|
2016-09-25 02:05:16 +00:00
|
|
|
var self = this;
|
|
|
|
this.watchFiles(
|
|
|
|
[embarkConfig.contracts],
|
|
|
|
function(eventName, path) {
|
|
|
|
self.logger.info(`${eventName}: ${path}`);
|
|
|
|
self.trigger('redeploy', path);
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
2016-09-25 01:39:36 +00:00
|
|
|
};
|
2016-09-23 09:45:21 +00:00
|
|
|
|
2016-09-25 01:39:36 +00:00
|
|
|
Watch.prototype.watchConfigs = function(callback) {
|
2016-09-25 02:05:16 +00:00
|
|
|
var self = this;
|
|
|
|
this.watchFiles(
|
|
|
|
"config/**/contracts.json",
|
|
|
|
function(eventName, path) {
|
|
|
|
self.logger.info(`${eventName}: ${path}`);
|
|
|
|
self.trigger('redeploy', path);
|
|
|
|
},
|
|
|
|
function() {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.watchFiles = function(files, changeCallback, doneCallback) {
|
2016-09-25 02:37:52 +00:00
|
|
|
this.logger.trace('watchFiles');
|
2016-09-25 02:05:16 +00:00
|
|
|
this.logger.trace(files);
|
|
|
|
|
|
|
|
var configWatcher = chokidar.watch(files, {
|
2016-09-23 09:45:21 +00:00
|
|
|
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
|
|
|
|
});
|
2016-09-25 01:39:36 +00:00
|
|
|
|
2016-09-23 09:45:21 +00:00
|
|
|
configWatcher
|
2016-09-25 02:05:16 +00:00
|
|
|
.on('add', path => changeCallback('add', path))
|
|
|
|
.on('change', path => changeCallback('add', path))
|
|
|
|
.on('unlink', path => changeCallback('add', path))
|
|
|
|
.on('ready', doneCallback);
|
2016-08-21 15:24:57 +00:00
|
|
|
};
|
|
|
|
|
2016-09-23 08:37:15 +00:00
|
|
|
Watch.prototype.on = function(eventName, callback) {
|
|
|
|
this.events[eventName] = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.trigger = function(eventName, values) {
|
|
|
|
this.events[eventName](values);
|
|
|
|
};
|
|
|
|
|
2016-08-21 15:24:57 +00:00
|
|
|
module.exports = Watch;
|