2016-08-21 16:02:02 +00:00
|
|
|
/*jshint esversion: 6 */
|
2017-03-29 17:50:05 +00:00
|
|
|
let chokidar = require('chokidar');
|
2016-08-21 15:24:57 +00:00
|
|
|
|
2017-03-29 17:50:05 +00:00
|
|
|
let fs = require('../core/fs.js');
|
2017-02-19 18:17:28 +00:00
|
|
|
|
2017-02-19 03:40:42 +00:00
|
|
|
// TODO: this should be receiving the config object not re-reading the
|
|
|
|
// embark.json file
|
2017-03-30 11:12:39 +00:00
|
|
|
class Watch {
|
|
|
|
constructor(options) {
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
2016-08-21 15:24:57 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
start() {
|
|
|
|
let self = this;
|
|
|
|
// TODO: should come from the config object instead of reading the file
|
|
|
|
// directly
|
|
|
|
let embarkConfig = fs.readJSONSync("embark.json");
|
|
|
|
|
|
|
|
this.watchAssets(embarkConfig, function () {
|
|
|
|
self.logger.trace('ready to watch asset changes');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.watchContracts(embarkConfig, function () {
|
|
|
|
self.logger.trace('ready to watch contract changes');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.watchConfigs(function () {
|
|
|
|
self.logger.trace('ready to watch config changes');
|
|
|
|
});
|
|
|
|
|
|
|
|
this.logger.info("ready to watch file changes");
|
|
|
|
}
|
|
|
|
|
|
|
|
watchAssets(embarkConfig, callback) {
|
|
|
|
let self = this;
|
|
|
|
let appConfig = embarkConfig.app;
|
|
|
|
let filesToWatch = [];
|
|
|
|
|
|
|
|
for (let targetFile in appConfig) {
|
|
|
|
filesToWatch.push(appConfig[targetFile]);
|
2016-09-25 02:05:16 +00:00
|
|
|
}
|
2017-03-30 11:12:39 +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);
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
watchContracts(embarkConfig, callback) {
|
|
|
|
let 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);
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
watchConfigs(callback) {
|
|
|
|
let 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);
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
watchFiles(files, changeCallback, doneCallback) {
|
|
|
|
this.logger.trace('watchFiles');
|
|
|
|
this.logger.trace(files);
|
|
|
|
|
|
|
|
let configWatcher = chokidar.watch(files, {
|
|
|
|
ignored: /[\/\\]\./, persistent: true, ignoreInitial: true, followSymlinks: true
|
|
|
|
});
|
|
|
|
|
|
|
|
configWatcher
|
|
|
|
.on('add', path => changeCallback('add', path))
|
|
|
|
.on('change', path => changeCallback('change', path))
|
|
|
|
.on('unlink', path => changeCallback('remove', path))
|
|
|
|
.on('ready', doneCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-21 15:24:57 +00:00
|
|
|
|
|
|
|
module.exports = Watch;
|