2017-06-02 16:03:47 -08:00
|
|
|
const vm = require('vm');
|
|
|
|
|
const notify = require('../notify');
|
|
|
|
|
|
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) {
|
|
|
|
|
notify(`Error loading config: ${err.name}, see DevTools for more info`);
|
2017-09-10 05:35:10 -08:00
|
|
|
//eslint-disable-next-line no-console
|
2017-06-02 16:03:47 -08:00
|
|
|
console.error('Error loading config:', err);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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) {
|
|
|
|
|
_cfg.plugins = _cfg.plugins || [];
|
|
|
|
|
_cfg.localPlugins = _cfg.localPlugins || [];
|
|
|
|
|
_cfg.keymaps = _cfg.keymaps || {};
|
|
|
|
|
notify('Error reading configuration: `config` key is missing');
|
|
|
|
|
return _extractDefault(cfg.defaultCfg);
|
|
|
|
|
}
|
2017-09-06 12:58:09 -08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
return _extractDefault(cfg.defaultCfg);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = _init;
|