Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest manager #404

Closed
wants to merge 16 commits into from
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
"productName": "ComfyUI",
"repository": "github:comfy-org/electron",
"copyright": "Copyright © 2024 Comfy Org",
"version": "0.3.20",
"version": "0.3.21",
"homepage": "https://comfy.org",
"description": "The best modular GUI to run AI diffusion models.",
"main": ".vite/build/main.js",
"packageManager": "[email protected]",
"config": {
"frontendVersion": "1.5.0",
"comfyVersion": "0.3.2",
"managerCommit": "ada683c6bafcb983c8366e4310b7f95f66bd6362",
"managerCommit": "ad5fae39acfbd73a7c00a8713f64ad61fcb4000f",
"uvVersion": "0.5.4"
},
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { DownloadManager } from '../models/DownloadManager';
import { VirtualEnvironment } from '../virtualEnvironment';
import { InstallWizard } from '../install/installWizard';
import { Terminal } from '../terminal';

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 @@ -180,6 +181,16 @@ export class ComfyDesktopApp {
this.appWindow.send(IPC_CHANNELS.LOG_MESSAGE, data);
},
});
const store = new Store();
if (!store.get('Comfy-Desktop.RestoredCustomNodes', false)) {
try {
await restoreCustomNodes(virtualEnvironment, this.appWindow);
store.set('Comfy-Desktop.RestoredCustomNodes', true);
} catch (error) {
log.error('Failed to restore custom nodes:', error);
store.set('Comfy-Desktop.RestoredCustomNodes', false);
}
}

this.appWindow.sendServerStartProgress(ProgressStatus.STARTING_SERVER);
this.comfyServer = new ComfyServer(this.basePath, serverArgs, virtualEnvironment, this.appWindow);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPC_CHANNELS, DEFAULT_SERVER_ARGS, ProgressStatus, SENTRY_URL_ENDPOINT } from './constants';
import { IPC_CHANNELS, DEFAULT_SERVER_ARGS, ProgressStatus } from './constants';
import { app, dialog, ipcMain } from 'electron';
import log from 'electron-log/main';
import { findAvailablePort } from './utils';
Expand Down
103 changes: 103 additions & 0 deletions src/services/backup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import * as path from 'path';
import * as fs from 'fs';
import * as glob from 'glob';
import { app } from 'electron';
import { VirtualEnvironment } from '../virtualEnvironment';
import { getAppResourcesPath } from '../install/resourcePaths';
import log from 'electron-log/main';
import { AppWindow } from '../main-process/appWindow';
import { IPC_CHANNELS } from '../constants';

function parseLogFile(logPath: string): Set<string> {
console.log('Parsing log file:', logPath);
const customNodes = new Set<string>();
const content = fs.readFileSync(logPath, 'utf-8');

const lines = content.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
// Match the exact format from Python's "{:6.1f} seconds"
const timeMatch = line.match(/\s*\d+\.\d+\s+seconds/);
if (timeMatch) {
log.info(line);
// Second pattern: extract custom node name from path
const customNodeMatch = line.match(/custom_nodes[/\\]([^/\\]+)/);
if (customNodeMatch) {
log.info('Node match found:', customNodeMatch[1]);
const nodeName = customNodeMatch[1];
if (nodeName !== 'ComfyUI-Manager' && nodeName !== 'websocket_image_save.py') {
customNodes.add(nodeName);
}
}
}
}

return customNodes;
}

function getSortedLogFiles(): string[] {
try {
const logsDir = app.getPath('logs');
const logFiles = glob.sync(path.join(logsDir, 'comfyui*.log'));

// Sort files by modification time, newest first
return logFiles.sort((a, b) => {
return fs.statSync(b).mtime.getTime() - fs.statSync(a).mtime.getTime();
});
} catch (error) {
console.error('Failed to get logs directory:', error);
return [];
}
}

async function installCustomNodes(
nodes: string[],
virtualEnvironment: VirtualEnvironment,
appWindow: AppWindow
): Promise<void> {
if (nodes.length === 0) {
return;
}
const cmCliPath = path.join(getAppResourcesPath(), 'ComfyUI', 'custom_nodes', 'ComfyUI-Manager', 'cm-cli.py');
appWindow.send(IPC_CHANNELS.LOG_MESSAGE, `Reinstalling ${nodes.length} custom nodes...\n`);
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
appWindow.send(IPC_CHANNELS.LOG_MESSAGE, `Installing custom node (${i + 1}/${nodes.length}): ${node}\n`);
const cmd = [
cmCliPath,
'install',
node,
'--install-path',
path.join(virtualEnvironment.venvRootPath, 'custom_nodes'),
'--no-deps',
];
const { exitCode } = await virtualEnvironment.runPythonCommandAsync(cmd, {
onStdout: (data) => {
log.info(data.toString());
},
onStderr: (data) => {
log.error(data.toString());
},
});
if (exitCode !== 0) {
log.error(`Failed to install custom nodes: ${exitCode}`);
}
log.info(`Successfully installed custom node: ${node}`);
}
}

export async function restoreCustomNodes(virtualEnvironment: VirtualEnvironment, appWindow: AppWindow): Promise<void> {
const logFiles = getSortedLogFiles();
if (logFiles.length === 0) {
return;
}

const customNodes = new Set<string>();
for (const logFile of logFiles) {
const nodes = parseLogFile(logFile);
nodes.forEach((node) => customNodes.add(node));
}

log.info('Found custom nodes:', customNodes);
await installCustomNodes(Array.from(customNodes), virtualEnvironment, appWindow);
}
2 changes: 1 addition & 1 deletion todesktop.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "241012ess7yxs0e",
"id": "241125q2i8rhv",
"icon": "./assets/UI/Comfy_Logo_x128.png",
"appBuilderLibVersion": "25.1.8",
"schemaVersion": 1,
Expand Down
Loading