2019-11-28 05:17:01 -09:00
|
|
|
import vm from 'vm';
|
|
|
|
|
import notify from '../notify';
|
|
|
|
|
import mapKeys from '../utils/map-keys';
|
2020-04-27 05:32:08 -08:00
|
|
|
import {parsedConfig, rawConfig, configOptions} from '../../lib/config';
|
2022-12-25 20:01:04 -09:00
|
|
|
import _ from 'lodash';
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2019-12-24 06:56:23 -09:00
|
|
|
const _extract = (script?: vm.Script): Record<string, any> => {
|
|
|
|
|
const module: Record<string, any> = {};
|
|
|
|
|
script?.runInNewContext({module});
|
2017-06-02 16:03:47 -08:00
|
|
|
if (!module.exports) {
|
|
|
|
|
throw new Error('Error reading configuration: `module.exports` not set');
|
|
|
|
|
}
|
2021-03-28 12:13:49 -08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
2017-06-02 16:03:47 -08:00
|
|
|
return module.exports;
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-24 06:56:23 -09:00
|
|
|
const _syntaxValidation = (cfg: string) => {
|
2017-06-02 16:03:47 -08:00
|
|
|
try {
|
|
|
|
|
return new vm.Script(cfg, {filename: '.hyper.js', displayErrors: true});
|
2021-08-30 05:37:13 -08:00
|
|
|
} catch (_err) {
|
|
|
|
|
const err = _err as {name: string};
|
2020-11-06 08:47:09 -09:00
|
|
|
notify(`Error loading config: ${err.name}`, `${err}`, {error: err});
|
2017-06-02 16:03:47 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-12-24 06:56:23 -09:00
|
|
|
const _extractDefault = (cfg: string) => {
|
2017-06-02 16:03:47 -08:00
|
|
|
return _extract(_syntaxValidation(cfg));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// init config
|
2022-12-25 20:01:04 -09:00
|
|
|
const _init = (userCfg: rawConfig, defaultCfg: rawConfig): parsedConfig => {
|
2020-04-27 05:32:08 -08:00
|
|
|
return {
|
|
|
|
|
config: (() => {
|
2022-12-25 20:01:04 -09:00
|
|
|
if (userCfg?.config) {
|
2023-01-06 20:39:33 -09:00
|
|
|
return _.merge({}, defaultCfg.config, userCfg.config);
|
2020-04-27 05:32:08 -08:00
|
|
|
} else {
|
|
|
|
|
notify('Error reading configuration: `config` key is missing');
|
2022-12-25 20:01:04 -09:00
|
|
|
return defaultCfg.config || ({} as configOptions);
|
2020-04-27 05:32:08 -08:00
|
|
|
}
|
|
|
|
|
})(),
|
2017-11-02 18:51:18 -08:00
|
|
|
// Merging platform specific keymaps with user defined keymaps
|
2022-12-25 20:01:04 -09:00
|
|
|
keymaps: mapKeys({...defaultCfg.keymaps, ...userCfg?.keymaps}),
|
2017-06-12 17:48:56 -08:00
|
|
|
// Ignore undefined values in plugin and localPlugins array Issue #1862
|
2022-12-25 20:01:04 -09:00
|
|
|
plugins: (userCfg?.plugins && userCfg.plugins.filter(Boolean)) || [],
|
|
|
|
|
localPlugins: (userCfg?.localPlugins && userCfg.localPlugins.filter(Boolean)) || []
|
2020-04-27 05:32:08 -08:00
|
|
|
};
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export {_init, _extractDefault};
|