|
1 | 1 | /** |
2 | 2 | * Claude CLI Tools Configuration Manager |
3 | 3 | * Manages .claude/cli-tools.json (tools) and .claude/cli-settings.json (settings) |
4 | | - * with fallback: |
5 | | - * 1. Project workspace: {projectDir}/.claude/ (priority) |
| 4 | + * |
| 5 | + * Configuration Strategy: |
| 6 | + * - READ: Project → Global → Default (fallback chain) |
| 7 | + * - CREATE: Always in ~/.claude/ (global user-level config) |
| 8 | + * - SAVE: Based on source (project config saves to project, others to global) |
| 9 | + * |
| 10 | + * Read priority: |
| 11 | + * 1. Project workspace: {projectDir}/.claude/ (if exists) |
6 | 12 | * 2. Global: ~/.claude/ (fallback) |
7 | 13 | */ |
8 | 14 | import * as fs from 'fs'; |
@@ -295,38 +301,25 @@ function migrateConfig(config: any, projectDir: string): ClaudeCliToolsConfig { |
295 | 301 |
|
296 | 302 | /** |
297 | 303 | * Ensure CLI tools configuration file exists |
298 | | - * Creates default config if missing (auto-rebuild feature) |
299 | | - * @param projectDir - Project directory path |
300 | | - * @param createInProject - If true, create in project dir; if false, create in global dir |
| 304 | + * Creates default config in global ~/.claude directory (user-level config) |
| 305 | + * @param projectDir - Project directory path (used for reading existing project config) |
| 306 | + * @param createInProject - DEPRECATED: Always creates in global dir. Kept for backward compatibility. |
301 | 307 | * @returns The config that was created/exists |
302 | 308 | */ |
303 | | -export function ensureClaudeCliTools(projectDir: string, createInProject: boolean = true): ClaudeCliToolsConfig & { _source?: string } { |
| 309 | +export function ensureClaudeCliTools(projectDir: string, createInProject: boolean = false): ClaudeCliToolsConfig & { _source?: string } { |
304 | 310 | const resolved = resolveConfigPath(projectDir); |
305 | 311 |
|
306 | 312 | if (resolved.source !== 'default') { |
307 | 313 | // Config exists, load and return it |
308 | 314 | return loadClaudeCliTools(projectDir); |
309 | 315 | } |
310 | 316 |
|
311 | | - // Config doesn't exist - create default |
312 | | - console.log('[claude-cli-tools] Config not found, creating default cli-tools.json'); |
| 317 | + // Config doesn't exist - create in global directory only |
| 318 | + console.log('[claude-cli-tools] Config not found, creating default cli-tools.json in ~/.claude'); |
313 | 319 |
|
314 | 320 | const defaultConfig: ClaudeCliToolsConfig = { ...DEFAULT_TOOLS_CONFIG }; |
315 | 321 |
|
316 | | - if (createInProject) { |
317 | | - // Create in project directory |
318 | | - ensureClaudeDir(projectDir); |
319 | | - const projectPath = getProjectConfigPath(projectDir); |
320 | | - try { |
321 | | - fs.writeFileSync(projectPath, JSON.stringify(defaultConfig, null, 2), 'utf-8'); |
322 | | - console.log(`[claude-cli-tools] Created default config at: ${projectPath}`); |
323 | | - return { ...defaultConfig, _source: 'project' }; |
324 | | - } catch (err) { |
325 | | - console.error('[claude-cli-tools] Failed to create project config:', err); |
326 | | - } |
327 | | - } |
328 | | - |
329 | | - // Fallback: create in global directory |
| 322 | + // Always create in global directory (user-level config) |
330 | 323 | const globalDir = path.join(os.homedir(), '.claude'); |
331 | 324 | if (!fs.existsSync(globalDir)) { |
332 | 325 | fs.mkdirSync(globalDir, { recursive: true }); |
@@ -405,14 +398,28 @@ export function loadClaudeCliTools(projectDir: string): ClaudeCliToolsConfig & { |
405 | 398 | } |
406 | 399 |
|
407 | 400 | /** |
408 | | - * Save CLI tools configuration to project .claude/cli-tools.json |
| 401 | + * Save CLI tools configuration |
| 402 | + * - If config was loaded from project, saves to project |
| 403 | + * - Otherwise saves to global ~/.claude/cli-tools.json |
409 | 404 | */ |
410 | 405 | export function saveClaudeCliTools(projectDir: string, config: ClaudeCliToolsConfig & { _source?: string }): void { |
411 | | - ensureClaudeDir(projectDir); |
412 | | - const configPath = getProjectConfigPath(projectDir); |
413 | | - |
414 | 406 | const { _source, ...configToSave } = config; |
415 | 407 |
|
| 408 | + // Determine save location based on source |
| 409 | + let configPath: string; |
| 410 | + if (_source === 'project') { |
| 411 | + // Config was loaded from project, save back to project |
| 412 | + ensureClaudeDir(projectDir); |
| 413 | + configPath = getProjectConfigPath(projectDir); |
| 414 | + } else { |
| 415 | + // Default: save to global directory |
| 416 | + const globalDir = path.join(os.homedir(), '.claude'); |
| 417 | + if (!fs.existsSync(globalDir)) { |
| 418 | + fs.mkdirSync(globalDir, { recursive: true }); |
| 419 | + } |
| 420 | + configPath = getGlobalConfigPath(); |
| 421 | + } |
| 422 | + |
416 | 423 | try { |
417 | 424 | fs.writeFileSync(configPath, JSON.stringify(configToSave, null, 2), 'utf-8'); |
418 | 425 | console.log(`[claude-cli-tools] Saved tools config to: ${configPath}`); |
|
0 commit comments