2020-07-15 05:03:44 -08:00
|
|
|
import chokidar from 'chokidar';
|
2019-11-28 05:17:01 -09:00
|
|
|
import notify from './notify';
|
|
|
|
|
import {_import, getDefaultConfig} from './config/import';
|
|
|
|
|
import _openConfig from './config/open';
|
|
|
|
|
import {cfgPath, cfgDir} from './config/paths';
|
|
|
|
|
import {getColorMap} from './utils/colors';
|
2020-04-27 05:32:08 -08:00
|
|
|
import {parsedConfig, configOptions} from '../lib/config';
|
2021-01-07 02:44:09 -09:00
|
|
|
import {app} from 'electron';
|
2017-01-04 16:05:19 -09:00
|
|
|
|
2020-06-19 04:51:34 -08:00
|
|
|
const watchers: Function[] = [];
|
2020-04-27 05:32:08 -08:00
|
|
|
let cfg: parsedConfig = {} as any;
|
2020-07-15 05:03:44 -08:00
|
|
|
let _watcher: chokidar.FSWatcher;
|
2020-01-02 05:44:11 -09:00
|
|
|
|
2020-04-27 05:32:08 -08:00
|
|
|
export const getDeprecatedCSS = (config: configOptions) => {
|
2020-01-02 05:44:11 -09:00
|
|
|
const deprecated: string[] = [];
|
|
|
|
|
const deprecatedCSS = ['x-screen', 'x-row', 'cursor-node', '::selection'];
|
2020-03-25 02:15:08 -08:00
|
|
|
deprecatedCSS.forEach((css) => {
|
2020-07-13 05:05:34 -08:00
|
|
|
if (config.css?.includes(css) || config.termCSS?.includes(css)) {
|
2020-01-02 05:44:11 -09:00
|
|
|
deprecated.push(css);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return deprecated;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const checkDeprecatedConfig = () => {
|
|
|
|
|
if (!cfg.config) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const deprecated = getDeprecatedCSS(cfg.config);
|
|
|
|
|
if (deprecated.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const deprecatedStr = deprecated.join(', ');
|
|
|
|
|
notify('Configuration warning', `Your configuration uses some deprecated CSS classes (${deprecatedStr})`);
|
|
|
|
|
};
|
2016-07-07 06:46:58 -08:00
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
const _watch = () => {
|
2017-08-12 14:23:48 -08:00
|
|
|
if (_watcher) {
|
2020-07-15 05:03:44 -08:00
|
|
|
return;
|
2017-08-12 14:23:48 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-16 05:49:43 -08:00
|
|
|
const onChange = () => {
|
2018-01-09 06:05:19 -09:00
|
|
|
// Need to wait 100ms to ensure that write is complete
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
cfg = _import();
|
|
|
|
|
notify('Configuration updated', 'Hyper configuration reloaded!');
|
2021-03-28 12:13:49 -08:00
|
|
|
watchers.forEach((fn) => {
|
|
|
|
|
fn();
|
|
|
|
|
});
|
2018-01-09 06:05:19 -09:00
|
|
|
checkDeprecatedConfig();
|
|
|
|
|
}, 100);
|
2017-09-16 05:49:43 -08:00
|
|
|
};
|
2017-08-12 14:23:48 -08:00
|
|
|
|
2020-07-15 05:03:44 -08:00
|
|
|
_watcher = chokidar.watch(cfgPath);
|
|
|
|
|
_watcher.on('change', onChange);
|
|
|
|
|
_watcher.on('error', (error) => {
|
|
|
|
|
console.error('error watching config', error);
|
|
|
|
|
});
|
2021-01-07 02:44:09 -09:00
|
|
|
|
2022-01-03 04:08:38 -09:00
|
|
|
app.on('before-quit', () => {
|
2021-01-07 02:44:09 -09:00
|
|
|
if (Object.keys(_watcher.getWatched()).length > 0) {
|
2022-01-03 04:08:38 -09:00
|
|
|
_watcher.close().catch((err) => {
|
|
|
|
|
console.warn(err);
|
|
|
|
|
});
|
2021-01-07 02:44:09 -09:00
|
|
|
}
|
|
|
|
|
});
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
2017-01-19 05:11:35 -09:00
|
|
|
|
2020-01-02 05:44:11 -09:00
|
|
|
export const subscribe = (fn: Function) => {
|
2016-07-07 07:16:48 -08:00
|
|
|
watchers.push(fn);
|
|
|
|
|
return () => {
|
|
|
|
|
watchers.splice(watchers.indexOf(fn), 1);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const getConfigDir = () => {
|
2017-01-04 16:05:19 -09:00
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const getConfig = () => {
|
2016-07-07 12:49:10 -08:00
|
|
|
return cfg.config;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const openConfig = () => {
|
2017-05-26 08:23:25 -08:00
|
|
|
return _openConfig();
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-02 05:44:11 -09:00
|
|
|
export const getPlugins = (): {plugins: string[]; localPlugins: string[]} => {
|
2016-07-07 12:49:10 -08:00
|
|
|
return {
|
|
|
|
|
plugins: cfg.plugins,
|
|
|
|
|
localPlugins: cfg.localPlugins
|
|
|
|
|
};
|
2016-07-07 06:46:58 -08:00
|
|
|
};
|
2016-10-01 17:44:34 -08:00
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const getKeymaps = () => {
|
2017-06-02 16:03:47 -08:00
|
|
|
return cfg.keymaps;
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const setup = () => {
|
2017-06-02 16:03:47 -08:00
|
|
|
cfg = _import();
|
|
|
|
|
_watch();
|
2017-09-10 05:35:10 -08:00
|
|
|
checkDeprecatedConfig();
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
|
|
|
|
|
2021-03-28 09:33:05 -08:00
|
|
|
export {get as getWin, recordState as winRecord, defaults as windowDefaults} from './config/windows';
|
2017-09-10 01:35:39 -08:00
|
|
|
|
2020-04-27 05:32:08 -08:00
|
|
|
export const fixConfigDefaults = (decoratedConfig: configOptions) => {
|
|
|
|
|
const defaultConfig = getDefaultConfig().config!;
|
2018-03-21 01:00:29 -08:00
|
|
|
decoratedConfig.colors = getColorMap(decoratedConfig.colors) || {};
|
2017-09-20 13:32:32 -08:00
|
|
|
// We must have default colors for xterm css.
|
2020-04-27 05:32:08 -08:00
|
|
|
decoratedConfig.colors = {...defaultConfig.colors, ...decoratedConfig.colors};
|
2017-09-20 13:32:32 -08:00
|
|
|
return decoratedConfig;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-27 05:32:08 -08:00
|
|
|
export const htermConfigTranslate = (config: configOptions) => {
|
2020-01-02 05:44:11 -09:00
|
|
|
const cssReplacements: Record<string, string> = {
|
2017-09-10 05:35:10 -08:00
|
|
|
'x-screen x-row([ {.[])': '.xterm-rows > div$1',
|
|
|
|
|
'.cursor-node([ {.[])': '.terminal-cursor$1',
|
|
|
|
|
'::selection([ {.[])': '.terminal .xterm-selection div$1',
|
|
|
|
|
'x-screen a([ {.[])': '.terminal a$1',
|
|
|
|
|
'x-row a([ {.[])': '.terminal a$1'
|
|
|
|
|
};
|
2020-03-25 02:15:08 -08:00
|
|
|
Object.keys(cssReplacements).forEach((pattern) => {
|
2017-09-10 03:46:59 -08:00
|
|
|
const searchvalue = new RegExp(pattern, 'g');
|
|
|
|
|
const newvalue = cssReplacements[pattern];
|
2020-07-13 05:05:34 -08:00
|
|
|
config.css = config.css?.replace(searchvalue, newvalue);
|
|
|
|
|
config.termCSS = config.termCSS?.replace(searchvalue, newvalue);
|
2017-09-10 05:35:10 -08:00
|
|
|
});
|
2017-09-10 03:46:59 -08:00
|
|
|
return config;
|
2017-09-10 05:35:10 -08:00
|
|
|
};
|