2017-08-12 14:23:48 -08:00
|
|
|
const chokidar = require('chokidar');
|
2016-07-08 06:40:27 -08:00
|
|
|
const notify = require('./notify');
|
2017-06-02 16:03:47 -08:00
|
|
|
const _import = require('./config/import');
|
2017-05-26 08:23:25 -08:00
|
|
|
const _openConfig = require('./config/open');
|
2017-06-02 16:03:47 -08:00
|
|
|
const win = require('./config/windows');
|
|
|
|
|
const {cfgPath, cfgDir} = require('./config/paths');
|
2017-01-04 16:05:19 -09:00
|
|
|
|
2016-07-07 07:16:48 -08:00
|
|
|
const watchers = [];
|
2017-06-02 16:03:47 -08:00
|
|
|
// watch for changes on config every 2s on windows
|
|
|
|
|
// https://github.com/zeit/hyper/pull/1772
|
|
|
|
|
const watchCfg = process.platform === 'win32' ? {interval: 2000} : {};
|
2016-07-07 06:46:58 -08:00
|
|
|
let cfg = {};
|
2017-08-12 14:23:48 -08:00
|
|
|
let _watcher;
|
2016-07-07 06:46:58 -08:00
|
|
|
|
2017-06-02 16:03:47 -08:00
|
|
|
const _watch = function () {
|
2017-08-12 14:23:48 -08:00
|
|
|
if (_watcher) {
|
|
|
|
|
return _watcher;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_watcher = chokidar.watch(cfgPath, watchCfg);
|
|
|
|
|
|
|
|
|
|
_watcher.on('change', () => {
|
|
|
|
|
cfg = _import();
|
|
|
|
|
notify('Configuration updated', 'Hyper configuration reloaded!');
|
|
|
|
|
watchers.forEach(fn => fn());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_watcher.on('error', error => {
|
|
|
|
|
console.error('error watching config', error);
|
2016-07-07 06:46:58 -08:00
|
|
|
});
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
2017-01-19 05:11:35 -09:00
|
|
|
|
2016-07-07 07:16:48 -08:00
|
|
|
exports.subscribe = function (fn) {
|
|
|
|
|
watchers.push(fn);
|
|
|
|
|
return () => {
|
|
|
|
|
watchers.splice(watchers.indexOf(fn), 1);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-04 16:05:19 -09:00
|
|
|
exports.getConfigDir = function () {
|
|
|
|
|
// expose config directory to load plugin from the right place
|
2017-06-02 16:03:47 -08:00
|
|
|
return cfgDir;
|
2017-01-04 16:05:19 -09:00
|
|
|
};
|
|
|
|
|
|
2016-07-07 12:49:10 -08:00
|
|
|
exports.getConfig = function () {
|
|
|
|
|
return cfg.config;
|
|
|
|
|
};
|
|
|
|
|
|
2017-05-26 08:23:25 -08:00
|
|
|
exports.openConfig = function () {
|
|
|
|
|
return _openConfig();
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-07 12:49:10 -08:00
|
|
|
exports.getPlugins = function () {
|
|
|
|
|
return {
|
|
|
|
|
plugins: cfg.plugins,
|
|
|
|
|
localPlugins: cfg.localPlugins
|
|
|
|
|
};
|
2016-07-07 06:46:58 -08:00
|
|
|
};
|
2016-10-01 17:44:34 -08:00
|
|
|
|
2017-06-02 16:03:47 -08:00
|
|
|
exports.getKeymaps = function () {
|
|
|
|
|
return cfg.keymaps;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.extendKeymaps = function (keymaps) {
|
|
|
|
|
if (keymaps) {
|
|
|
|
|
cfg.keymaps = keymaps;
|
2016-10-01 17:44:34 -08:00
|
|
|
}
|
|
|
|
|
};
|
2017-06-02 16:03:47 -08:00
|
|
|
|
|
|
|
|
exports.setup = function () {
|
|
|
|
|
cfg = _import();
|
|
|
|
|
_watch();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.getWin = win.get;
|
|
|
|
|
exports.winRecord = win.recordState;
|