Skip to content

Commit

Permalink
Add file validation to pre-window checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
webfiltered committed Dec 2, 2024
1 parent f5fed9d commit d1a1d51
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
29 changes: 29 additions & 0 deletions src/install/installationValidator.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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(() => {});
}
}
42 changes: 37 additions & 5 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -205,16 +206,47 @@ export class ComfyDesktopApp {

static async create(appWindow: AppWindow): Promise<ComfyDesktopApp> {
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<string | undefined> {
// 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<string> {
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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export type AppWindowSettings = {

export type DesktopSettings = {
basePath?: string;
installState?: 'started' | 'installed';
installState?: 'started' | 'installed' | 'upgraded';
};

0 comments on commit d1a1d51

Please sign in to comment.