From 8a90c230be868215bd1484682b42eccfe4976f5e Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 19 Sep 2022 16:35:24 -0400 Subject: [PATCH] Move userStoreFileMigration call to the init runnable Signed-off-by: Sebastian Malton --- .../file-name-migration.injectable.ts | 30 +++++++++++-------- .../user-store/user-store.injectable.ts | 6 ---- src/main/stores/init-user-store.injectable.ts | 7 ++++- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/common/user-store/file-name-migration.injectable.ts b/src/common/user-store/file-name-migration.injectable.ts index 8f2dcf1e23..cf05c67bec 100644 --- a/src/common/user-store/file-name-migration.injectable.ts +++ b/src/common/user-store/file-name-migration.injectable.ts @@ -9,26 +9,30 @@ import directoryForUserDataInjectable from "../app-paths/directory-for-user-data import { isErrnoException } from "../utils"; import { getInjectable } from "@ogre-tools/injectable"; +export type UserStoreFileNameMigration = () => Promise; + const userStoreFileNameMigrationInjectable = getInjectable({ id: "user-store-file-name-migration", - instantiate: (di) => { + instantiate: (di): UserStoreFileNameMigration => { const userDataPath = di.inject(directoryForUserDataInjectable); const configJsonPath = path.join(userDataPath, "config.json"); const lensUserStoreJsonPath = path.join(userDataPath, "lens-user-store.json"); - try { - fse.moveSync(configJsonPath, lensUserStoreJsonPath); - } catch (error) { - if (error instanceof Error && error.message === "dest already exists.") { - fse.removeSync(configJsonPath); - } else if (isErrnoException(error) && error.code === "ENOENT" && error.path === configJsonPath) { - // (No such file or directory) - return; // file already moved - } else { - // pass other errors along - throw error; + return async () => { + try { + await fse.move(configJsonPath, lensUserStoreJsonPath); + } catch (error) { + if (error instanceof Error && error.message === "dest already exists.") { + await fse.remove(configJsonPath); + } else if (isErrnoException(error) && error.code === "ENOENT" && error.path === configJsonPath) { + // (No such file or directory) + return; // file already moved + } else { + // pass other errors along + throw error; + } } - } + }; }, }); diff --git a/src/common/user-store/user-store.injectable.ts b/src/common/user-store/user-store.injectable.ts index 3b4aba0b56..ffd2310886 100644 --- a/src/common/user-store/user-store.injectable.ts +++ b/src/common/user-store/user-store.injectable.ts @@ -3,8 +3,6 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; -import { ipcMain } from "electron"; -import userStoreFileNameMigrationInjectable from "./file-name-migration.injectable"; import { UserStore } from "./user-store"; import selectedUpdateChannelInjectable from "../application-update/selected-update-channel/selected-update-channel.injectable"; @@ -14,10 +12,6 @@ const userStoreInjectable = getInjectable({ instantiate: (di) => { UserStore.resetInstance(); - if (ipcMain) { - di.inject(userStoreFileNameMigrationInjectable); - } - return UserStore.createInstance({ selectedUpdateChannel: di.inject(selectedUpdateChannelInjectable), }); diff --git a/src/main/stores/init-user-store.injectable.ts b/src/main/stores/init-user-store.injectable.ts index c7d088a21b..572c0154e4 100644 --- a/src/main/stores/init-user-store.injectable.ts +++ b/src/main/stores/init-user-store.injectable.ts @@ -3,6 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { getInjectable } from "@ogre-tools/injectable"; +import userStoreFileNameMigrationInjectable from "../../common/user-store/file-name-migration.injectable"; import userStoreInjectable from "../../common/user-store/user-store.injectable"; import { beforeApplicationIsLoadingInjectionToken } from "../start-main-application/runnable-tokens/before-application-is-loading-injection-token"; import initDefaultUpdateChannelInjectableInjectable from "../vars/default-update-channel/init.injectable"; @@ -11,9 +12,13 @@ const initUserStoreInjectable = getInjectable({ id: "init-user-store", instantiate: (di) => { const userStore = di.inject(userStoreInjectable); + const userStoreFileNameMigration = di.inject(userStoreFileNameMigrationInjectable); return { - run: () => userStore.load(), + run: async () => { + await userStoreFileNameMigration(); + userStore.load(); + }, runAfter: di.inject(initDefaultUpdateChannelInjectableInjectable), }; },