2023-02-14 05:28:07 -09:00
|
|
|
import {readFileSync, mkdirpSync} from 'fs-extra';
|
2023-01-06 20:39:33 -09:00
|
|
|
import {defaultCfg, cfgPath, plugs, defaultPlatformKeyPath} from './paths';
|
|
|
|
|
import {_init} from './init';
|
2019-11-28 05:17:01 -09:00
|
|
|
import notify from '../notify';
|
2023-06-26 01:29:50 -08:00
|
|
|
import type {rawConfig} from '../../lib/config';
|
2023-01-06 20:39:33 -09:00
|
|
|
import {migrateHyper3Config} from './migrate';
|
2017-06-02 16:03:47 -08:00
|
|
|
|
2020-04-27 05:32:08 -08:00
|
|
|
let defaultConfig: rawConfig;
|
2017-09-20 13:32:32 -08:00
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
const _importConf = () => {
|
2019-05-09 09:32:32 -08:00
|
|
|
// init plugin directories if not present
|
|
|
|
|
mkdirpSync(plugs.base);
|
2017-08-21 16:07:50 -08:00
|
|
|
mkdirpSync(plugs.local);
|
|
|
|
|
|
2019-05-09 09:32:32 -08:00
|
|
|
try {
|
2022-12-25 20:01:04 -09:00
|
|
|
migrateHyper3Config();
|
2019-05-09 09:32:32 -08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-25 20:01:04 -09:00
|
|
|
let defaultCfgRaw = '{}';
|
2017-06-02 16:03:47 -08:00
|
|
|
try {
|
2020-04-27 05:32:08 -08:00
|
|
|
defaultCfgRaw = readFileSync(defaultCfg, 'utf8');
|
2017-06-02 16:03:47 -08:00
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
2022-12-25 20:01:04 -09:00
|
|
|
const _defaultCfg = JSON.parse(defaultCfgRaw) as rawConfig;
|
2020-04-27 05:32:08 -08:00
|
|
|
|
|
|
|
|
// Importing platform specific keymap
|
|
|
|
|
let content = '{}';
|
|
|
|
|
try {
|
|
|
|
|
content = readFileSync(defaultPlatformKeyPath(), 'utf8');
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
}
|
|
|
|
|
const mapping = JSON.parse(content) as Record<string, string | string[]>;
|
|
|
|
|
_defaultCfg.keymaps = mapping;
|
|
|
|
|
|
|
|
|
|
// Import user config
|
2022-12-25 20:01:04 -09:00
|
|
|
let userCfg: rawConfig;
|
2020-04-27 05:32:08 -08:00
|
|
|
try {
|
2022-12-25 20:01:04 -09:00
|
|
|
userCfg = JSON.parse(readFileSync(cfgPath, 'utf8'));
|
2020-04-27 05:32:08 -08:00
|
|
|
} catch (err) {
|
2022-12-30 21:14:48 -09:00
|
|
|
notify("Couldn't parse config file. Using default config instead.");
|
2022-12-25 20:01:04 -09:00
|
|
|
userCfg = JSON.parse(defaultCfgRaw);
|
2020-04-27 05:32:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {userCfg, defaultCfg: _defaultCfg};
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const _import = () => {
|
2017-09-20 13:32:32 -08:00
|
|
|
const imported = _importConf();
|
2020-04-27 05:32:08 -08:00
|
|
|
defaultConfig = imported.defaultCfg;
|
2022-12-25 20:01:04 -09:00
|
|
|
const result = _init(imported.userCfg, imported.defaultCfg);
|
2017-11-03 15:42:25 -08:00
|
|
|
return result;
|
2017-06-02 16:03:47 -08:00
|
|
|
};
|
|
|
|
|
|
2019-11-28 05:17:01 -09:00
|
|
|
export const getDefaultConfig = () => {
|
2017-09-20 13:32:32 -08:00
|
|
|
if (!defaultConfig) {
|
2020-04-27 05:32:08 -08:00
|
|
|
defaultConfig = _importConf().defaultCfg;
|
2017-09-20 13:32:32 -08:00
|
|
|
}
|
|
|
|
|
return defaultConfig;
|
|
|
|
|
};
|