From 377353e76fd43f140ce8063db528e53fa884ba4b Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Thu, 9 May 2019 17:32:32 +0000 Subject: [PATCH 1/4] Simplify migration to avoid handling complicated edge cases (#3643) --- app/config/import.js | 69 +++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/app/config/import.js b/app/config/import.js index bfd2e81e..c779d72c 100644 --- a/app/config/import.js +++ b/app/config/import.js @@ -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 {defaultCfg, cfgPath, legacyCfgPath, plugs, defaultPlatformKeyPath} = require('./paths'); 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'); }; -const migrate = (old, _new, oldBackupPath) => { - if (old === _new) { +// Migrate Hyper2 config to Hyper3 but only if the user hasn't manually +// touched the new config and if the old config is not a symlink +const migrateHyper2Config = () => { + if (cfgPath === legacyCfgPath) { + // No need to migrate return; } - if (existsSync(old)) { - //eslint-disable-next-line no-console - console.log('Found legacy config. Migrating ', old, '->', _new); - if (existsSync(_new)) { - saveAsBackup(_new); - } - copySync(old, _new); - saveAsBackup(oldBackupPath || old); - return true; + if (!existsSync(legacyCfgPath)) { + // Already migrated or user never used Hyper 2 + return; } - 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() { // init plugin directories if not present 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); + try { + migrateHyper2Config(); + } catch (err) { + //eslint-disable-next-line no-console + console.error(err); + } + try { const defaultCfgRaw = readFileSync(defaultCfg, 'utf8'); const _defaultCfg = _extractDefault(defaultCfgRaw); From efeedd0a9d3fe8126df9402bb326206f0a792147 Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Thu, 9 May 2019 13:33:12 -0400 Subject: [PATCH 2/4] 3.0.1-canary.3 --- app/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/package.json b/app/package.json index 0e98e1bc..745849df 100644 --- a/app/package.json +++ b/app/package.json @@ -2,7 +2,7 @@ "name": "hyper", "productName": "Hyper", "description": "A terminal built on web technologies", - "version": "3.0.1-canary.2", + "version": "3.0.1-canary.3", "license": "MIT", "author": { "name": "ZEIT, Inc.", From 6a05b7307c6b4b9de36ead754be735b0b9748c65 Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Thu, 9 May 2019 18:31:11 +0000 Subject: [PATCH 3/4] Ignore line endings when comparing config file against default (#3645) --- app/config/import.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/app/config/import.js b/app/config/import.js index c779d72c..678dc3d6 100644 --- a/app/config/import.js +++ b/app/config/import.js @@ -55,16 +55,20 @@ const migrateHyper2Config = () => { 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; + if (existsNew) { + const cfg1 = readFileSync(defaultCfg, 'utf8').replace(/\r|\n/g, ''); + const cfg2 = readFileSync(cfgPath, 'utf8').replace(/\r|\n/g, ''); + const hasNewConfigBeenTouched = cfg1 !== cfg2; + 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 From ef14af49eb9a9be23a4b6f4f5791260c5e5f5278 Mon Sep 17 00:00:00 2001 From: Juan Campa Date: Thu, 9 May 2019 15:12:10 -0400 Subject: [PATCH 4/4] 3.0.1-canary.4 --- app/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/package.json b/app/package.json index 745849df..bd90baa7 100644 --- a/app/package.json +++ b/app/package.json @@ -2,7 +2,7 @@ "name": "hyper", "productName": "Hyper", "description": "A terminal built on web technologies", - "version": "3.0.1-canary.3", + "version": "3.0.1-canary.4", "license": "MIT", "author": { "name": "ZEIT, Inc.",