|
| 1 | +import { app, ipcMain } from 'electron'; |
| 2 | +import log from 'electron-log/main'; |
| 3 | +import { IPC_CHANNELS } from '../constants'; |
| 4 | +import { createModelConfigFiles, getModelConfigPath } from '../config/extra_model_config'; |
| 5 | +import fs from 'fs'; |
| 6 | +import { AppWindow } from '../main-process/appWindow'; |
| 7 | +import { ComfyConfigManager } from '../config/comfyConfigManager'; |
| 8 | + |
| 9 | +type InstallationState = |
| 10 | + | { status: 'NOT_INSTALLED' } |
| 11 | + | { status: 'ASKING_FOR_DIRECTORY'; defaultLocation: string; validationErrorMessage?: string } |
| 12 | + | { |
| 13 | + status: 'INSTALLING'; |
| 14 | + location: string; |
| 15 | + } |
| 16 | + | { |
| 17 | + status: 'READY'; |
| 18 | + location: string; // Must exist! |
| 19 | + } |
| 20 | + | { |
| 21 | + status: 'ERROR'; |
| 22 | + error: Error; // Must exist! |
| 23 | + }; |
| 24 | + |
| 25 | +/** |
| 26 | + * This class is responsible for handling the installation of ComfyUI. |
| 27 | + * It registers an IPC handler for the current installation state. |
| 28 | + * It also sends any changes to the state to the renderer process. |
| 29 | + */ |
| 30 | +export class ComfyUIInstall { |
| 31 | + private static instance: ComfyUIInstall; |
| 32 | + private installLocation: string; |
| 33 | + private defaultInstallLocation: string; |
| 34 | + private state: InstallationState = { status: 'NOT_INSTALLED' }; |
| 35 | + private appWindow: AppWindow; |
| 36 | + |
| 37 | + private constructor(appWindow: AppWindow) { |
| 38 | + const defaultInstallLocation = app.getPath('documents'); |
| 39 | + ipcMain.handle(IPC_CHANNELS.DEFAULT_INSTALL_LOCATION, () => defaultInstallLocation); // TODO: Remove after migration. |
| 40 | + ipcMain.handle(IPC_CHANNELS.GET_INSTALLATION_STATE, () => this.state); |
| 41 | + this.installLocation = defaultInstallLocation; |
| 42 | + this.defaultInstallLocation = defaultInstallLocation; |
| 43 | + this.appWindow = appWindow; |
| 44 | + } |
| 45 | + |
| 46 | + static get(appWindow: AppWindow): ComfyUIInstall { |
| 47 | + if (!ComfyUIInstall.instance) { |
| 48 | + ComfyUIInstall.instance = new ComfyUIInstall(appWindow); |
| 49 | + } |
| 50 | + return ComfyUIInstall.instance; |
| 51 | + } |
| 52 | + |
| 53 | + setState(state: InstallationState): void { |
| 54 | + log.info('Setting installation state:', state); |
| 55 | + this.state = state; |
| 56 | + this.appWindow.send(IPC_CHANNELS.INSTALLATION_STATE_CHANGED, state); |
| 57 | + } |
| 58 | + |
| 59 | + public async install(): Promise<void> { |
| 60 | + const firstTimeSetup = await this.isFirstTimeSetup(); |
| 61 | + log.info('First time setup:', firstTimeSetup); |
| 62 | + if (!firstTimeSetup) { |
| 63 | + this.appWindow.send(IPC_CHANNELS.FIRST_TIME_SETUP_COMPLETE, null); // TODO:Remove after migration. |
| 64 | + return; |
| 65 | + } |
| 66 | + this.setState({ status: 'ASKING_FOR_DIRECTORY', defaultLocation: this.defaultInstallLocation }); |
| 67 | + this.appWindow.send(IPC_CHANNELS.SHOW_SELECT_DIRECTORY, null); // TODO:Remove after migration. |
| 68 | + while (this.state.status === 'ASKING_FOR_DIRECTORY') { |
| 69 | + const selectedDirectory = await this.selectInstallDirectory(); |
| 70 | + const { valid, errorMessage } = await this.isValidComfyDirectory(selectedDirectory); |
| 71 | + if (!valid) { |
| 72 | + this.setState({ ...this.state, validationErrorMessage: errorMessage }); |
| 73 | + return; |
| 74 | + } else { |
| 75 | + this.setState({ status: 'INSTALLING', location: selectedDirectory }); |
| 76 | + this.installLocation = selectedDirectory; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const actualComfyDirectory = ComfyConfigManager.setUpComfyUI(this.installLocation); |
| 81 | + const modelConfigPath = getModelConfigPath(); |
| 82 | + await createModelConfigFiles(modelConfigPath, actualComfyDirectory); |
| 83 | + |
| 84 | + this.setState({ status: 'READY', location: this.installLocation }); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Check if the user has completed the first time setup wizard. |
| 89 | + * This means the extra_models_config.yaml file exists in the user's data directory. |
| 90 | + */ |
| 91 | + private async isFirstTimeSetup(): Promise<boolean> { |
| 92 | + const extraModelsConfigPath = getModelConfigPath(); |
| 93 | + return !fs.existsSync(extraModelsConfigPath); |
| 94 | + } |
| 95 | + |
| 96 | + private async selectInstallDirectory(): Promise<string> { |
| 97 | + return new Promise((resolve, reject) => { |
| 98 | + ipcMain.on(IPC_CHANNELS.SELECTED_DIRECTORY, (_event, value: string) => { |
| 99 | + log.info('Directory selected:', value); |
| 100 | + resolve(value); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + async isValidComfyDirectory(selectedDirectory: string): Promise<{ valid: boolean; errorMessage?: string }> { |
| 106 | + //TODO: Implement |
| 107 | + return { valid: true }; |
| 108 | + } |
| 109 | +} |
0 commit comments