Skip to content

Commit

Permalink
ts-fix: disambiguate ElectronStore typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ArjixWasTaken committed Aug 1, 2024
1 parent 7b033b5 commit 8775735
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 42 deletions.
28 changes: 13 additions & 15 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Store from 'electron-store';
import { deepmergeCustom } from 'deepmerge-ts';

import defaultConfig from './defaults';

import store from './store';
import store, { IStore } from './store';
import plugins from './plugins';

import { restart } from '@/providers/app-controls';
Expand Down Expand Up @@ -62,20 +61,19 @@ type Join<K, P> = K extends string | number
type Paths<T, D extends number = 10> = [D] extends [never]
? never
: T extends object
? {
[K in keyof T]-?: K extends string | number
? `${K}` | Join<K, Paths<T[K], Prev[D]>>
: never;
}[keyof T]
: '';
? {
[K in keyof T]-?: K extends string | number
? `${K}` | Join<K, Paths<T[K], Prev[D]>>
: never;
}[keyof T]
: '';

type SplitKey<K> = K extends `${infer A}.${infer B}` ? [A, B] : [K, string];
type PathValue<T, K extends string> = SplitKey<K> extends [
infer A extends keyof T,
infer B extends string,
]
? PathValue<T[A], B>
: T;
type PathValue<T, K extends string> =
SplitKey<K> extends [infer A extends keyof T, infer B extends string]
? PathValue<T[A], B>
: T;

const get = <Key extends Paths<typeof defaultConfig>>(key: Key) =>
store.get(key) as PathValue<typeof defaultConfig, typeof key>;

Expand All @@ -86,7 +84,7 @@ export default {
setPartial,
setMenuOption,
edit: () => store.openInEditor(),
watch(cb: Parameters<Store['onDidAnyChange']>[0]) {
watch(cb: Parameters<IStore['onDidAnyChange']>[0]) {
store.onDidAnyChange(cb);
},
plugins,
Expand Down
56 changes: 29 additions & 27 deletions src/config/store.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Store from 'electron-store';
import Conf from 'conf';

import defaults from './defaults';

import { DefaultPresetList, type Preset } from '@/plugins/downloader/types';

// prettier-ignore
export type IStore = InstanceType<typeof import('conf/dist/source/index').default<Record<string, unknown>>>;

const migrations = {
'>=3.3.0'(store: Conf<Record<string, unknown>>) {
'>=3.3.0'(store: IStore) {
const lastfmConfig = store.get('plugins.lastfm') as {
enabled?: boolean;
token?: string;
Expand All @@ -16,21 +18,21 @@ const migrations = {
secret?: string;
};
if (lastfmConfig) {
let scrobblerConfig = store.get(
'plugins.scrobbler',
) as {
enabled?: boolean;
scrobblers?: {
lastfm?: {
let scrobblerConfig = store.get('plugins.scrobbler') as
| {
enabled?: boolean;
token?: string;
sessionKey?: string;
apiRoot?: string;
apiKey?: string;
secret?: string;
};
};
} | undefined;
scrobblers?: {
lastfm?: {
enabled?: boolean;
token?: string;
sessionKey?: string;
apiRoot?: string;
apiKey?: string;
secret?: string;
};
};
}
| undefined;

if (!scrobblerConfig) {
scrobblerConfig = {
Expand All @@ -56,7 +58,7 @@ const migrations = {
store.delete('plugins.lastfm');
}
},
'>=3.0.0'(store: Conf<Record<string, unknown>>) {
'>=3.0.0'(store: IStore) {
const discordConfig = store.get('plugins.discord') as Record<
string,
unknown
Expand All @@ -78,14 +80,14 @@ const migrations = {
}
}
},
'>=2.1.3'(store: Conf<Record<string, unknown>>) {
'>=2.1.3'(store: IStore) {
const listenAlong = store.get('plugins.discord.listenAlong');
if (listenAlong !== undefined) {
store.set('plugins.discord.playOnYouTubeMusic', listenAlong);
store.delete('plugins.discord.listenAlong');
}
},
'>=2.1.0'(store: Conf<Record<string, unknown>>) {
'>=2.1.0'(store: IStore) {
const originalPreset = store.get('plugins.downloader.preset') as
| string
| undefined;
Expand All @@ -110,7 +112,7 @@ const migrations = {
store.delete('plugins.downloader.ffmpegArgs');
}
},
'>=1.20.0'(store: Conf<Record<string, unknown>>) {
'>=1.20.0'(store: IStore) {
store.delete('plugins.visualizer'); // default value is now in the plugin

if (store.get('plugins.notifications.toastStyle') === undefined) {
Expand All @@ -125,14 +127,14 @@ const migrations = {
store.set('options.likeButtons', 'force');
}
},
'>=1.17.0'(store: Conf<Record<string, unknown>>) {
'>=1.17.0'(store: IStore) {
store.delete('plugins.picture-in-picture'); // default value is now in the plugin

if (store.get('plugins.video-toggle.mode') === undefined) {
store.set('plugins.video-toggle.mode', 'custom');
}
},
'>=1.14.0'(store: Conf<Record<string, unknown>>) {
'>=1.14.0'(store: IStore) {
if (
typeof store.get('plugins.precise-volume.globalShortcuts') !== 'object'
) {
Expand All @@ -144,12 +146,12 @@ const migrations = {
store.set('plugins.video-toggle.enabled', true);
}
},
'>=1.13.0'(store: Conf<Record<string, unknown>>) {
'>=1.13.0'(store: IStore) {
if (store.get('plugins.discord.listenAlong') === undefined) {
store.set('plugins.discord.listenAlong', true);
}
},
'>=1.12.0'(store: Conf<Record<string, unknown>>) {
'>=1.12.0'(store: IStore) {
const options = store.get('plugins.shortcuts') as
| Record<
string,
Expand Down Expand Up @@ -187,12 +189,12 @@ const migrations = {
}
}
},
'>=1.11.0'(store: Conf<Record<string, unknown>>) {
'>=1.11.0'(store: IStore) {
if (store.get('options.resumeOnStart') === undefined) {
store.set('options.resumeOnStart', true);
}
},
'>=1.7.0'(store: Conf<Record<string, unknown>>) {
'>=1.7.0'(store: IStore) {
const enabledPlugins = store.get('plugins') as string[];
if (!Array.isArray(enabledPlugins)) {
console.warn('Plugins are not in array format, cannot migrate');
Expand Down Expand Up @@ -233,4 +235,4 @@ export default new Store({
},
clearInvalidConfig: false,
migrations,
});
}) as Store & IStore;

0 comments on commit 8775735

Please sign in to comment.