embark/lib/watch.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

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;
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, {
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
2016-08-21 11:24:57 -04:00
});
2016-09-22 22:18:28 -07:00
assetWatcher
.on('add', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been added`);
this.trigger('rebuildAssets', path);
})
.on('change', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been changed`);
this.trigger('rebuildAssets', path);
})
.on('unlink', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been removed`);
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, {
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
2016-09-22 22:18:28 -07:00
});
contractWatcher
.on('add', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been added`);
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'));
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`);
this.trigger('redeploy', path);
})
.on('change', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been changed`);
this.trigger('redeploy', path);
})
.on('unlink', path => {
2016-09-24 21:23:57 -04:00
this.logger.info(`File ${path} has been removed`);
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
};
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;