mirror of
https://github.com/quine-global/hyper.git
synced 2026-01-13 04:28:41 -09:00
Simplify migration to avoid handling complicated edge cases (#3643)
This commit is contained in:
parent
f73a23055b
commit
377353e76f
1 changed files with 43 additions and 26 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
const {moveSync, copySync, existsSync, writeFileSync, readFileSync} = require('fs-extra');
|
const {moveSync, copySync, existsSync, writeFileSync, readFileSync, lstatSync} = require('fs-extra');
|
||||||
const {sync: mkdirpSync} = require('mkdirp');
|
const {sync: mkdirpSync} = require('mkdirp');
|
||||||
const {defaultCfg, cfgPath, legacyCfgPath, plugs, defaultPlatformKeyPath} = require('./paths');
|
const {defaultCfg, cfgPath, legacyCfgPath, plugs, defaultPlatformKeyPath} = require('./paths');
|
||||||
const {_init, _extractDefault} = require('./init');
|
const {_init, _extractDefault} = require('./init');
|
||||||
|
|
@ -37,41 +37,58 @@ const saveAsBackup = src => {
|
||||||
throw new Error('Failed to create backup for config file. Too many backups');
|
throw new Error('Failed to create backup for config file. Too many backups');
|
||||||
};
|
};
|
||||||
|
|
||||||
const migrate = (old, _new, oldBackupPath) => {
|
// Migrate Hyper2 config to Hyper3 but only if the user hasn't manually
|
||||||
if (old === _new) {
|
// touched the new config and if the old config is not a symlink
|
||||||
|
const migrateHyper2Config = () => {
|
||||||
|
if (cfgPath === legacyCfgPath) {
|
||||||
|
// No need to migrate
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (existsSync(old)) {
|
if (!existsSync(legacyCfgPath)) {
|
||||||
//eslint-disable-next-line no-console
|
// Already migrated or user never used Hyper 2
|
||||||
console.log('Found legacy config. Migrating ', old, '->', _new);
|
return;
|
||||||
if (existsSync(_new)) {
|
|
||||||
saveAsBackup(_new);
|
|
||||||
}
|
|
||||||
copySync(old, _new);
|
|
||||||
saveAsBackup(oldBackupPath || old);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
const existsNew = existsSync(cfgPath);
|
||||||
|
if (lstatSync(legacyCfgPath).isSymbolicLink() || (existsNew && lstatSync(cfgPath).isSymbolicLink())) {
|
||||||
|
// One of the files is a symlink, there could be a number of complications
|
||||||
|
// in this case so let's avoid those and not do automatic migration
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasNewConfigBeenTouched = existsNew && readFileSync(cfgPath, 'utf8') !== readFileSync(defaultCfg, 'utf8');
|
||||||
|
if (hasNewConfigBeenTouched) {
|
||||||
|
// Assume the user has migrated manually but rename old config to .backup so
|
||||||
|
// we don't keep trying to migrate on every launch
|
||||||
|
const backupPath = saveAsBackup(legacyCfgPath);
|
||||||
|
notify(
|
||||||
|
'Hyper 3',
|
||||||
|
`Settings location has changed to ${cfgPath}.\nWe've backed up your old Hyper config to ${backupPath}`
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Migrate
|
||||||
|
copySync(legacyCfgPath, cfgPath);
|
||||||
|
saveAsBackup(legacyCfgPath);
|
||||||
|
|
||||||
|
notify(
|
||||||
|
'Hyper 3',
|
||||||
|
`Settings location has changed to ${cfgPath}.\nWe've automatically migrated your existing config!\nPlease restart Hyper now`
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const _importConf = function() {
|
const _importConf = function() {
|
||||||
// init plugin directories if not present
|
// init plugin directories if not present
|
||||||
mkdirpSync(plugs.base);
|
mkdirpSync(plugs.base);
|
||||||
|
|
||||||
// Migrate Hyper2 config to Hyper3
|
|
||||||
const migratedConfig = migrate(legacyCfgPath, cfgPath);
|
|
||||||
const migratedPlugins = migrate(plugs.legacyLocal, plugs.local, plugs.legacyBase);
|
|
||||||
if (migratedConfig || migratedPlugins) {
|
|
||||||
notify(
|
|
||||||
'Hyper 3',
|
|
||||||
`Settings location has changed to ${cfgPath}.\nWe've automatically migrated your existing config!\nPlease restart hyper`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run this after the migration so that we don't generate a ".backup" file for
|
|
||||||
// an empty local/ directory
|
|
||||||
mkdirpSync(plugs.local);
|
mkdirpSync(plugs.local);
|
||||||
|
|
||||||
|
try {
|
||||||
|
migrateHyper2Config();
|
||||||
|
} catch (err) {
|
||||||
|
//eslint-disable-next-line no-console
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const defaultCfgRaw = readFileSync(defaultCfg, 'utf8');
|
const defaultCfgRaw = readFileSync(defaultCfg, 'utf8');
|
||||||
const _defaultCfg = _extractDefault(defaultCfgRaw);
|
const _defaultCfg = _extractDefault(defaultCfgRaw);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue