2017-06-02 16:03:47 -08:00
|
|
|
const vm = require('vm');
|
|
|
|
|
const notify = require('../notify');
|
2017-11-02 18:51:18 -08:00
|
|
|
const mapKeys = require('../utils/map-keys');
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const _extract = function(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;
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const _syntaxValidation = function(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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-10 05:35:10 -08:00
|
|
|
const _extractDefault = function(cfg) {
|
2017-06-02 16:03:47 -08:00
|
|
|
return _extract(_syntaxValidation(cfg));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init config
|
2017-09-10 05:35:10 -08:00
|
|
|
const _init = function(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
|
|
|
};
|
|
|
|
|
|
2017-09-20 13:32:32 -08:00
|
|
|
module.exports = {
|
|
|
|
|
_init,
|
|
|
|
|
_extractDefault
|
|
|
|
|
};
|