2017-03-29 17:50:05 +00:00
|
|
|
let chokidar = require('chokidar');
|
2018-02-21 18:51:48 +00:00
|
|
|
let path = require('path');
|
2018-05-02 19:48:06 +00:00
|
|
|
const _ = require('underscore');
|
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;
|
2018-04-27 20:34:13 +00:00
|
|
|
this.fileWatchers = [];
|
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');
|
|
|
|
});
|
|
|
|
|
2018-06-14 13:15:31 +00:00
|
|
|
this.watchConfigs(embarkConfig, function () {
|
2017-03-30 11:12:39 +00:00
|
|
|
self.logger.trace('ready to watch config changes');
|
|
|
|
});
|
|
|
|
|
2018-05-08 21:49:46 +00:00
|
|
|
this.logger.info(__("ready to watch file changes"));
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-27 20:34:13 +00:00
|
|
|
restart() {
|
|
|
|
this.stop();
|
|
|
|
this.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
stop() {
|
|
|
|
this.fileWatchers.forEach(fileWatcher => {
|
2018-05-15 21:20:43 +00:00
|
|
|
if (fileWatcher.shouldClose) return;
|
|
|
|
if (fileWatcher.isReady) fileWatcher.close();
|
|
|
|
fileWatcher.shouldClose = true;
|
2018-04-27 20:34:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
watchAssets(embarkConfig, callback) {
|
|
|
|
let self = this;
|
|
|
|
let appConfig = embarkConfig.app;
|
|
|
|
let filesToWatch = [];
|
|
|
|
|
|
|
|
for (let targetFile in appConfig) {
|
2018-02-21 18:51:48 +00:00
|
|
|
let files = appConfig[targetFile];
|
|
|
|
let fileGlob = files;
|
|
|
|
|
|
|
|
// workaround for imports issue
|
|
|
|
// so embark reacts to changes made in imported js files
|
2018-05-02 19:48:06 +00:00
|
|
|
// chokidar glob patterns only work with front-slashes
|
2018-02-21 19:04:08 +00:00
|
|
|
if (!Array.isArray(files)) {
|
2018-05-02 19:48:06 +00:00
|
|
|
fileGlob = path.join(path.dirname(files), '**', '*.*').replace(/\\/g, '/');
|
2018-02-21 19:04:08 +00:00
|
|
|
} else if (files.length === 1) {
|
2018-05-02 19:48:06 +00:00
|
|
|
fileGlob = path.join(path.dirname(files[0]), '**', '*.*').replace(/\\/g, '/');
|
2018-02-21 18:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filesToWatch.push(fileGlob);
|
2016-09-25 02:05:16 +00:00
|
|
|
}
|
2018-05-02 19:48:06 +00:00
|
|
|
filesToWatch = _.uniq(filesToWatch);
|
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();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-14 13:15:31 +00:00
|
|
|
watchConfigs(embarkConfig, callback) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
2018-06-14 13:15:31 +00:00
|
|
|
let configFolder = embarkConfig.config.replace(/\\/g, '/');
|
|
|
|
if (configFolder.charAt(configFolder.length - 1) !== '/') {
|
|
|
|
configFolder += '/';
|
|
|
|
}
|
2017-03-30 11:12:39 +00:00
|
|
|
this.watchFiles(
|
2018-06-14 13:15:31 +00:00
|
|
|
[`${configFolder}**/contracts.json`, `${configFolder}**/contracts.js`],
|
2017-03-30 11:12:39 +00:00
|
|
|
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, {
|
2018-05-10 17:42:02 +00:00
|
|
|
ignored: /[\/\\]\.|tmp_/, persistent: true, ignoreInitial: true, followSymlinks: true
|
2017-03-30 11:12:39 +00:00
|
|
|
});
|
2018-04-27 20:34:13 +00:00
|
|
|
this.fileWatchers.push(configWatcher);
|
2017-03-30 11:12:39 +00:00
|
|
|
|
|
|
|
configWatcher
|
|
|
|
.on('add', path => changeCallback('add', path))
|
|
|
|
.on('change', path => changeCallback('change', path))
|
|
|
|
.on('unlink', path => changeCallback('remove', path))
|
2018-05-15 21:20:43 +00:00
|
|
|
.once('ready', () => {
|
|
|
|
configWatcher.isReady = true;
|
|
|
|
if (configWatcher.shouldClose) configWatcher.close();
|
|
|
|
doneCallback();
|
|
|
|
});
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-21 15:24:57 +00:00
|
|
|
|
|
|
|
module.exports = Watch;
|