From adb6222bc671c3c4ade1bb019705a985de1947fa Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Wed, 24 Dec 2025 18:19:47 -0500 Subject: [PATCH] refactor(paths): use expandPath() consistently for cross-platform path handling - variant-settings.ts: Use expandPath() in deleteSettingsFile() - profile-writer.ts: Use expandPath() in removeApiProfileUnified() - migration-manager.ts: Remove local expandPath(), import from helpers All path expansion now uses the central expandPath() from utils/helpers.ts which handles ~, ~\, ${VAR}, $VAR, and %VAR% on all platforms. --- src/api/services/profile-writer.ts | 6 ++++-- src/cliproxy/services/variant-settings.ts | 6 ++++-- src/config/migration-manager.ts | 8 +------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/api/services/profile-writer.ts b/src/api/services/profile-writer.ts index e48ff6fa..fbc42a71 100644 --- a/src/api/services/profile-writer.ts +++ b/src/api/services/profile-writer.ts @@ -6,6 +6,7 @@ import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import { getCcsDir, getConfigPath, loadConfig } from '../../utils/config-manager'; +import { expandPath } from '../../utils/helpers'; import { loadOrCreateUnifiedConfig, saveUnifiedConfig, @@ -144,9 +145,10 @@ function removeApiProfileUnified(name: string): void { throw new Error(`API profile not found: ${name}`); } - // Delete the settings file if it exists + // Delete the settings file if it exists. + // Uses expandPath() for cross-platform path handling. if (profile.settings) { - const settingsPath = profile.settings.replace(/^~/, os.homedir()); + const settingsPath = expandPath(profile.settings); if (fs.existsSync(settingsPath)) { fs.unlinkSync(settingsPath); } diff --git a/src/cliproxy/services/variant-settings.ts b/src/cliproxy/services/variant-settings.ts index b3c4b185..47afe52f 100644 --- a/src/cliproxy/services/variant-settings.ts +++ b/src/cliproxy/services/variant-settings.ts @@ -10,6 +10,7 @@ import * as path from 'path'; import * as os from 'os'; import { CLIProxyProfileName } from '../../auth/profile-detector'; import { getCcsDir } from '../../utils/config-manager'; +import { expandPath } from '../../utils/helpers'; import { getClaudeEnvVars, CLIPROXY_DEFAULT_PORT } from '../config-generator'; import { CLIProxyProvider } from '../types'; @@ -124,10 +125,11 @@ export function createSettingsFileUnified( } /** - * Delete settings file if it exists + * Delete settings file if it exists. + * Uses expandPath() for cross-platform path handling. */ export function deleteSettingsFile(settingsPath: string): boolean { - const resolvedPath = settingsPath.replace(/^~/, os.homedir()); + const resolvedPath = expandPath(settingsPath); if (fs.existsSync(resolvedPath)) { fs.unlinkSync(resolvedPath); return true; diff --git a/src/config/migration-manager.ts b/src/config/migration-manager.ts index e45245d7..9cc4cb43 100644 --- a/src/config/migration-manager.ts +++ b/src/config/migration-manager.ts @@ -15,6 +15,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { getCcsDir } from '../utils/config-manager'; +import { expandPath } from '../utils/helpers'; import type { ProfileConfig, AccountConfig, CLIProxyVariantConfig } from './unified-config-types'; import { createEmptyUnifiedConfig } from './unified-config-types'; import { saveUnifiedConfig, hasUnifiedConfig, loadUnifiedConfig } from './unified-config-loader'; @@ -311,13 +312,6 @@ function readJsonSafe(filePath: string): Record | null { } } -/** - * Expand ~ to home directory in path. - */ -function expandPath(p: string): string { - return p.replace(/^~/, process.env.HOME || ''); -} - /** * Move file if it exists. Returns true if moved, false if source didn't exist. */