Skip to content

Commit

Permalink
Disallow selecting existing ComfyUI dir as install path (#256)
Browse files Browse the repository at this point in the history
* Disallow selecting existing ComfyUI dir as install path

* Fix tests

* nit
  • Loading branch information
huchenlei authored Nov 15, 2024
1 parent dc1fd95 commit 6f00252
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
11 changes: 3 additions & 8 deletions src/config/comfyConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,13 @@ export class ComfyConfigManager {
'Comfy.Workflow.ShowMissingModelsWarning': true,
};

public static setUpComfyUI(localComfyDirectory: string): string {
if (!this.isComfyUIDirectory(localComfyDirectory)) {
log.info(
`Selected directory ${localComfyDirectory} is not a ComfyUI directory. Appending ComfyUI to install path.`
);
localComfyDirectory = path.join(localComfyDirectory, 'ComfyUI');
public static setUpComfyUI(localComfyDirectory: string) {
if (fs.existsSync(localComfyDirectory)) {
throw new Error(`Selected directory ${localComfyDirectory} already exists`);
}

this.createComfyDirectories(localComfyDirectory);
const userSettingsPath = path.join(localComfyDirectory, 'user', 'default');
this.createComfyConfigFile(userSettingsPath, true);
return localComfyDirectory;
}

public static createComfyConfigFile(userSettingsPath: string, overwrite: boolean = false): void {
Expand Down
15 changes: 11 additions & 4 deletions src/handlers/pathHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { SystemPaths } from '../preload';
import fs from 'fs';
import si from 'systeminformation';
import { ComfyConfigManager } from '../config/comfyConfigManager';
import path from 'path';

export class PathHandlers {
static readonly REQUIRED_SPACE = 10 * 1024 * 1024 * 1024; // 10GB in bytes
Expand Down Expand Up @@ -45,23 +46,29 @@ export class PathHandlers {
*/
ipcMain.handle(
IPC_CHANNELS.VALIDATE_INSTALL_PATH,
async (event, path: string): Promise<{ isValid: boolean; error?: string }> => {
async (event, inputPath: string): Promise<{ isValid: boolean; error?: string }> => {
try {
// Check if path exists
if (!fs.existsSync(path)) {
if (!fs.existsSync(inputPath)) {
return { isValid: false, error: 'Path does not exist' };
}

// Check if `path/ComfyUI` exists
// We are going to create a ComfyUI directory in the selected path
if (fs.existsSync(path.join(inputPath, 'ComfyUI'))) {
return { isValid: false, error: 'Path already contains ComfyUI/' };
}

// Check if path is writable
try {
fs.accessSync(path, fs.constants.W_OK);
fs.accessSync(inputPath, fs.constants.W_OK);
} catch (err) {
return { isValid: false, error: 'Path is not writable' };
}

// Check available disk space (require at least 10GB free)
const disks = await si.fsSize();
const disk = disks.find((disk) => path.startsWith(disk.mount));
const disk = disks.find((disk) => inputPath.startsWith(disk.mount));
if (disk && disk.available < PathHandlers.REQUIRED_SPACE) {
return {
isValid: false,
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ async function handleInstall(installOptions: InstallOptions) {
const migrationSource = installOptions.migrationSourcePath;
const migrationItemIds = new Set<string>(installOptions.migrationItemIds ?? []);

const actualComfyDirectory = ComfyConfigManager.setUpComfyUI(installOptions.installPath);
const actualComfyDirectory = path.join(installOptions.installPath, 'ComfyUI');
ComfyConfigManager.setUpComfyUI(actualComfyDirectory);

const { comfyui: comfyuiConfig, ...extraConfigs } = await ComfyServerConfig.getMigrationConfig(
migrationSource,
Expand Down
25 changes: 5 additions & 20 deletions tests/unit/comfyConfigManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,20 @@ describe('ComfyConfigManager', () => {
});

describe('setUpComfyUI', () => {
it('should use existing directory when it contains ComfyUI structure', () => {
// Mock isComfyUIDirectory to return true for the input path
(fs.existsSync as jest.Mock).mockImplementation((path: string) => {
const requiredDirs = [
'/existing/ComfyUI/models',
'/existing/ComfyUI/input',
'/existing/ComfyUI/user',
'/existing/ComfyUI/output',
'/existing/ComfyUI/custom_nodes',
];
return requiredDirs.includes(path);
});

const result = ComfyConfigManager.setUpComfyUI('/existing/ComfyUI');

expect(result).toBe('/existing/ComfyUI');
it('should reject existing directory when it contains ComfyUI structure', () => {
(fs.existsSync as jest.Mock).mockReturnValue(true);
expect(() => ComfyConfigManager.setUpComfyUI('/existing/ComfyUI')).toThrow();
});

it('should create ComfyUI subdirectory when it is missing', () => {
(fs.existsSync as jest.Mock).mockImplementationOnce((path: string) => {
if (path === '/some/base/path/ComfyUI') {
if (['/some/base/path/ComfyUI'].includes(path)) {
return false;
}
return true;
});

const result = ComfyConfigManager.setUpComfyUI('/some/base/path');

expect(result).toBe('/some/base/path/ComfyUI');
ComfyConfigManager.setUpComfyUI('/some/base/path/ComfyUI');
});
});

Expand Down

0 comments on commit 6f00252

Please sign in to comment.