Skip to content

Commit

Permalink
Merge branch 'main' into enh/Improve-UNINSTALL-logic
Browse files Browse the repository at this point in the history
  • Loading branch information
otociulis committed Nov 23, 2024
2 parents 9329ab6 + e933a88 commit e818ede
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 50 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This electron app is the simplest way to use [ComfyUI](https://github.com/comfyanonymous/ComfyUI) and comes bundled with a few things:

- Stable version of ComfyUI from [releases](https://github.com/comfyanonymous/ComfyUI/releases)
- comfyui [manager-core](https://github.com/Comfy-Org/manager-core)
- [ComfyUI-Manager](https://github.com/ltdrdata/ComfyUI-Manager)
- [uv](https://github.com/astral-sh/uv)

On startup, it will install all the necessary python dependencies with uv and start the ComfyUI server. The app will automatically update with stable releases of ComfyUI, ComfyUI-Manager, and the uv executable.
Expand Down Expand Up @@ -99,6 +99,15 @@ Confirmed working:
- Visual Studio Community 2022 - 17.12.1
- Desktop development with C++ workload
- MSVC v143 x64 spectre-mitigated libraries (Latest / v14.42-17.12)
- Open the Visual Studio Installer
- Click "Modify" on your Visual Studio 2022 Community installation
- Go to the "Individual Components" tab
- Search for "Spectre"
- Check the boxes for the Spectre-mitigated libraries that match your project's architecture (x86 and/or x64)
- ![image](https://github.com/user-attachments/assets/0829db3d-84b7-48e8-9d13-c72c35169a05)

Look for "MSVC v143 - VS 2022 C++ x64/x86 Spectre-mitigated libs"
If you're using other toolsets, you may need their corresponding Spectre-mitigated libraries as well

## NPM Dependencies

Expand Down Expand Up @@ -163,6 +172,7 @@ yarn download:uv
You can then run `start` to build/launch the code and a live buildserver that will automatically rebuild the code on any changes:

```bash
deactivate # Deactivate your existing python env to avoid influencing the
yarn start
```

Expand Down
8 changes: 4 additions & 4 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.3",
"version": "0.3.8",
"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.4.4",
"comfyVersion": "0.2.7",
"managerCommit": "e629215c100c89a9a9d33fc03be3248069ff67ef",
"frontendVersion": "1.4.5",
"comfyVersion": "0.3.2",
"managerCommit": "1ab304bfbcfd7f550fc5adbb75a4e3b24861b947",
"uvVersion": "0.5.2"
},
"scripts": {
Expand Down
7 changes: 0 additions & 7 deletions scripts/makeComfy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function makeAssets(gpuFlag) {
'&&',
`comfy-cli --skip-prompt --here install --version ${pkg.config.comfyVersion} --fast-deps`,
gpuFlag,
'--manager-url https://github.com/Comfy-Org/manager-core',
'--manager-commit',
pkg.config.managerCommit,
'&&',
Expand All @@ -19,12 +18,6 @@ function makeAssets(gpuFlag) {


execSync(baseCommand, { stdio: 'inherit' });

// Rename custom_nodes/ComfyUI-Manager to manager-core
if (!fs.existsSync('assets/ComfyUI/custom_nodes/ComfyUI-Manager')) {
throw new Error('ComfyUI-Manager not found');
}
fs.renameSync('./assets/ComfyUI/custom_nodes/ComfyUI-Manager', './assets/ComfyUI/custom_nodes/manager-core');
}

// Get GPU flag from command line argument
Expand Down
2 changes: 1 addition & 1 deletion scripts/todesktop/afterPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = async ({ appOutDir, packager, outDir }) => {
const resourcePath = path.join(appPath, "Contents", "Resources");
// Remove these Git folders that mac's codesign is choking on. Need a more recursive way to just find all folders with '.git' and delete
await fs.rm(path.join(assetPath, "ComfyUI", ".git"), { recursive: true, force: true });
await fs.rm(path.join(assetPath, "ComfyUI", 'custom_nodes', 'manager-core', ".git"), { recursive: true, force: true });
await fs.rm(path.join(assetPath, "ComfyUI", 'custom_nodes', 'ComfyUI-Manager', ".git"), { recursive: true, force: true });
await fs.rm(path.join(assetPath, "ComfyUI", 'custom_nodes', 'DesktopSettingsExtension', ".git"), { recursive: true, force: true });
// Move rest of items to the resource folder
await fs.cp(assetPath, resourcePath, { recursive: true });
Expand Down
2 changes: 1 addition & 1 deletion src/config/comfySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface ComfySettingsData {
*/
export class ComfySettings {
public readonly filePath: string;
private settings: ComfySettingsData = DEFAULT_SETTINGS;
private settings: ComfySettingsData = structuredClone(DEFAULT_SETTINGS);

constructor(basePath: string) {
this.filePath = path.join(basePath, 'user', 'default', 'comfy.settings.json');
Expand Down
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,12 @@ export const DEFAULT_SERVER_ARGS = {
};

export type ServerArgs = typeof DEFAULT_SERVER_ARGS;

export enum DownloadStatus {
PENDING = 'pending',
IN_PROGRESS = 'in_progress',
COMPLETED = 'completed',
PAUSED = 'paused',
ERROR = 'error',
CANCELLED = 'cancelled',
}
35 changes: 19 additions & 16 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ import { getAppResourcesPath } from '../install/resourcePaths';

export class ComfyDesktopApp {
public comfyServer: ComfyServer | null = null;
private terminal: Terminal;
private terminal: Terminal | null = null; // Only created after server starts.

constructor(
public basePath: string,
public comfySettings: ComfySettings,
public appWindow: AppWindow
) {
this.terminal = new Terminal(this.appWindow, getAppResourcesPath());
}
) {}

get pythonInstallPath() {
return app.isPackaged ? this.basePath : path.join(app.getAppPath(), 'assets');
Expand All @@ -50,6 +48,22 @@ export class ComfyDesktopApp {
});
}

private initializeTerminal(virtualEnvironment: VirtualEnvironment) {
this.terminal = new Terminal(this.appWindow, this.basePath, virtualEnvironment.uvPath);

ipcMain.handle(IPC_CHANNELS.TERMINAL_WRITE, (_event, command: string) => {
this.terminal?.write(command);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESIZE, (_event, cols: number, rows: number) => {
this.terminal?.resize(cols, rows);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESTORE, (_event) => {
return this.terminal?.restore();
});
}

async setupGPUContext(): Promise<void> {
log.debug('Setting up GPU context');
try {
Expand Down Expand Up @@ -112,18 +126,6 @@ export class ComfyDesktopApp {
return null;
}
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_WRITE, (_event, command: string) => {
this.terminal.write(command);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESIZE, (_event, cols: number, rows: number) => {
this.terminal.resize(cols, rows);
});

ipcMain.handle(IPC_CHANNELS.TERMINAL_RESTORE, (_event) => {
return this.terminal.restore();
});
}

/**
Expand Down Expand Up @@ -175,6 +177,7 @@ export class ComfyDesktopApp {
this.appWindow.sendServerStartProgress(ProgressStatus.STARTING_SERVER);
this.comfyServer = new ComfyServer(this.basePath, serverArgs, virtualEnvironment, this.appWindow);
await this.comfyServer.start();
this.initializeTerminal(virtualEnvironment);
}

static async create(appWindow: AppWindow): Promise<ComfyDesktopApp> {
Expand Down
7 changes: 1 addition & 6 deletions src/main_types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
export * from './constants';
export * from './models/DownloadManager';
export type { DownloadManager, Download, DownloadState } from './models/DownloadManager';
export type { ElectronAPI } from './preload';

import { ElectronAPI } from './preload';
declare global {
const electronAPI: ElectronAPI;
}
11 changes: 2 additions & 9 deletions src/models/DownloadManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as path from 'path';
import * as fs from 'fs';
import { IPC_CHANNELS } from '../constants';
import log from 'electron-log/main';
import { AppWindow } from '../main-process/appWindow';
import type { AppWindow } from '../main-process/appWindow';
import { DownloadStatus } from '../main_types';

export interface Download {
url: string;
Expand All @@ -13,14 +14,6 @@ export interface Download {
item: DownloadItem | null;
}

export enum DownloadStatus {
PENDING = 'pending',
IN_PROGRESS = 'in_progress',
COMPLETED = 'completed',
PAUSED = 'paused',
ERROR = 'error',
CANCELLED = 'cancelled',
}
export interface DownloadState {
url: string;
filename: string;
Expand Down
4 changes: 2 additions & 2 deletions src/preload.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contextBridge, ipcRenderer } from 'electron';
import { IPC_CHANNELS, ELECTRON_BRIDGE_API, ProgressStatus } from './constants';
import { DownloadState, DownloadStatus } from './models/DownloadManager';
import { IPC_CHANNELS, ELECTRON_BRIDGE_API, ProgressStatus, DownloadStatus } from './constants';
import type { DownloadState } from './models/DownloadManager';
import path from 'node:path';

/**
Expand Down
25 changes: 23 additions & 2 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class Terminal {
#pty: pty.IPty | undefined;
#window: AppWindow | undefined;
#cwd: string | undefined;
#uvPath: string | undefined;

readonly sessionBuffer: string[] = [];
readonly size = { cols: 80, rows: 30 };
Expand All @@ -21,9 +22,10 @@ export class Terminal {
return this.#window;
}

constructor(window: AppWindow, cwd: string) {
constructor(window: AppWindow, cwd: string, uvPath: string) {
this.#window = window;
this.#cwd = cwd;
this.#uvPath = uvPath;
}

write(data: string) {
Expand All @@ -46,7 +48,7 @@ export class Terminal {
#createPty() {
const window = this.window;
// TODO: does this want to be a setting?
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
const shell = this.#getDefaultShell();
const instance = pty.spawn(shell, [], {
handleFlowControl: false,
conptyInheritCursor: false,
Expand All @@ -56,6 +58,14 @@ export class Terminal {
cwd: this.#cwd,
});

if (process.platform === 'win32') {
// PowerShell function
instance.write(`function pip { & "${this.#uvPath}" pip $args }\r\n`);
} else {
// Bash/Zsh alias
instance.write(`alias pip='"${this.#uvPath}" pip'\r\n`);
}

instance.onData((data) => {
this.sessionBuffer.push(data);
window.send(IPC_CHANNELS.TERMINAL_ON_OUTPUT, data);
Expand All @@ -64,4 +74,15 @@ export class Terminal {

return instance;
}

#getDefaultShell(): string {
switch (os.platform()) {
case 'win32':
return 'powershell.exe';
case 'darwin':
return 'zsh';
default: // Linux and others
return 'bash';
}
}
}
7 changes: 6 additions & 1 deletion src/virtualEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class VirtualEnvironment {
resourcesPath,
'ComfyUI',
'custom_nodes',
'manager-core',
'ComfyUI-Manager',
'requirements.txt'
);

Expand Down Expand Up @@ -119,6 +119,11 @@ export class VirtualEnvironment {
throw new Error(`Failed to create virtual environment: exit code ${exitCode}`);
}

const { exitCode: ensurepipExitCode } = await this.runPythonCommandAsync(['-m', 'ensurepip', '--upgrade']);
if (ensurepipExitCode !== 0) {
throw new Error(`Failed to upgrade pip: exit code ${ensurepipExitCode}`);
}

log.info(`Successfully created virtual environment at ${this.venvPath}`);
} catch (error) {
log.error(`Error creating virtual environment: ${error}`);
Expand Down
1 change: 1 addition & 0 deletions vite.types.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineConfig({
rollupOptions: {
external: ['electron'],
},
minify: false,
},
plugins: [
dts({
Expand Down

0 comments on commit e818ede

Please sign in to comment.