chore: Move files around to better places

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton committed 2023-05-31 16:21:35 -04:00
1 parent 34adfcd20b
commit 0c1e822788
30 files changed
+37 -37

No files matched your search

@@ -24,7 +24,7 @@ import writeFileSyncInjectable from "../../../common/fs/write-file-sync.injectab
import type { WriteBufferSync } from "../../../common/fs/write-buffer-sync.injectable";
import writeBufferSyncInjectable from "../../../common/fs/write-buffer-sync.injectable";
import clustersPersistentStorageInjectable from "./common/storage.injectable";
import type { PersistentStorage } from "../../../common/persistent-storage/create.injectable";
import type { PersistentStorage } from "../../persistent-storage/common/create.injectable";
import type { AddCluster } from "./common/add.injectable";
import addClusterInjectable from "./common/add.injectable";
import type { GetClusterById } from "./common/get-by-id.injectable";
@@ -4,7 +4,7 @@
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { MigrationDeclaration } from "../../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration } from "../../../persistent-storage/common/migrations.injectable";
export const clusterStoreMigrationInjectionToken = getInjectionToken<MigrationDeclaration>({
id: "cluster-store-migration",
@@ -9,8 +9,8 @@ import { clusterStoreMigrationInjectionToken } from "./migration-token";
import type { ClusterId, ClusterModel } from "../../../../common/cluster-types";
import { Cluster } from "../../../../common/cluster/cluster";
import { loggerInjectionToken } from "@k8slens/logger";
import createPersistentStorageInjectable from "../../../../common/persistent-storage/create.injectable";
import persistentStorageMigrationsInjectable from "../../../../common/persistent-storage/migrations.injectable";
import createPersistentStorageInjectable from "../../../persistent-storage/common/create.injectable";
import persistentStorageMigrationsInjectable from "../../../persistent-storage/common/migrations.injectable";
import storeMigrationVersionInjectable from "../../../../common/vars/store-migration-version.injectable";
import clustersStateInjectable from "./state.injectable";
@@ -6,9 +6,9 @@ import type { LensExtensionId } from "@k8slens/legacy-extensions";
import { isDefined } from "@k8slens/utilities";
import { getInjectable } from "@ogre-tools/injectable";
import { action, toJS } from "mobx";
import createPersistentStorageInjectable from "../../../../common/persistent-storage/create.injectable";
import persistentStorageMigrationsInjectable from "../../../../common/persistent-storage/migrations.injectable";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "./migrations";
import createPersistentStorageInjectable from "../../../persistent-storage/common/create.injectable";
import persistentStorageMigrationsInjectable from "../../../persistent-storage/common/migrations.injectable";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "../main/migrations";
import type { LensExtensionState } from "./state.injectable";
import enabledExtensionsStateInjectable from "./state.injectable";
import { enabledExtensionsPersistentStorageVersionInitializable } from "./storage-version";
@@ -4,7 +4,7 @@
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { MigrationDeclaration } from "../../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration } from "../../../persistent-storage/common/migrations.injectable";
export const enabledExtensionsMigrationDeclarationInjectionToken = getInjectionToken<MigrationDeclaration>({
id: "enabled-extensions-migration-declaration",
@@ -2,8 +2,8 @@
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import storageMigrationVersionInjectable from "../../../../common/persistent-storage/storage-migration-version.injectable";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "../common/migrations";
import storageMigrationVersionInjectable from "../../../persistent-storage/main/storage-migration-version.injectable";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "./migrations";
import { enabledExtensionsPersistentStorageVersionInitializable } from "../common/storage-version";
import { getInjectable } from "@ogre-tools/injectable";
@@ -4,7 +4,7 @@
*/
import { isObject } from "@k8slens/utilities";
import { getInjectable } from "@ogre-tools/injectable";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "../common/migrations";
import { enabledExtensionsMigrationDeclarationInjectionToken } from "./migrations";
const enabledExtensionsMigrationV650Injectable = getInjectable({
id: "enabled-extensions-migration-v650",
@@ -4,7 +4,7 @@
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { MigrationDeclaration } from "../../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration } from "../../../persistent-storage/common/migrations.injectable";
export const hotbarStoreMigrationInjectionToken = getInjectionToken<MigrationDeclaration>({
id: "hotbar-store-migration-token",
@@ -8,8 +8,8 @@ import { action, comparer } from "mobx";
import catalogCatalogEntityInjectable from "../../../../common/catalog-entities/general-catalog-entities/implementations/catalog-catalog-entity.injectable";
import { hotbarStoreMigrationInjectionToken } from "./migrations-token";
import { defaultHotbarCells } from "./types";
import createPersistentStorageInjectable from "../../../../common/persistent-storage/create.injectable";
import persistentStorageMigrationsInjectable from "../../../../common/persistent-storage/migrations.injectable";
import createPersistentStorageInjectable from "../../../persistent-storage/common/create.injectable";
import persistentStorageMigrationsInjectable from "../../../persistent-storage/common/migrations.injectable";
import storeMigrationVersionInjectable from "../../../../common/vars/store-migration-version.injectable";
import activeHotbarIdInjectable from "./active-id.injectable";
import createHotbarInjectable from "./create-hotbar.injectable";
@@ -3,7 +3,7 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import welcomeCatalogEntityInjectable from "../../../../common/catalog-entities/general-catalog-entities/implementations/welcome-catalog-entity.injectable";
import type { MigrationDeclaration, MigrationStore } from "../../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration, MigrationStore } from "../../../persistent-storage/common/migrations.injectable";
import { getDiForUnitTesting } from "../../../../main/getDiForUnitTesting";
import type { HotbarData } from "../common/hotbar";
import type { HotbarItem } from "../common/types";
@@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export interface IpcChannelPrefixes {
readonly local: string;
readonly remote: string;
}
export const persistentStorageIpcChannelPrefixesInjectionToken = getInjectionToken<IpcChannelPrefixes>({
id: "persistent-storage-ipc-channel-prefix-token",
});
@@ -0,0 +1,158 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { disposer, isPromiseLike } from "@k8slens/utilities";
import { getInjectable } from "@ogre-tools/injectable";
import type { Options } from "conf/dist/source";
import { isEqual, kebabCase } from "lodash";
import type { IEqualsComparer } from "mobx";
import { reaction } from "mobx";
import { loggerInjectionToken } from "@k8slens/logger";
import { enlistMessageChannelListenerInjectionToken, sendMessageToChannelInjectionToken } from "@k8slens/messaging";
import type { MessageChannel } from "@k8slens/messaging";
import { persistentStorageIpcChannelPrefixesInjectionToken } from "./channel-prefix";
import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "./disable-sync";
import { persistStateToConfigInjectionToken } from "./save-to-file";
import type { Migrations } from "./migrations.injectable";
import { nextTick } from "process";
import directoryForUserDataInjectable from "../../../common/app-paths/directory-for-user-data/directory-for-user-data.injectable";
import getConfigurationFileModelInjectable from "../../../common/get-configuration-file-model/get-configuration-file-model.injectable";
import getBasenameOfPathInjectable from "../../../common/path/get-basename.injectable";
export interface PersistentStorage {
/**
* This method does the initial synchronous load from disk and then starts writing the state
* back to disk whenever it changes.
*/
loadAndStartSyncing: () => void;
}
export interface PersistentStorageParams<T extends object> extends Omit<Options<T>, "migrations"> {
readonly syncOptions?: {
readonly fireImmediately?: boolean;
equals?: IEqualsComparer<T>;
};
readonly configName: string;
readonly migrations?: Migrations;
/**
* fromStore is called internally when a child class syncs with the file
* system.
*
* Note: This function **must** be synchronous.
*
* @param data the parsed information read from the stored JSON file
*/
fromStore(data: Partial<T>): void;
/**
* toJSON is called when syncing the store to the filesystem. It should
* produce a JSON serializable object representation of the current state.
*
* It is recommended that a round trip is valid. Namely, calling
* `this.fromStore(this.toJSON())` shouldn't change the state.
*/
toJSON(): T;
}
export type CreatePersistentStorage = <T extends object>(params: PersistentStorageParams<T>) => PersistentStorage;
const createPersistentStorageInjectable = getInjectable({
id: "create-persistent-storage",
instantiate: (di): CreatePersistentStorage => {
const directoryForUserData = di.inject(directoryForUserDataInjectable);
const getConfigurationFileModel = di.inject(getConfigurationFileModelInjectable);
const logger = di.inject(loggerInjectionToken);
const getBasenameOfPath = di.inject(getBasenameOfPathInjectable);
const ipcChannelPrefixes = di.inject(persistentStorageIpcChannelPrefixesInjectionToken);
const persistStateToConfig = di.inject(persistStateToConfigInjectionToken);
const enlistMessageChannelListener = di.inject(enlistMessageChannelListenerInjectionToken);
const shouldDisableSyncInListener = di.inject(shouldPersistentStorageDisableSyncInIpcListenerInjectionToken);
const sendMessageToChannel = di.inject(sendMessageToChannelInjectionToken);
return <T extends object>(rawParams: PersistentStorageParams<T>) => {
const {
fromStore,
toJSON,
syncOptions,
migrations,
cwd = directoryForUserData,
...params
} = rawParams;
const displayName = kebabCase(params.configName).toUpperCase();
const loadAndStartSyncing = () => {
logger.info(`[${displayName}]: LOADING ...`);
const config = getConfigurationFileModel({
projectName: "lens",
cwd,
migrations: migrations as Options<T>["migrations"],
...params,
});
const res = fromStore(config.store);
if (isPromiseLike(res)) {
logger.error(`${displayName} extends BaseStore<T>'s fromStore method returns a Promise or promise-like object. This is an error and must be fixed.`);
}
logger.info(`[${displayName}]: LOADED from ${config.path}`);
const syncDisposers = disposer();
const sendChannel: MessageChannel<T> = {
id: `${ipcChannelPrefixes.remote}:${config.path}`,
};
const receiveChannel: MessageChannel<T> = {
id: `${ipcChannelPrefixes.local}:${config.path}`,
};
const name = getBasenameOfPath(config.path);
const disableSync = () => syncDisposers();
const enableSync = () => {
syncDisposers.push(
reaction(
() => toJSON(),
model => {
persistStateToConfig(config, model);
sendMessageToChannel(sendChannel, model);
},
syncOptions,
),
enlistMessageChannelListener({
id: "persistent-storage-sync",
channel: receiveChannel,
handler: (model) => {
logger.silly(`[${displayName}]: syncing ${name}`, { model });
if (shouldDisableSyncInListener) {
disableSync();
}
// todo: use "resourceVersion" if merge required (to avoid equality checks => better performance)
if (!isEqual(toJSON(), model)) {
fromStore(model);
}
if (shouldDisableSyncInListener) {
nextTick(() => {
enableSync();
});
}
},
}),
);
};
enableSync();
};
return {
loadAndStartSyncing,
};
};
},
});
export default createPersistentStorageInjectable;
@@ -0,0 +1,10 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
export const shouldPersistentStorageDisableSyncInIpcListenerInjectionToken = getInjectionToken<boolean>({
id: "should-persistent-storage-disable-sync-in-ipc-listener-token",
});
@@ -0,0 +1,58 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { InjectionToken } from "@ogre-tools/injectable";
import { lifecycleEnum, getInjectable } from "@ogre-tools/injectable";
import { loggerInjectionToken } from "@k8slens/logger";
import { getOrInsert, iter, object } from "@k8slens/utilities";
export type AllowedSetValue<T> = T extends (...args: any[]) => any
? never
: T extends undefined | symbol
? never
: T;
export interface MigrationStore {
readonly path: string;
get(key: string): unknown;
delete(key: string): void;
has(key: string): boolean;
clear(): void;
set<T>(key: string, value: AllowedSetValue<T>): void;
}
export type Migrations = Partial<Record<string, (store: MigrationStore) => void>>;
export interface MigrationDeclaration {
version: string;
run(store: MigrationStore): void;
}
const persistentStorageMigrationsInjectable = getInjectable({
id: "persistent-storage-migrations",
instantiate: (di, token) => {
const logger = di.inject(loggerInjectionToken);
const declarations = di.injectMany(token);
const migrations = new Map<string, MigrationDeclaration["run"][]>();
for (const decl of declarations) {
getOrInsert(migrations, decl.version, []).push(decl.run);
}
return iter.chain(migrations.entries())
.map(([v, fns]) => [v, (store: MigrationStore) => {
logger.info(`Running ${v} migration for ${store.path}`);
for (const fn of fns) {
fn(store);
}
}] as const)
.collect(object.fromEntries);
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, token: InjectionToken<MigrationDeclaration, void>) => token.id,
}),
});
export default persistentStorageMigrationsInjectable;
@@ -0,0 +1,12 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type Config from "conf";
export type PersistStateToConfig = <T extends object>(config: Config<T>, state: T) => void;
export const persistStateToConfigInjectionToken = getInjectionToken<PersistStateToConfig>({
id: "persist-state-to-config-token",
});
@@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "../common/disable-sync";
const shouldBaseStoreDisableSyncInIpcListenerInjectable = getInjectable({
id: "should-base-store-disable-sync-in-ipc-listener",
instantiate: () => false,
injectionToken: shouldPersistentStorageDisableSyncInIpcListenerInjectionToken,
});
export default shouldBaseStoreDisableSyncInIpcListenerInjectable;
@@ -0,0 +1,17 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { persistentStorageIpcChannelPrefixesInjectionToken } from "../common/channel-prefix";
const baseStoreIpcChannelPrefixInjectable = getInjectable({
id: "base-store-ipc-channel-prefix",
instantiate: () => ({
local: "store-sync-main",
remote: "store-sync-renderer",
}),
injectionToken: persistentStorageIpcChannelPrefixesInjectionToken,
});
export default baseStoreIpcChannelPrefixInjectable;
@@ -0,0 +1,25 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { loggerInjectionToken } from "@k8slens/logger";
import { persistStateToConfigInjectionToken } from "../common/save-to-file";
const persistStateToConfigInjectable = getInjectable({
id: "persist-state-to-config",
instantiate: (di) => {
const logger = di.inject(loggerInjectionToken);
return (config, state) => {
logger.info(`[BASE-STORE]: saving ${config.path}...`);
for (const [key, value] of Object.entries(state)) {
config.set(key, value);
}
};
},
injectionToken: persistStateToConfigInjectionToken,
});
export default persistStateToConfigInjectable;
@@ -0,0 +1,32 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import type { InjectionToken } from "@ogre-tools/injectable";
import { getInjectable, lifecycleEnum } from "@ogre-tools/injectable";
import * as semver from "semver";
import type { MigrationDeclaration } from "../common/migrations.injectable";
/**
* NOTE: not all stores can use this computed version, namely if any migration uses a range for
* the version selector.
*/
const storageMigrationVersionInjectable = getInjectable({
id: "storage-migration-version",
instantiate: (di, token) => {
const declarations = di.injectMany(token);
return declarations.reduce((version, decl) => {
if (semver.gte(decl.version, version)) {
return decl.version;
}
return version;
}, "1.0.0");
},
lifecycle: lifecycleEnum.keyedSingleton({
getInstanceKey: (di, token: InjectionToken<MigrationDeclaration, void>) => token.id,
}),
});
export default storageMigrationVersionInjectable;
@@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { shouldPersistentStorageDisableSyncInIpcListenerInjectionToken } from "../common/disable-sync";
const shouldBaseStoreDisableSyncInIpcListenerInjectable = getInjectable({
id: "should-base-store-disable-sync-in-ipc-listener",
instantiate: () => true,
injectionToken: shouldPersistentStorageDisableSyncInIpcListenerInjectionToken,
});
export default shouldBaseStoreDisableSyncInIpcListenerInjectable;
@@ -0,0 +1,17 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { persistentStorageIpcChannelPrefixesInjectionToken } from "../common/channel-prefix";
const baseStoreIpcChannelPrefixInjectable = getInjectable({
id: "base-store-ipc-channel-prefix",
instantiate: () => ({
local: "store-sync-renderer",
remote: "store-sync-main",
}),
injectionToken: persistentStorageIpcChannelPrefixesInjectionToken,
});
export default baseStoreIpcChannelPrefixInjectable;
@@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { noop } from "@k8slens/utilities";
import { persistStateToConfigInjectionToken } from "../common/save-to-file";
const persistStateToConfigInjectable = getInjectable({
id: "persist-state-to-config",
instantiate: () => noop,
injectionToken: persistStateToConfigInjectionToken,
});
export default persistStateToConfigInjectable;
@@ -4,7 +4,7 @@
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { MigrationDeclaration } from "../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration } from "../../persistent-storage/common/migrations.injectable";
export const userPreferencesMigrationInjectionToken = getInjectionToken<MigrationDeclaration>({
id: "user-preferences-migration-token",
@@ -5,8 +5,8 @@
import { getInjectable } from "@ogre-tools/injectable";
import { action } from "mobx";
import { prefixedLoggerInjectable } from "@k8slens/logger";
import createPersistentStorageInjectable from "../../../common/persistent-storage/create.injectable";
import persistentStorageMigrationsInjectable from "../../../common/persistent-storage/migrations.injectable";
import createPersistentStorageInjectable from "../../persistent-storage/common/create.injectable";
import persistentStorageMigrationsInjectable from "../../persistent-storage/common/migrations.injectable";
import { userPreferencesMigrationInjectionToken } from "./migrations-token";
import { toJS } from "../../../common/utils";
import storeMigrationVersionInjectable from "../../../common/vars/store-migration-version.injectable";
@@ -4,7 +4,7 @@
*/
import { getInjectionToken } from "@ogre-tools/injectable";
import type { MigrationDeclaration } from "../../../common/persistent-storage/migrations.injectable";
import type { MigrationDeclaration } from "../../persistent-storage/common/migrations.injectable";
export const weblinkStoreMigrationInjectionToken = getInjectionToken<MigrationDeclaration>({
id: "weblink-store-migration-token",
@@ -4,8 +4,8 @@
*/
import { getInjectable } from "@ogre-tools/injectable";
import { action, comparer, toJS } from "mobx";
import createPersistentStorageInjectable from "../../../common/persistent-storage/create.injectable";
import persistentStorageMigrationsInjectable from "../../../common/persistent-storage/migrations.injectable";
import createPersistentStorageInjectable from "../../persistent-storage/common/create.injectable";
import persistentStorageMigrationsInjectable from "../../persistent-storage/common/migrations.injectable";
import storeMigrationVersionInjectable from "../../../common/vars/store-migration-version.injectable";
import { weblinkStoreMigrationInjectionToken } from "./migration-token";
import weblinksStateInjectable from "./state.injectable";