hyper/app/config/import.ts

116 lines
3 KiB
TypeScript
Raw Normal View History

2022-12-25 20:01:04 -09:00
import {copySync, existsSync, writeFileSync, readFileSync, copy} from 'fs-extra';
2019-11-28 05:17:01 -09:00
import {sync as mkdirpSync} from 'mkdirp';
2022-12-25 20:01:04 -09:00
import {
defaultCfg,
cfgPath,
legacyCfgPath,
plugs,
defaultPlatformKeyPath,
schemaPath,
cfgDir,
schemaFile
} from './paths';
2019-11-28 05:17:01 -09:00
import {_init, _extractDefault} from './init';
import notify from '../notify';
2020-04-27 05:32:08 -08:00
import {rawConfig} from '../../lib/config';
2022-12-25 20:01:04 -09:00
import _ from 'lodash';
import {resolve} from 'path';
2020-04-27 05:32:08 -08:00
let defaultConfig: rawConfig;
const _write = (path: string, data: string) => {
// This method will take text formatted as Unix line endings and transform it
// to text formatted with DOS line endings. We do this because the default
// text editor on Windows (notepad) doesn't Deal with LF files. Still. In 2017.
const crlfify = (str: string) => {
return str.replace(/\r?\n/g, '\r\n');
};
const format = process.platform === 'win32' ? crlfify(data.toString()) : data;
writeFileSync(path, format, 'utf8');
};
2022-12-25 20:01:04 -09:00
// Migrate Hyper3 config to Hyper4 but only if the user hasn't manually
// touched the new config and if the old config is not a symlink
const migrateHyper3Config = () => {
copy(schemaPath, resolve(cfgDir, schemaFile), (err) => {
if (err) {
console.error(err);
}
2022-12-25 20:01:04 -09:00
});
2022-12-25 20:01:04 -09:00
if (existsSync(cfgPath)) {
return;
}
2022-12-25 20:01:04 -09:00
if (!existsSync(legacyCfgPath)) {
2022-12-25 20:01:04 -09:00
copySync(defaultCfg, cfgPath);
return;
}
// Migrate
2022-12-25 20:01:04 -09:00
const defaultCfgData = JSON.parse(readFileSync(defaultCfg, 'utf8'));
const legacyCfgData = _extractDefault(readFileSync(legacyCfgPath, 'utf8'));
const newCfgData = _.merge(defaultCfgData, legacyCfgData);
_write(cfgPath, JSON.stringify(newCfgData, null, 2));
notify(
2022-12-25 20:01:04 -09:00
'Hyper 4',
`Settings location and format has changed.\nWe've automatically migrated your existing config to ${cfgPath}`
);
};
2019-11-28 05:17:01 -09:00
const _importConf = () => {
// init plugin directories if not present
mkdirpSync(plugs.base);
mkdirpSync(plugs.local);
try {
2022-12-25 20:01:04 -09:00
migrateHyper3Config();
} catch (err) {
console.error(err);
}
2022-12-25 20:01:04 -09:00
let defaultCfgRaw = '{}';
try {
2020-04-27 05:32:08 -08:00
defaultCfgRaw = readFileSync(defaultCfg, 'utf8');
} 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) {
_write(cfgPath, defaultCfgRaw);
2022-12-25 20:01:04 -09:00
userCfg = JSON.parse(defaultCfgRaw);
2020-04-27 05:32:08 -08:00
}
return {userCfg, defaultCfg: _defaultCfg};
};
2019-11-28 05:17:01 -09:00
export const _import = () => {
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);
return result;
};
2019-11-28 05:17:01 -09:00
export const getDefaultConfig = () => {
if (!defaultConfig) {
2020-04-27 05:32:08 -08:00
defaultConfig = _importConf().defaultCfg;
}
return defaultConfig;
};