embark/lib/modules/watcher/index.js

208 lines
6.3 KiB
JavaScript
Raw Normal View History

2017-03-29 17:50:05 +00:00
let chokidar = require('chokidar');
let path = require('path');
2016-08-21 15:24:57 +00:00
let fs = require('../../core/fs.js');
const DAPP_PIPELINE_CONFIG_FILE = 'pipeline.js';
const DAPP_WEBPACK_CONFIG_FILE = 'webpack.config.js';
const DAPP_BABEL_LOADER_OVERRIDES_CONFIG_FILE = 'babel-loader-overrides.js';
2017-02-19 03:40:42 +00:00
// TODO: this should be receiving the config object not re-reading the
// embark.json file
class Watcher {
constructor(embark) {
this.logger = embark.logger;
this.events = embark.events;
this.fileWatchers = [];
this.events.setCommandHandler('watcher:start', () => this.start());
this.events.setCommandHandler('watcher:stop', () => this.stop());
this.events.setCommandHandler('watcher:restart', () => this.restart());
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-09-07 18:54:15 +00:00
this.watchContractConfig(embarkConfig, function () {
self.logger.trace('ready to watch contract config changes');
});
this.watchPipelineConfig(embarkConfig, function () {
self.logger.trace('ready to watch pipeline config changes');
});
2018-09-07 18:54:15 +00:00
this.watchWebserverConfig(embarkConfig, function () {
self.logger.trace('ready to watch webserver config changes');
2017-03-30 11:12:39 +00:00
});
2018-05-08 21:49:46 +00:00
this.logger.info(__("ready to watch file changes"));
2017-03-30 11:12:39 +00:00
}
restart() {
this.stop();
this.start();
}
stop() {
this.fileWatchers.forEach(fileWatcher => {
if (fileWatcher.shouldClose) return;
if (fileWatcher.isReady) fileWatcher.close();
fileWatcher.shouldClose = true;
});
}
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) {
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
if (!Array.isArray(files)) {
2018-05-02 19:48:06 +00:00
fileGlob = path.join(path.dirname(files), '**', '*.*').replace(/\\/g, '/');
} else if (files.length === 1) {
2018-05-02 19:48:06 +00:00
fileGlob = path.join(path.dirname(files[0]), '**', '*.*').replace(/\\/g, '/');
}
filesToWatch.push(fileGlob);
2016-09-25 02:05:16 +00:00
}
2018-07-24 12:29:06 +00:00
filesToWatch = Array.from(new Set(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', {fileType: 'asset', path});
2017-03-30 11:12:39 +00:00
},
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', {fileType: 'contract', path});
2017-03-30 11:12:39 +00:00
},
function () {
callback();
}
);
}
2018-09-07 18:54:15 +00:00
watchWebserverConfig(embarkConfig, callback) {
let self = this;
let webserverConfig;
if (typeof embarkConfig.config === 'object') {
if (!embarkConfig.config.webserver) {
return;
}
webserverConfig = embarkConfig.config.webserver;
} else {
let contractsFolder = embarkConfig.config.replace(/\\/g, '/');
if (contractsFolder.charAt(contractsFolder.length - 1) !== '/') {
contractsFolder += '/';
}
webserverConfig = [`${contractsFolder}**/webserver.json`, `${contractsFolder}**/webserver.js`];
}
this.watchFiles(webserverConfig,
function (eventName, path) {
self.logger.info(`${eventName}: ${path}`);
self.events.emit('webserver:config:change', 'config', path);
},
function () {
callback();
}
);
}
watchContractConfig(embarkConfig, callback) {
2017-03-30 11:12:39 +00:00
let self = this;
2018-09-07 18:54:15 +00:00
let contractConfig;
2018-06-15 19:50:23 +00:00
if (typeof embarkConfig.config === 'object' || embarkConfig.config.contracts) {
2018-09-07 18:54:15 +00:00
contractConfig = embarkConfig.config.contracts;
2018-06-15 19:50:23 +00:00
} else {
let contractsFolder = embarkConfig.config.replace(/\\/g, '/');
if (contractsFolder.charAt(contractsFolder.length - 1) !== '/') {
contractsFolder += '/';
}
2018-09-07 18:54:15 +00:00
contractConfig = [`${contractsFolder}**/contracts.json`, `${contractsFolder}**/contracts.js`];
}
2018-09-07 18:54:15 +00:00
this.watchFiles(contractConfig,
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', {fileType: 'config', path});
2017-03-30 11:12:39 +00:00
},
function () {
callback();
}
);
}
watchPipelineConfig(embarkConfig, callback) {
let filesToWatch = [
fs.dappPath('', DAPP_WEBPACK_CONFIG_FILE),
fs.dappPath('', DAPP_BABEL_LOADER_OVERRIDES_CONFIG_FILE)
];
if (typeof embarkConfig.config === 'object' && embarkConfig.config.pipeline) {
filesToWatch.push(embarkConfig.config.pipeline);
} else if (typeof embarkConfig.config === 'string') {
filesToWatch.push(fs.dappPath(embarkConfig.config, DAPP_PIPELINE_CONFIG_FILE));
}
this.watchFiles(filesToWatch, (eventName, path) => {
this.logger.info(`${eventName}: ${path}`);
this.events.emit('file-' + eventName, 'config', path);
this.events.emit('file-event', {fileType: 'config', path});
}, callback);
}
2017-03-30 11:12:39 +00:00
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
});
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))
.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 = Watcher;