From d1a1d5134beafbd3868d5aa1a00d3ec98bbbfc76 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Thu, 28 Nov 2024 01:12:00 +1100 Subject: [PATCH] Add file validation to pre-window checks Provides the user with some information about what went wrong and why - and where they can look to fix it. Needs help / support button added. --- src/install/installationValidator.ts | 29 +++++++++++++++++++ src/main-process/comfyDesktopApp.ts | 42 ++++++++++++++++++++++++---- src/store/index.ts | 2 +- 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 src/install/installationValidator.ts diff --git a/src/install/installationValidator.ts b/src/install/installationValidator.ts new file mode 100644 index 00000000..45abfe52 --- /dev/null +++ b/src/install/installationValidator.ts @@ -0,0 +1,29 @@ +import { app, dialog, shell } from 'electron'; + +export class InstallationValidator { + /** + * Shows a dialog box with an option to open the problematic file in the native shell file viewer. + * @param options The options paramter of {@link dialog.showMessageBox}, filled with defaults for invalid config + * @returns + */ + static async showInvalidFileAndQuit(file: string, options: Electron.MessageBoxOptions): Promise { + const defaults: Electron.MessageBoxOptions = { + // Message must be set by caller. + message: `Was unable to read the file shown below. It could be missing, inaccessible, or corrupt.\n\n${file}`, + title: 'Invalid file', + type: 'error', + buttons: ['Locate the &file (then quit)', '&Quit'], + defaultId: 0, + cancelId: 1, + normalizeAccessKeys: true, + }; + const opt = Object.assign(defaults, options); + + const result = await dialog.showMessageBox(opt); + + if (result.response === 0) shell.showItemInFolder(file); + app.quit(); + // Wait patiently for graceful termination. + await new Promise(() => {}); + } +} diff --git a/src/main-process/comfyDesktopApp.ts b/src/main-process/comfyDesktopApp.ts index 48653207..f3bef42c 100644 --- a/src/main-process/comfyDesktopApp.ts +++ b/src/main-process/comfyDesktopApp.ts @@ -17,6 +17,7 @@ import { VirtualEnvironment } from '../virtualEnvironment'; import { InstallWizard } from '../install/installWizard'; import { Terminal } from '../terminal'; import { useDesktopStore } from '../store/store'; +import { InstallationValidator } from '../install/installationValidator'; import { restoreCustomNodes } from '../services/backup'; export class ComfyDesktopApp { @@ -205,16 +206,47 @@ export class ComfyDesktopApp { static async create(appWindow: AppWindow): Promise { const { store } = useDesktopStore(); + // Migrate settings from old version if required + const installState = store.get('installState') ?? (await ComfyDesktopApp.migrateInstallState()); - const installed = store.get('installState') === 'installed'; - const basePath = installed ? store.get('basePath') : await this.install(appWindow); + // Fresh install + const basePath = + installState === undefined ? await ComfyDesktopApp.install(appWindow) : await ComfyDesktopApp.loadBasePath(); - if (!basePath) { - throw new Error(`Base path not found! ${ComfyServerConfig.configPath} is probably corrupted.`); - } return new ComfyDesktopApp(basePath, new ComfySettings(basePath), appWindow); } + /** + * Sets the ugpraded state if this is a version upgrade from <= 0.3.18 + * @returns 'upgraded' if this install has just been upgraded, or undefined for a fresh install + */ + static async migrateInstallState(): Promise { + // Fresh install + if (!ComfyServerConfig.exists()) return undefined; + + // Upgrade + const basePath = await ComfyDesktopApp.loadBasePath(); + + // Migrate config + const { store } = useDesktopStore(); + const upgraded = 'upgraded'; + store.set('installState', upgraded); + store.set('basePath', basePath); + return upgraded; + } + + /** Loads the base_path value from the YAML config. Quits in the event of failure. */ + static async loadBasePath(): Promise { + const basePath = await ComfyServerConfig.readBasePathFromConfig(ComfyServerConfig.configPath); + if (basePath) return basePath; + + log.error(`Base path not found! ${ComfyServerConfig.configPath} is probably corrupted.`); + await InstallationValidator.showInvalidFileAndQuit(ComfyServerConfig.configPath, { + message: `Base path not found! This file is probably corrupt:\n\n${ComfyServerConfig.configPath}`, + }); + throw new Error(/* Unreachable. */); + } + uninstall(): void { fs.rmSync(ComfyServerConfig.configPath); } diff --git a/src/store/index.ts b/src/store/index.ts index 7c83a149..14b13707 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -8,5 +8,5 @@ export type AppWindowSettings = { export type DesktopSettings = { basePath?: string; - installState?: 'started' | 'installed'; + installState?: 'started' | 'installed' | 'upgraded'; };