Skip to content

Commit

Permalink
Revert "Settings store (#362)"
Browse files Browse the repository at this point in the history
This reverts commit 54f1a96.
  • Loading branch information
robinjhuang authored Dec 5, 2024
1 parent 5c0a37e commit 182f78a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 282 deletions.
54 changes: 13 additions & 41 deletions scripts/resetInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ const readline = require('readline');

/**
* Get the path to the extra_models_config.yaml file based on the platform.
* @param {string} filename The name of the file to find in the user data folder
* @returns The path to the extra_models_config.yaml file.
*/

function getConfigPath(filename) {
function getConfigPath() {
switch (process.platform) {
case 'darwin': // macOS
return path.join(os.homedir(), 'Library', 'Application Support', 'ComfyUI', filename);
return path.join(os.homedir(), 'Library', 'Application Support', 'ComfyUI', 'extra_models_config.yaml');
case 'win32': // Windows
return path.join(process.env.APPDATA, 'ComfyUI', filename);
return path.join(process.env.APPDATA, 'ComfyUI', 'extra_models_config.yaml');
default:
console.log('Platform not supported for this operation');
process.exit(1);
Expand All @@ -38,54 +37,27 @@ async function askForConfirmation(question) {

async function main() {
try {
const configPath = getConfigPath('config.json');
const windowStorePath = getConfigPath('window.json');
const modelsConfigPath = getConfigPath('extra_models_config.yaml');
let desktopBasePath = null;
const configPath = getConfigPath();
let basePath = null;

// Read basePath from desktop config
// Read base_path before deleting the config file
if (fs.existsSync(configPath)) {
const configContent = fs.readFileSync(configPath, 'utf8');
const parsed = JSON.parse(configContent);
desktopBasePath = parsed?.basePath;
}

// Read base_path before deleting the config file
if (fs.existsSync(modelsConfigPath)) {
const configContent = fs.readFileSync(modelsConfigPath, 'utf8');
const config = yaml.parse(configContent);
basePath = config?.comfyui?.base_path;

// Delete config file
fs.unlinkSync(configPath);
console.log(`Successfully removed ${configPath}`);
} else {
console.log('Config file not found, nothing to remove');
}

// Delete all config files
for (const file of [configPath, windowStorePath, modelsConfigPath]) {
if (fs.existsSync(file)) {
fs.unlinkSync(file);
console.log(`Successfully removed ${file}`);
}
}

// If config.json basePath exists, ask user if they want to delete it
if (desktopBasePath && fs.existsSync(desktopBasePath)) {
console.log(`Found ComfyUI installation directory at: ${desktopBasePath}`);
const shouldDelete = await askForConfirmation('Would you like to delete this directory as well?');

if (shouldDelete) {
fs.rmSync(desktopBasePath, { recursive: true, force: true });
console.log(`Successfully removed ComfyUI directory at ${desktopBasePath}`);
} else {
console.log('Skipping ComfyUI directory deletion');
}
}

// If base_path exists and does not match basePath, ask user if they want to delete it
if (basePath && basePath !== desktopBasePath && fs.existsSync(basePath)) {
console.log(`Found ComfyUI models directory at: ${basePath}`);
// If base_path exists, ask user if they want to delete it
if (basePath && fs.existsSync(basePath)) {
console.log(`Found ComfyUI installation directory at: ${basePath}`);
const shouldDelete = await askForConfirmation('Would you like to delete this directory as well?');

if (shouldDelete) {
fs.rmSync(basePath, { recursive: true, force: true });
console.log(`Successfully removed ComfyUI directory at ${basePath}`);
Expand Down
58 changes: 0 additions & 58 deletions src/install/installationValidator.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/main-process/appWindow.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem } from 'electron';
import path from 'node:path';
import Store from 'electron-store';
import { AppWindowSettings } from '../store';
import { StoreType } from '../store';
import log from 'electron-log/main';
import { IPC_CHANNELS, ProgressStatus, ServerArgs } from '../constants';
import { getAppResourcesPath } from '../install/resourcePaths';
Expand All @@ -12,7 +12,7 @@ import { getAppResourcesPath } from '../install/resourcePaths';
*/
export class AppWindow {
private window: BrowserWindow;
private store: Store<AppWindowSettings>;
private store: Store<StoreType>;
private messageQueue: Array<{ channel: string; data: any }> = [];
private rendererReady: boolean = false;

Expand Down Expand Up @@ -142,10 +142,10 @@ export class AppWindow {
* There are edge cases where this might not be a catastrophic failure, but inability
* to write to our own datastore may result in unexpected user data loss.
*/
private loadWindowStore(): Store<AppWindowSettings> {
private loadWindowStore(): Store<StoreType> {
try {
// Separate file for non-critical convenience settings - just resets itself if invalid
return new Store<AppWindowSettings>({
return new Store<StoreType>({
clearInvalidConfig: true,
name: 'window',
});
Expand Down
54 changes: 8 additions & 46 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import { DownloadManager } from '../models/DownloadManager';
import { VirtualEnvironment } from '../virtualEnvironment';
import { InstallWizard } from '../install/installWizard';
import { Terminal } from '../terminal';
import { DesktopConfig } from '../store/desktopConfig';
import { InstallationValidator } from '../install/installationValidator';
import { restoreCustomNodes } from '../services/backup';

import Store from 'electron-store';
export class ComfyDesktopApp {
public comfyServer: ComfyServer | null = null;
private terminal: Terminal | null = null; // Only created after server starts.
Expand Down Expand Up @@ -147,11 +145,7 @@ export class ComfyDesktopApp {
return new Promise<string>((resolve) => {
ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, async (event, installOptions: InstallOptions) => {
const installWizard = new InstallWizard(installOptions);
const { store } = DesktopConfig;
store.set('basePath', installWizard.basePath);

await installWizard.install();
store.set('installState', 'installed');
resolve(installWizard.basePath);
});
});
Expand Down Expand Up @@ -188,7 +182,7 @@ export class ComfyDesktopApp {
this.appWindow.send(IPC_CHANNELS.LOG_MESSAGE, data);
},
});
const { store } = DesktopConfig;
const store = new Store();
if (!store.get('Comfy-Desktop.RestoredCustomNodes', false)) {
try {
await restoreCustomNodes(virtualEnvironment, this.appWindow);
Expand All @@ -206,48 +200,16 @@ export class ComfyDesktopApp {
}

static async create(appWindow: AppWindow): Promise<ComfyDesktopApp> {
const { store } = DesktopConfig;
// Migrate settings from old version if required
const installState = store.get('installState') ?? (await ComfyDesktopApp.migrateInstallState());

// Fresh install
const basePath =
installState === undefined ? await ComfyDesktopApp.install(appWindow) : await ComfyDesktopApp.loadBasePath();
const basePath = ComfyServerConfig.exists()
? await ComfyServerConfig.readBasePathFromConfig(ComfyServerConfig.configPath)
: await this.install(appWindow);

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 } = DesktopConfig;
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
17 changes: 1 addition & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { AppInfoHandlers } from './handlers/appInfoHandlers';
import { ComfyDesktopApp } from './main-process/comfyDesktopApp';
import { LevelOption } from 'electron-log';
import SentryLogging from './services/sentry';
import { DesktopConfig } from './store/desktopConfig';

dotenv.config();
log.initialize();
Expand Down Expand Up @@ -41,17 +40,6 @@ if (!gotTheLock) {
app.on('ready', async () => {
log.debug('App ready');

const store = await DesktopConfig.load();
if (store) {
startApp();
} else {
app.exit(20);
}
});
}

async function startApp() {
try {
const appWindow = new AppWindow();
appWindow.onClose(() => {
log.info('App window closed. Quitting application.');
Expand Down Expand Up @@ -91,8 +79,5 @@ async function startApp() {
appWindow.sendServerStartProgress(ProgressStatus.ERROR);
appWindow.send(IPC_CHANNELS.LOG_MESSAGE, error);
}
} catch (error) {
log.error('Fatal error occurred during app startup.', error);
app.exit(2024);
}
});
}
Loading

0 comments on commit 182f78a

Please sign in to comment.