hyper/app/config.js

77 lines
1.6 KiB
JavaScript
Raw Normal View History

const chokidar = require('chokidar');
2016-07-08 06:40:27 -08:00
const notify = require('./notify');
const _import = require('./config/import');
const _openConfig = require('./config/open');
const win = require('./config/windows');
const {cfgPath, cfgDir} = require('./config/paths');
const watchers = [];
// 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 = {};
let _watcher;
2016-07-07 06:46:58 -08:00
const _watch = function () {
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
});
};
exports.subscribe = function (fn) {
watchers.push(fn);
return () => {
watchers.splice(watchers.indexOf(fn), 1);
};
};
exports.getConfigDir = function () {
// expose config directory to load plugin from the right place
return cfgDir;
};
2016-07-07 12:49:10 -08:00
exports.getConfig = function () {
return cfg.config;
};
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
};
exports.getKeymaps = function () {
return cfg.keymaps;
};
exports.extendKeymaps = function (keymaps) {
if (keymaps) {
cfg.keymaps = keymaps;
}
};
exports.setup = function () {
cfg = _import();
_watch();
};
exports.getWin = win.get;
exports.winRecord = win.recordState;