2019-11-28 05:17:01 -09:00
|
|
|
import vm from 'vm';
|
|
|
|
|
import notify from '../notify';
|
|
|
|
|
import mapKeys from '../utils/map-keys';
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
const _extract = script => {
|
2017-06-02 16:03:47 -08:00
|
|
|
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 => {
|
2017-06-02 16:03:47 -08:00
|
|
|
try {
|
|
|
|
|
return new vm.Script(cfg, {filename: '.hyper.js', displayErrors: true});
|
|
|
|
|
} catch (err) {
|
2018-05-02 00:10:44 -08:00
|
|
|
notify('Error loading config:', `${err.name}, see DevTools for more info`, {error: err});
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
const _extractDefault = cfg => {
|
2017-06-02 16:03:47 -08:00
|
|
|
return _extract(_syntaxValidation(cfg));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init config
|
2019-11-28 05:17:01 -09:00
|
|
|
const _init = cfg => {
|
2017-06-02 16:03:47 -08:00
|
|
|
const script = _syntaxValidation(cfg.userCfg);
|
|
|
|
|
if (script) {
|
|
|
|
|
const _cfg = _extract(script);
|
|
|
|
|
if (!_cfg.config) {
|
|
|
|
|
notify('Error reading configuration: `config` key is missing');
|
2017-11-02 18:51:18 -08:00
|
|
|
return cfg.defaultCfg;
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
2017-11-02 18:51:18 -08:00
|
|
|
// Merging platform specific keymaps with user defined keymaps
|
|
|
|
|
_cfg.keymaps = mapKeys(Object.assign({}, cfg.defaultCfg.keymaps, _cfg.keymaps));
|
2017-06-12 17:48:56 -08:00
|
|
|
// 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)) || [];
|
2017-06-02 16:03:47 -08:00
|
|
|
return _cfg;
|
|
|
|
|
}
|
2017-11-02 18:51:18 -08:00
|
|
|
return cfg.defaultCfg;
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export {_init, _extractDefault};
|