2016-08-21 12:02:02 -04:00
|
|
|
/*jshint esversion: 6 */
|
2016-08-21 11:24:57 -04:00
|
|
|
var fs = require('fs');
|
|
|
|
var chokidar = require('chokidar');
|
|
|
|
|
|
|
|
var Watch = function(options) {
|
2016-09-16 23:56:25 -04:00
|
|
|
this.logger = options.logger;
|
2016-09-23 01:37:15 -07:00
|
|
|
this.events = {};
|
2016-08-21 11:24:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.start = function() {
|
|
|
|
var embarkConfig = JSON.parse(fs.readFileSync("embark.json"));
|
|
|
|
|
|
|
|
var appConfig = embarkConfig.app;
|
|
|
|
var filesToWatch = [];
|
|
|
|
|
|
|
|
for(var targetFile in appConfig) {
|
|
|
|
filesToWatch.push(appConfig[targetFile]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add callback to ready
|
2016-09-16 23:56:25 -04:00
|
|
|
this.logger.trace(filesToWatch);
|
2016-09-22 22:18:28 -07:00
|
|
|
var assetWatcher = chokidar.watch(filesToWatch, {
|
2016-09-23 01:37:15 -07:00
|
|
|
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
|
2016-08-21 11:24:57 -04:00
|
|
|
});
|
2016-09-22 22:18:28 -07:00
|
|
|
assetWatcher
|
2016-09-23 01:37:15 -07:00
|
|
|
.on('add', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been added`);
|
2016-09-23 01:37:15 -07:00
|
|
|
this.trigger('rebuildAssets', path);
|
|
|
|
})
|
|
|
|
.on('change', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been changed`);
|
2016-09-23 01:37:15 -07:00
|
|
|
this.trigger('rebuildAssets', path);
|
|
|
|
})
|
|
|
|
.on('unlink', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been removed`);
|
2016-09-23 01:37:15 -07:00
|
|
|
this.trigger('rebuildAssets', path);
|
|
|
|
})
|
2016-09-16 23:56:25 -04:00
|
|
|
.on('ready', () => this.logger.info('ready to watch changes'));
|
2016-09-22 22:18:28 -07:00
|
|
|
|
|
|
|
var contractsToWatch = [];
|
|
|
|
contractsToWatch.push(embarkConfig.contracts);
|
|
|
|
this.logger.trace(contractsToWatch);
|
|
|
|
var contractWatcher = chokidar.watch(contractsToWatch, {
|
2016-09-23 01:37:15 -07:00
|
|
|
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
|
2016-09-22 22:18:28 -07:00
|
|
|
});
|
|
|
|
contractWatcher
|
2016-09-23 01:37:15 -07:00
|
|
|
.on('add', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been added`);
|
2016-09-23 01:37:15 -07:00
|
|
|
this.trigger('redeploy', path);
|
|
|
|
})
|
|
|
|
.on('change', path => {
|
|
|
|
this.logger.info(`File ${path} has been changed`);
|
|
|
|
this.trigger('redeploy', path);
|
|
|
|
})
|
2016-09-22 22:18:28 -07:00
|
|
|
.on('unlink', path => this.logger.info(`File ${path} has been removed`))
|
|
|
|
.on('ready', () => this.logger.info('ready to watch changes'));
|
|
|
|
|
2016-09-23 02:45:21 -07:00
|
|
|
|
|
|
|
var configWatcher = chokidar.watch("config/**/contracts.json", {
|
|
|
|
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
|
|
|
|
});
|
|
|
|
configWatcher
|
|
|
|
.on('add', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been added`);
|
2016-09-23 02:45:21 -07:00
|
|
|
this.trigger('redeploy', path);
|
|
|
|
})
|
|
|
|
.on('change', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been changed`);
|
2016-09-23 02:45:21 -07:00
|
|
|
this.trigger('redeploy', path);
|
|
|
|
})
|
|
|
|
.on('unlink', path => {
|
2016-09-24 21:23:57 -04:00
|
|
|
this.logger.info(`File ${path} has been removed`);
|
2016-09-23 02:45:21 -07:00
|
|
|
this.trigger('redeploy', path);
|
|
|
|
})
|
|
|
|
.on('ready', () => this.logger.info('ready to watch changes'));
|
|
|
|
|
2016-09-16 23:56:25 -04:00
|
|
|
this.logger.info("done!");
|
2016-08-21 11:24:57 -04:00
|
|
|
};
|
|
|
|
|
2016-09-23 01:37:15 -07:00
|
|
|
Watch.prototype.on = function(eventName, callback) {
|
|
|
|
this.events[eventName] = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
Watch.prototype.trigger = function(eventName, values) {
|
|
|
|
this.events[eventName](values);
|
|
|
|
};
|
|
|
|
|
2016-08-21 11:24:57 -04:00
|
|
|
module.exports = Watch;
|