hyper/app/config/init.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-11-28 05:17:01 -09:00
import vm from 'vm';
import notify from '../notify';
import mapKeys from '../utils/map-keys';
2019-11-28 05:17:01 -09:00
const _extract = script => {
const module = {};
script.runInNewContext({module});
if (!module.exports) {
throw new Error('Error reading configuration: `module.exports` not set');
}
return module.exports;
};
2019-11-28 05:17:01 -09:00
const _syntaxValidation = cfg => {
try {
return new vm.Script(cfg, {filename: '.hyper.js', displayErrors: true});
} catch (err) {
notify('Error loading config:', `${err.name}, see DevTools for more info`, {error: err});
}
};
2019-11-28 05:17:01 -09:00
const _extractDefault = cfg => {
return _extract(_syntaxValidation(cfg));
};
// init config
2019-11-28 05:17:01 -09:00
const _init = cfg => {
const script = _syntaxValidation(cfg.userCfg);
if (script) {
const _cfg = _extract(script);
if (!_cfg.config) {
notify('Error reading configuration: `config` key is missing');
return cfg.defaultCfg;
}
// Merging platform specific keymaps with user defined keymaps
_cfg.keymaps = mapKeys(Object.assign({}, cfg.defaultCfg.keymaps, _cfg.keymaps));
// Ignore undefined values in plugin and localPlugins array Issue #1862
_cfg.plugins = (_cfg.plugins && _cfg.plugins.filter(Boolean)) || [];
_cfg.localPlugins = (_cfg.localPlugins && _cfg.localPlugins.filter(Boolean)) || [];
return _cfg;
}
return cfg.defaultCfg;
};
2019-11-28 05:17:01 -09:00
export {_init, _extractDefault};