embark/lib/pipeline/watch.js

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-08-21 16:02:02 +00:00
/*jshint esversion: 6 */
2016-08-21 15:24:57 +00:00
var chokidar = require('chokidar');
var fs = require('../core/fs.js');
2017-02-19 03:40:42 +00:00
// TODO: this should be receiving the config object not re-reading the
// embark.json file
2016-08-21 15:24:57 +00:00
var Watch = function(options) {
2016-09-17 03:56:25 +00:00
this.logger = options.logger;
this.events = options.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
2017-02-19 03:40:42 +00:00
var embarkConfig = fs.readJSONSync("embark.json");
2016-08-21 15:24:57 +00:00
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
});
this.logger.info("ready to watch file 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.events.emit('file-' + eventName, 'asset', path);
self.events.emit('file-event', 'asset', path);
2016-09-25 02:05:16 +00:00
},
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.events.emit('file-' + eventName, 'contract', path);
self.events.emit('file-event', 'contract', path);
2016-09-25 02:05:16 +00:00
},
function() {
callback();
}
);
2016-09-25 01:39:36 +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.events.emit('file-' + eventName, 'config', path);
self.events.emit('file-event', 'config', path);
2016-09-25 02:05:16 +00:00
},
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, {
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
});
2016-09-25 01:39:36 +00:00
configWatcher
2016-09-25 02:05:16 +00:00
.on('add', path => changeCallback('add', path))
.on('change', path => changeCallback('change', path))
.on('unlink', path => changeCallback('remove', path))
2016-09-25 02:05:16 +00:00
.on('ready', doneCallback);
2016-08-21 15:24:57 +00:00
};
module.exports = Watch;