From da494982dd3877f15ba45ad7d7b21f563fb5aba7 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 24 May 2021 21:07:39 +0800 Subject: [PATCH] feat: disable copy snippet button for github.com --- src/utils/config/migrations/2.6.0.ts | 31 ++++++++++---------------- src/utils/config/migrations/3.0.0.ts | 33 ++++++++++++++++++++++++++++ src/utils/config/migrations/index.ts | 16 +++++++++++++- 3 files changed, 60 insertions(+), 20 deletions(-) create mode 100644 src/utils/config/migrations/3.0.0.ts diff --git a/src/utils/config/migrations/2.6.0.ts b/src/utils/config/migrations/2.6.0.ts index ff3cca5..759cc30 100644 --- a/src/utils/config/migrations/2.6.0.ts +++ b/src/utils/config/migrations/2.6.0.ts @@ -1,7 +1,5 @@ import { storageHelper } from 'utils/storageHelper' -import { Migration } from '.' -import { Storage } from '../../storageHelper' -import { VersionedConfig } from '../helper' +import { Migration, onConfigOutdated } from '.' export const migration: Migration = { version: '2.6.0', @@ -13,29 +11,24 @@ export const migration: Migration = { accessToken?: string } - const config = await storageHelper.get & Storage>() - if (config && config.configVersion < version) { - const { configVersion, ...restConfig } = config - for (const key of Object.keys(restConfig)) { + onConfigOutdated(version, async configs => { + for (const key of Object.keys(configs)) { if ( - typeof restConfig[key] === 'object' && - restConfig[key] && - 'access_token' in restConfig[key] + typeof configs[key] === 'object' && + configs[key] !== null && + 'access_token' in configs[key] ) { - const config: ConfigBeforeMigrate = restConfig[key] - const { access_token: accessToken, ...legacy } = config - const migrated: ConfigAfterMigrate = { - ...legacy, + const configBeforeMigrate: ConfigBeforeMigrate = configs[key] + const { access_token: accessToken, ...rest } = configBeforeMigrate + const configAfterMigrate: ConfigAfterMigrate = { + ...rest, accessToken, } await storageHelper.set({ - [key]: migrated, + [key]: configAfterMigrate, }) } } - await storageHelper.set({ - configVersion: version, - }) - } + }) }, } diff --git a/src/utils/config/migrations/3.0.0.ts b/src/utils/config/migrations/3.0.0.ts new file mode 100644 index 0000000..2ff94fe --- /dev/null +++ b/src/utils/config/migrations/3.0.0.ts @@ -0,0 +1,33 @@ +import { storageHelper } from 'utils/storageHelper' +import { Migration, onConfigOutdated } from '.' + +export const migration: Migration = { + version: '3.0.0', + async migrate(version) { + // disable copy snippet button for github.com + type ConfigBeforeMigrate = { + copySnippetButton: boolean + } + type ConfigAfterMigrate = { + copySnippetButton: boolean + } + + onConfigOutdated(version, async configs => { + const key = 'platform_github.com' + const config = configs[key] + if (typeof config === 'object' && config !== null && 'copySnippetButton' in config) { + const configBeforeMigrate: ConfigBeforeMigrate = config + const { copySnippetButton, ...rest } = configBeforeMigrate + if (copySnippetButton) { + const configAfterMigrate: ConfigAfterMigrate = { + ...rest, + copySnippetButton: false, + } + await storageHelper.set({ + [key]: configAfterMigrate, + }) + } + } + }) + }, +} diff --git a/src/utils/config/migrations/index.ts b/src/utils/config/migrations/index.ts index 7daa138..bdcb137 100644 --- a/src/utils/config/migrations/index.ts +++ b/src/utils/config/migrations/index.ts @@ -3,6 +3,7 @@ import { Storage } from '../../storageHelper' import { migration as v1v0v1 } from './1.0.1' import { migration as v1v3v4 } from './1.3.4' import { migration as v2v6v0 } from './2.6.0' +import { migration as v3v0v0 } from './3.0.0' export type Migration = { version: string @@ -10,9 +11,22 @@ export type Migration = { } export async function migrateConfig() { - const migrations: Migration[] = [v1v0v1, v1v3v4, v2v6v0] + const migrations: Migration[] = [v1v0v1, v1v3v4, v2v6v0, v3v0v0] for (const { version, migrate } of migrations) { await migrate(version) } } + +export async function onConfigOutdated( + configVersion: string, + runIfOutdated: (config: T) => Async, +) { + const config = await storageHelper.get() + + if (config && config.configVersion < configVersion) { + const { configVersion, ...restConfig } = config + runIfOutdated(restConfig as T) + await storageHelper.set({ configVersion }) + } +}