From 0ab496c51a895442ef7d3a7daacad2d5e137ef77 Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:02:33 -0500 Subject: [PATCH 01/11] feat: add local settings path helpers for settings.local.json Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils/__tests__/claude-settings.test.ts | 23 +++++++++++++++++++++ src/utils/claude-settings.ts | 15 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 702d04e22..a25da4a38 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -14,8 +14,10 @@ import { import { DEFAULT_SETTINGS } from '../../types/Settings'; import { CCSTATUSLINE_COMMANDS, + getClaudeLocalSettingsPath, getClaudeSettingsPath, getExistingStatusLine, + hasLocalSettings, installStatusLine, isInstalled, isKnownCommand, @@ -108,6 +110,27 @@ describe('isKnownCommand', () => { }); }); +describe('local settings path helpers', () => { + it('getClaudeLocalSettingsPath should return settings.local.json in config dir', () => { + expect(getClaudeLocalSettingsPath()).toBe( + path.join(testClaudeConfigDir, 'settings.local.json') + ); + }); + + it('hasLocalSettings should return false when file does not exist', () => { + expect(hasLocalSettings()).toBe(false); + }); + + it('hasLocalSettings should return true when file exists', () => { + fs.writeFileSync( + path.join(testClaudeConfigDir, 'settings.local.json'), + '{}', + 'utf-8' + ); + expect(hasLocalSettings()).toBe(true); + }); +}); + describe('buildCommand via installStatusLine', () => { it('should use base command when no custom config path', async () => { initConfigPath(); diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index e12e10f46..af503f82e 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -93,6 +93,21 @@ export function getClaudeSettingsPath(): string { return path.join(getClaudeConfigDir(), 'settings.json'); } +/** + * Gets the full path to the Claude settings.local.json file. + * Claude Code merges this file on top of settings.json. + */ +export function getClaudeLocalSettingsPath(): string { + return path.join(getClaudeConfigDir(), 'settings.local.json'); +} + +/** + * Checks whether a settings.local.json file exists in the Claude config directory. + */ +export function hasLocalSettings(): boolean { + return fs.existsSync(getClaudeLocalSettingsPath()); +} + /** * Creates a backup of the current Claude settings file. */ From 390531ad9e5a6691bc2899186f76d0923849fe21 Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:04:15 -0500 Subject: [PATCH 02/11] feat: add file-path parameter to Claude settings load/save Co-Authored-By: Claude Sonnet 4.6 --- src/utils/__tests__/claude-settings.test.ts | 41 +++++++++++++++++++++ src/utils/claude-settings.ts | 24 ++++++------ 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index a25da4a38..a9efd28d8 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -378,4 +378,45 @@ describe('backup and error handling behavior', () => { await expect(isInstalled()).resolves.toBe(true); }); +}); + +describe('file-path-aware load/save', () => { + it('loadClaudeSettings should load from specified filePath', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { type: 'command', command: 'local-command', padding: 0 } + }), 'utf-8'); + + const settings = await loadClaudeSettings({ filePath: localPath }); + expect(settings.statusLine?.command).toBe('local-command'); + }); + + it('loadClaudeSettings should return empty object when specified filePath does not exist', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const settings = await loadClaudeSettings({ filePath: localPath }); + expect(settings).toEqual({}); + }); + + it('saveClaudeSettings should save to specified filePath', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + await saveClaudeSettings({ + statusLine: { type: 'command', command: 'test-cmd', padding: 0 } + }, localPath); + + const content = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: { command?: string } }; + expect(content.statusLine?.command).toBe('test-cmd'); + }); + + it('saveClaudeSettings should create backup for specified filePath', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ existing: true }), 'utf-8'); + + await saveClaudeSettings({ + statusLine: { type: 'command', command: 'new-cmd', padding: 0 } + }, localPath); + + expect(fs.existsSync(`${localPath}.bak`)).toBe(true); + const backup = JSON.parse(fs.readFileSync(`${localPath}.bak`, 'utf-8')) as { existing?: boolean }; + expect(backup.existing).toBe(true); + }); }); \ No newline at end of file diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index af503f82e..aec075eb7 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -109,10 +109,9 @@ export function hasLocalSettings(): boolean { } /** - * Creates a backup of the current Claude settings file. + * Creates a backup of the given Claude settings file. */ -async function backupClaudeSettings(suffix = '.bak'): Promise { - const settingsPath = getClaudeSettingsPath(); +async function backupClaudeSettings(settingsPath: string, suffix = '.bak'): Promise { const backupPath = settingsPath + suffix; try { if (fs.existsSync(settingsPath)) { @@ -127,11 +126,11 @@ async function backupClaudeSettings(suffix = '.bak'): Promise { return null; } -interface LoadClaudeSettingsOptions { logErrors?: boolean } +interface LoadClaudeSettingsOptions { logErrors?: boolean; filePath?: string } export function loadClaudeSettingsSync(options: LoadClaudeSettingsOptions = {}): ClaudeSettings { - const { logErrors = true } = options; - const settingsPath = getClaudeSettingsPath(); + const { logErrors = true, filePath } = options; + const settingsPath = filePath ?? getClaudeSettingsPath(); // File doesn't exist - return empty object if (!fs.existsSync(settingsPath)) { @@ -150,8 +149,8 @@ export function loadClaudeSettingsSync(options: LoadClaudeSettingsOptions = {}): } export async function loadClaudeSettings(options: LoadClaudeSettingsOptions = {}): Promise { - const { logErrors = true } = options; - const settingsPath = getClaudeSettingsPath(); + const { logErrors = true, filePath } = options; + const settingsPath = filePath ?? getClaudeSettingsPath(); // File doesn't exist - return empty object if (!fs.existsSync(settingsPath)) { @@ -170,13 +169,14 @@ export async function loadClaudeSettings(options: LoadClaudeSettingsOptions = {} } export async function saveClaudeSettings( - settings: ClaudeSettings + settings: ClaudeSettings, + filePath?: string ): Promise { - const settingsPath = getClaudeSettingsPath(); + const settingsPath = filePath ?? getClaudeSettingsPath(); const dir = path.dirname(settingsPath); // Backup settings before overwriting - await backupClaudeSettings(); + await backupClaudeSettings(settingsPath); await mkdir(dir, { recursive: true }); await writeFile(settingsPath, JSON.stringify(settings, null, 2), 'utf-8'); @@ -239,7 +239,7 @@ async function loadSavedSettingsForHookSync(): Promise { export async function installStatusLine(useBunx = false): Promise { let settings: ClaudeSettings; - const backupPath = await backupClaudeSettings('.orig'); + const backupPath = await backupClaudeSettings(getClaudeSettingsPath(), '.orig'); try { settings = await loadClaudeSettings({ logErrors: false }); } catch { From d4c857c41afbe599e7f7130168cb9475e226f8aa Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:06:32 -0500 Subject: [PATCH 03/11] feat: detect statusLine in both settings.json and settings.local.json Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils/__tests__/claude-settings.test.ts | 95 +++++++++++++++++++++ src/utils/claude-settings.ts | 77 +++++++++++++---- 2 files changed, 156 insertions(+), 16 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index a9efd28d8..48dfb6aa0 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -14,6 +14,7 @@ import { import { DEFAULT_SETTINGS } from '../../types/Settings'; import { CCSTATUSLINE_COMMANDS, + getActiveClaudeSettingsPath, getClaudeLocalSettingsPath, getClaudeSettingsPath, getExistingStatusLine, @@ -43,6 +44,11 @@ function writeRawClaudeSettings(content: string): void { fs.writeFileSync(settingsPath, content, 'utf-8'); } +function writeLocalClaudeSettings(content: string): void { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, content, 'utf-8'); +} + beforeEach(() => { testClaudeConfigDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-claude-settings-')); process.env.CLAUDE_CONFIG_DIR = testClaudeConfigDir; @@ -419,4 +425,93 @@ describe('file-path-aware load/save', () => { const backup = JSON.parse(fs.readFileSync(`${localPath}.bak`, 'utf-8')) as { existing?: boolean }; expect(backup.existing).toBe(true); }); +}); + +describe('dual-file detection', () => { + it('isInstalled should detect installation in settings.local.json', async () => { + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + await expect(isInstalled()).resolves.toBe(true); + }); + + it('isInstalled should prefer local over global', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'other-command', padding: 0 } + })); + // Local has non-ccstatusline command, so isInstalled should be false + await expect(isInstalled()).resolves.toBe(false); + }); + + it('isInstalled should fall back to global when local has no statusLine', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({})); + await expect(isInstalled()).resolves.toBe(true); + }); + + it('getExistingStatusLine should return command from local first', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'global-command' } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'local-command' } + })); + await expect(getExistingStatusLine()).resolves.toBe('local-command'); + }); + + it('getExistingStatusLine should fall back to global', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'global-command' } + })); + await expect(getExistingStatusLine()).resolves.toBe('global-command'); + }); + + it('getActiveClaudeSettingsPath should return local path when local has statusLine', () => { + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'local-command' } + })); + expect(getActiveClaudeSettingsPath()).toBe( + path.join(testClaudeConfigDir, 'settings.local.json') + ); + }); + + it('getActiveClaudeSettingsPath should return global path when local has no statusLine', () => { + writeLocalClaudeSettings(JSON.stringify({})); + expect(getActiveClaudeSettingsPath()).toBe( + path.join(testClaudeConfigDir, 'settings.json') + ); + }); + + it('getActiveClaudeSettingsPath should return global path when no local file', () => { + expect(getActiveClaudeSettingsPath()).toBe( + path.join(testClaudeConfigDir, 'settings.json') + ); + }); + + it('should fall through to global when local file is malformed JSON', async () => { + writeLocalClaudeSettings('{ invalid json'); + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + await expect(isInstalled()).resolves.toBe(true); + await expect(getExistingStatusLine()).resolves.toBe(CCSTATUSLINE_COMMANDS.NPM); + }); + + it('should treat ccstatusline as inactive when local has a different statusLine', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'some-other-tool' } + })); + // Local wins — and local is not ccstatusline + await expect(isInstalled()).resolves.toBe(false); + // But getExistingStatusLine returns whatever is effective + await expect(getExistingStatusLine()).resolves.toBe('some-other-tool'); + }); }); \ No newline at end of file diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index aec075eb7..645e948b4 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -108,6 +108,53 @@ export function hasLocalSettings(): boolean { return fs.existsSync(getClaudeLocalSettingsPath()); } +/** + * Private resolver — single source of truth for dual-file statusLine precedence. + * Checks settings.local.json first (matching Claude Code's merge semantics), + * falls back to settings.json. + */ +interface ResolvedStatusLine { + command: string | null; + sourcePath: string; + isLocal: boolean; +} + +function resolveStatusLine(): ResolvedStatusLine { + const localPath = getClaudeLocalSettingsPath(); + if (fs.existsSync(localPath)) { + try { + const content = fs.readFileSync(localPath, 'utf-8'); + const data = JSON.parse(content) as ClaudeSettings; + if (data.statusLine?.command) { + return { command: data.statusLine.command, sourcePath: localPath, isLocal: true }; + } + } catch { + // Malformed local file — fall through to global + } + } + // Check global file + const globalPath = getClaudeSettingsPath(); + if (fs.existsSync(globalPath)) { + try { + const content = fs.readFileSync(globalPath, 'utf-8'); + const data = JSON.parse(content) as ClaudeSettings; + if (data.statusLine?.command) { + return { command: data.statusLine.command, sourcePath: globalPath, isLocal: false }; + } + } catch { + // fall through + } + } + return { command: null, sourcePath: globalPath, isLocal: false }; +} + +/** + * Returns the path to the Claude settings file that currently contains a statusLine entry. + */ +export function getActiveClaudeSettingsPath(): string { + return resolveStatusLine().sourcePath; +} + /** * Creates a backup of the given Claude settings file. */ @@ -183,20 +230,23 @@ export async function saveClaudeSettings( } export async function isInstalled(): Promise { - let settings: ClaudeSettings; + const resolved = resolveStatusLine(); + if (!resolved.command) { + return false; + } + // Load the full settings from the resolved file to check padding try { - settings = await loadClaudeSettings({ logErrors: false }); + const settings = await loadClaudeSettings({ logErrors: false, filePath: resolved.sourcePath }); + const command = settings.statusLine?.command ?? ''; + return ( + isKnownCommand(command) + && (settings.statusLine?.padding === 0 + || settings.statusLine?.padding === undefined) + ); } catch { - return false; // Can't determine if installed, assume not + return false; } - const command = settings.statusLine?.command ?? ''; - - return ( - isKnownCommand(command) - && (settings.statusLine?.padding === 0 - || settings.statusLine?.padding === undefined) - ); } export function isBunxAvailable(): boolean { @@ -292,10 +342,5 @@ export async function uninstallStatusLine(): Promise { } export async function getExistingStatusLine(): Promise { - try { - const settings = await loadClaudeSettings({ logErrors: false }); - return settings.statusLine?.command ?? null; - } catch { - return null; // Can't read settings, return null - } + return resolveStatusLine().command; } \ No newline at end of file From 72c746938d18dd8b425cfa792a13a61cc70bf01f Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:08:48 -0500 Subject: [PATCH 04/11] feat: allow installStatusLine to target a specific settings file Adds optional `targetPath` parameter to `installStatusLine` so it can write to settings.local.json (or any path) instead of the default settings.json. Hook sync is skipped for targeted writes pending Task 6. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils/__tests__/claude-settings.test.ts | 30 +++++++++++++++++++++ src/utils/claude-settings.ts | 25 ++++++++++------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 48dfb6aa0..2fbe66a7a 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -514,4 +514,34 @@ describe('dual-file detection', () => { // But getExistingStatusLine returns whatever is effective await expect(getExistingStatusLine()).resolves.toBe('some-other-tool'); }); +}); + +describe('file-targeted installStatusLine', () => { + it('should install to settings.local.json when targetPath specified', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + await installStatusLine(false, localPath); + + const content = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: { command?: string } }; + expect(content.statusLine?.command).toBe(CCSTATUSLINE_COMMANDS.NPM); + + // settings.json should not have been created + const globalPath = getClaudeSettingsPath(); + expect(fs.existsSync(globalPath)).toBe(false); + }); + + it('should create .orig backup for targeted file', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ existing: 'data' }), 'utf-8'); + + await installStatusLine(false, localPath); + + expect(fs.existsSync(`${localPath}.orig`)).toBe(true); + const orig = JSON.parse(fs.readFileSync(`${localPath}.orig`, 'utf-8')) as { existing?: string }; + expect(orig.existing).toBe('data'); + }); + + it('should still install to settings.json by default', async () => { + await installStatusLine(false); + expect(readInstalledCommand()).toBe(CCSTATUSLINE_COMMANDS.NPM); + }); }); \ No newline at end of file diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index 645e948b4..8c02b0c3a 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -286,14 +286,15 @@ async function loadSavedSettingsForHookSync(): Promise { } } -export async function installStatusLine(useBunx = false): Promise { - let settings: ClaudeSettings; +export async function installStatusLine(useBunx = false, targetPath?: string): Promise { + const settingsPath = targetPath ?? getClaudeSettingsPath(); - const backupPath = await backupClaudeSettings(getClaudeSettingsPath(), '.orig'); + const backupPath = await backupClaudeSettings(settingsPath, '.orig'); + let settings: ClaudeSettings; try { - settings = await loadClaudeSettings({ logErrors: false }); + settings = await loadClaudeSettings({ logErrors: false, filePath: settingsPath }); } catch { - const fallbackBackupPath = `${getClaudeSettingsPath()}.orig`; + const fallbackBackupPath = `${settingsPath}.orig`; console.error(`Warning: Could not read existing Claude settings. A backup exists at ${backupPath ?? fallbackBackupPath}.`); settings = {}; } @@ -309,12 +310,16 @@ export async function installStatusLine(useBunx = false): Promise { padding: 0 }; - await saveClaudeSettings(settings); + await saveClaudeSettings(settings, settingsPath); - const savedSettings = await loadSavedSettingsForHookSync(); - if (savedSettings) { - const { syncWidgetHooks } = await import('./hooks'); - await syncWidgetHooks(savedSettings); + // Sync hooks — when installing to a non-default target, defer to Task 6's + // file-aware hook sync which will correctly target the active file. + if (!targetPath) { + const savedSettings = await loadSavedSettingsForHookSync(); + if (savedSettings) { + const { syncWidgetHooks } = await import('./hooks'); + await syncWidgetHooks(savedSettings); + } } } From af095396a8f802407a65070f94d1b55db5e22efb Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:12:42 -0500 Subject: [PATCH 05/11] feat: uninstallStatusLine cleans both settings files --- src/utils/__tests__/claude-settings.test.ts | 83 +++++++++++++++++++++ src/utils/claude-settings.ts | 26 +++++-- 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 2fbe66a7a..0c4706b27 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -544,4 +544,87 @@ describe('file-targeted installStatusLine', () => { await installStatusLine(false); expect(readInstalledCommand()).toBe(CCSTATUSLINE_COMMANDS.NPM); }); +}); + +describe('dual-file uninstallStatusLine', () => { + it('should remove statusLine from settings.local.json', async () => { + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + + await uninstallStatusLine(); + + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const content = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: unknown }; + expect(content.statusLine).toBeUndefined(); + }); + + it('should remove statusLine from both files when both have it', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.BUNX, padding: 0 } + })); + + await uninstallStatusLine(); + + const globalContent = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as { statusLine?: unknown }; + expect(globalContent.statusLine).toBeUndefined(); + + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const localContent = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: unknown }; + expect(localContent.statusLine).toBeUndefined(); + }); + + it('should not error when local file does not exist', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + + await expect(uninstallStatusLine()).resolves.toBeUndefined(); + + const content = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as { statusLine?: unknown }; + expect(content.statusLine).toBeUndefined(); + }); + + it('should preserve non-ccstatusline statusLine in settings.json', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'company-managed-tool', padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + + await uninstallStatusLine(); + + // Our entry in local should be removed + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const localContent = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: unknown }; + expect(localContent.statusLine).toBeUndefined(); + + // Company's entry in global should survive + const globalContent = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as { statusLine?: { command?: string } }; + expect(globalContent.statusLine?.command).toBe('company-managed-tool'); + }); + + it('should preserve non-ccstatusline statusLine in settings.local.json', async () => { + writeRawClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } + })); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { type: 'command', command: 'user-custom-tool' } + })); + + await uninstallStatusLine(); + + // Our entry in global should be removed + const globalContent = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as { statusLine?: unknown }; + expect(globalContent.statusLine).toBeUndefined(); + + // User's custom entry in local should survive + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const localContent = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: { command?: string } }; + expect(localContent.statusLine?.command).toBe('user-custom-tool'); + }); }); \ No newline at end of file diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index 8c02b0c3a..80e59bf40 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -324,20 +324,32 @@ export async function installStatusLine(useBunx = false, targetPath?: string): P } export async function uninstallStatusLine(): Promise { - let settings: ClaudeSettings; - + // Remove from settings.json — only if it's ours try { - settings = await loadClaudeSettings({ logErrors: false }); + const settings = await loadClaudeSettings({ logErrors: false }); + if (settings.statusLine?.command && isKnownCommand(settings.statusLine.command)) { + delete settings.statusLine; + await saveClaudeSettings(settings); + } } catch { console.error('Warning: Could not read existing Claude settings.'); - return; // if we can't read, return... what are we uninstalling? } - if (settings.statusLine) { - delete settings.statusLine; - await saveClaudeSettings(settings); + // Also remove from settings.local.json — only if it's ours + const localPath = getClaudeLocalSettingsPath(); + if (fs.existsSync(localPath)) { + try { + const localSettings = await loadClaudeSettings({ logErrors: false, filePath: localPath }); + if (localSettings.statusLine?.command && isKnownCommand(localSettings.statusLine.command)) { + delete localSettings.statusLine; + await saveClaudeSettings(localSettings, localPath); + } + } catch { + // Ignore errors with local file during uninstall + } } + // Clean managed hooks from both files (stripManagedHooks already only removes _tag: ccstatusline-managed) try { const { removeManagedHooks } = await import('./hooks'); await removeManagedHooks(); From 86f409e3e97e9817625c58db700cd209accb63ef Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:15:05 -0500 Subject: [PATCH 06/11] feat: hook sync targets the file where statusLine lives --- src/utils/__tests__/hooks.test.ts | 94 +++++++++++++++++++++++++++++++ src/utils/claude-settings.ts | 12 ++-- src/utils/hooks.ts | 54 ++++++++++++++---- 3 files changed, 142 insertions(+), 18 deletions(-) diff --git a/src/utils/__tests__/hooks.test.ts b/src/utils/__tests__/hooks.test.ts index f1a8441bd..9a39c5223 100644 --- a/src/utils/__tests__/hooks.test.ts +++ b/src/utils/__tests__/hooks.test.ts @@ -11,6 +11,7 @@ import { } from 'vitest'; import { DEFAULT_SETTINGS } from '../../types/Settings'; +import { CCSTATUSLINE_COMMANDS } from '../claude-settings'; import { syncWidgetHooks } from '../hooks'; const ORIGINAL_CLAUDE_CONFIG_DIR = process.env.CLAUDE_CONFIG_DIR; @@ -76,4 +77,97 @@ describe('syncWidgetHooks', () => { ] }); }); + + it('syncs hooks to settings.local.json when statusLine lives there', async () => { + // Write statusLine to local file, not global + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { + type: 'command', + command: CCSTATUSLINE_COMMANDS.NPM, + padding: 0 + } + }, null, 2), 'utf-8'); + + const settingsWithSkills = { + ...DEFAULT_SETTINGS, + lines: [[{ id: 'skills-1', type: 'skills' }], [], []] + }; + + await syncWidgetHooks(settingsWithSkills); + + // Hooks should be written to local file, not global + const localSaved = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { hooks?: Record }; + expect(localSaved.hooks?.PreToolUse).toBeDefined(); + + // Global should not have hooks + const globalPath = path.join(testClaudeConfigDir, 'settings.json'); + if (fs.existsSync(globalPath)) { + const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record }; + expect(globalSaved.hooks?.PreToolUse).toBeUndefined(); + } + }); + + it('subsequent sync does not recreate hooks in old file', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { + type: 'command', + command: CCSTATUSLINE_COMMANDS.NPM, + padding: 0 + } + }, null, 2), 'utf-8'); + + const settingsWithSkills = { + ...DEFAULT_SETTINGS, + lines: [[{ id: 'skills-1', type: 'skills' }], [], []] + }; + + // First sync + await syncWidgetHooks(settingsWithSkills); + // Second sync (simulates saving settings again) + await syncWidgetHooks(settingsWithSkills); + + // Global should still have no managed hooks + const globalPath = path.join(testClaudeConfigDir, 'settings.json'); + if (fs.existsSync(globalPath)) { + const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record }; + const hasManagedHooks = Object.values(globalSaved.hooks ?? {}).some( + (entries: unknown[]) => (entries as Array<{ _tag?: string }>).some(e => e._tag === 'ccstatusline-managed') + ); + expect(hasManagedHooks).toBe(false); + } + }); + + it('cleans stale managed hooks from non-active file', async () => { + // Global file has stale managed hooks + const globalPath = path.join(testClaudeConfigDir, 'settings.json'); + fs.writeFileSync(globalPath, JSON.stringify({ + hooks: { + PreToolUse: [ + { + _tag: 'ccstatusline-managed', + matcher: 'Skill', + hooks: [{ type: 'command', command: 'old-command --hook' }] + } + ] + } + }, null, 2), 'utf-8'); + + // StatusLine is in local file + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { + type: 'command', + command: CCSTATUSLINE_COMMANDS.NPM, + padding: 0 + } + }, null, 2), 'utf-8'); + + await syncWidgetHooks(DEFAULT_SETTINGS); + + // Stale hooks should be removed from global + const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record }; + expect(globalSaved.hooks).toBeUndefined(); + }); }); \ No newline at end of file diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index 80e59bf40..b02cc878f 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -312,14 +312,10 @@ export async function installStatusLine(useBunx = false, targetPath?: string): P await saveClaudeSettings(settings, settingsPath); - // Sync hooks — when installing to a non-default target, defer to Task 6's - // file-aware hook sync which will correctly target the active file. - if (!targetPath) { - const savedSettings = await loadSavedSettingsForHookSync(); - if (savedSettings) { - const { syncWidgetHooks } = await import('./hooks'); - await syncWidgetHooks(savedSettings); - } + const savedSettings = await loadSavedSettingsForHookSync(); + if (savedSettings) { + const { syncWidgetHooks } = await import('./hooks'); + await syncWidgetHooks(savedSettings); } } diff --git a/src/utils/hooks.ts b/src/utils/hooks.ts index 7471b5146..c6183637d 100644 --- a/src/utils/hooks.ts +++ b/src/utils/hooks.ts @@ -1,7 +1,12 @@ +import * as fs from 'fs'; + import type { Settings } from '../types/Settings'; import type { Widget } from '../types/Widget'; import { + getActiveClaudeSettingsPath, + getClaudeLocalSettingsPath, + getClaudeSettingsPath, getExistingStatusLine, loadClaudeSettings, saveClaudeSettings @@ -54,7 +59,28 @@ function getActiveHookDefs(settings: Settings): WidgetHookDef[] { export async function syncWidgetHooks(settings: Settings): Promise { const needed = getActiveHookDefs(settings); - const claudeSettings = await loadClaudeSettings({ logErrors: false }); + const activePath = getActiveClaudeSettingsPath(); + + // Clean stale managed hooks from the non-active file to prevent duplicates + const globalPath = getClaudeSettingsPath(); + const localPath = getClaudeLocalSettingsPath(); + const nonActivePath = activePath === localPath ? globalPath : localPath; + if (fs.existsSync(nonActivePath)) { + try { + const otherSettings = await loadClaudeSettings({ logErrors: false, filePath: nonActivePath }); + if (Object.keys(otherSettings).length > 0) { + const otherHooks = (otherSettings.hooks ?? {}) as Record; + if (Object.values(otherHooks).some(entries => entries.some(e => e._tag === HOOK_TAG))) { + stripManagedHooks(otherHooks); + otherSettings.hooks = Object.keys(otherHooks).length > 0 ? otherHooks : undefined; + await saveClaudeSettings(otherSettings, nonActivePath); + } + } + } catch { /* ignore cleanup errors */ } + } + + // Load, strip, and re-add hooks on the active file + const claudeSettings = await loadClaudeSettings({ logErrors: false, filePath: activePath }); const hooks = (claudeSettings.hooks ?? {}) as Record; // Remove all ccstatusline-managed hooks @@ -63,7 +89,7 @@ export async function syncWidgetHooks(settings: Settings): Promise { const statusCommand = await getExistingStatusLine(); if (!statusCommand) { claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined; - await saveClaudeSettings(claudeSettings); + await saveClaudeSettings(claudeSettings, activePath); return; } const hookCommand = `${statusCommand} --hook`; @@ -82,15 +108,23 @@ export async function syncWidgetHooks(settings: Settings): Promise { } claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined; - await saveClaudeSettings(claudeSettings); + await saveClaudeSettings(claudeSettings, activePath); } export async function removeManagedHooks(): Promise { - const claudeSettings = await loadClaudeSettings({ logErrors: false }); - const hooks = (claudeSettings.hooks ?? {}) as Record; - - stripManagedHooks(hooks); - - claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined; - await saveClaudeSettings(claudeSettings); + // Clean managed hooks from both settings files + for (const filePath of [getClaudeSettingsPath(), getClaudeLocalSettingsPath()]) { + if (!fs.existsSync(filePath)) { + continue; + } + try { + const claudeSettings = await loadClaudeSettings({ logErrors: false, filePath }); + const hooks = (claudeSettings.hooks ?? {}) as Record; + if (Object.values(hooks).some(entries => entries.some(e => e._tag === HOOK_TAG))) { + stripManagedHooks(hooks); + claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined; + await saveClaudeSettings(claudeSettings, filePath); + } + } catch { /* ignore cleanup errors */ } + } } \ No newline at end of file From 72f407c5a7c723bdf462ca7b9216548c95f6d857 Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:16:11 -0500 Subject: [PATCH 07/11] feat: add FileTargetSelect component for install target choice --- src/tui/components/FileTargetSelect.tsx | 81 +++++++++++++++++++++++++ src/tui/components/index.ts | 1 + 2 files changed, 82 insertions(+) create mode 100644 src/tui/components/FileTargetSelect.tsx diff --git a/src/tui/components/FileTargetSelect.tsx b/src/tui/components/FileTargetSelect.tsx new file mode 100644 index 000000000..2d422663a --- /dev/null +++ b/src/tui/components/FileTargetSelect.tsx @@ -0,0 +1,81 @@ +import * as fs from 'fs'; + +import { + Box, + Text, + useInput +} from 'ink'; +import React from 'react'; + +import type { ClaudeSettings } from '../../types/ClaudeSettings'; +import { + getClaudeLocalSettingsPath, + getClaudeSettingsPath +} from '../../utils/claude-settings'; + +import { List } from './List'; + +export interface FileTargetSelectProps { + onSelect: (targetPath: string) => void; + onCancel: () => void; +} + +export const FileTargetSelect: React.FC = ({ + onSelect, + onCancel +}) => { + useInput((_, key) => { + if (key.escape) { + onCancel(); + } + }); + + const globalPath = getClaudeSettingsPath(); + const localPath = getClaudeLocalSettingsPath(); + + // Check if local already has a statusLine — installing to global would be overridden + let localHasStatusLine = false; + try { + const content = fs.readFileSync(localPath, 'utf-8'); + const data = JSON.parse(content) as ClaudeSettings; + localHasStatusLine = !!data.statusLine?.command; + } catch { + // file unreadable — no warning needed + } + + const items = [ + { + label: 'settings.json', + sublabel: localHasStatusLine + ? '(will be overridden by settings.local.json)' + : '(default)', + value: 'global' + }, + { label: 'settings.local.json', sublabel: '(local override)', value: 'local' } + ]; + + return ( + + settings.local.json detected + + Select which file to install to: + + { + if (value === 'back') { + onCancel(); + return; + } + onSelect(value === 'local' ? localPath : globalPath); + }} + showBackButton={true} + /> + + Press Enter to select, ESC to go back + + + ); +}; diff --git a/src/tui/components/index.ts b/src/tui/components/index.ts index 34afc2e68..5ecd934c2 100644 --- a/src/tui/components/index.ts +++ b/src/tui/components/index.ts @@ -1,6 +1,7 @@ // Barrel file - exports all components and their types export * from './ColorMenu'; export * from './ConfirmDialog'; +export * from './FileTargetSelect'; export * from './GlobalOverridesMenu'; export * from './InstallMenu'; export * from './ItemsEditor'; From 7ac95acb92d0c3868c13dd06d4b8109d43f61ff6 Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:22:36 -0500 Subject: [PATCH 08/11] feat: TUI install flow supports settings.local.json target selection Co-Authored-By: Claude Sonnet 4.6 --- src/tui/App.tsx | 79 +++++++++++++++++++++---- src/tui/components/FileTargetSelect.tsx | 3 +- src/tui/components/InstallMenu.tsx | 10 ++-- 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/tui/App.tsx b/src/tui/App.tsx index aead5585e..3e4a678ea 100644 --- a/src/tui/App.tsx +++ b/src/tui/App.tsx @@ -1,4 +1,5 @@ import chalk from 'chalk'; +import * as fs from 'fs'; import { Box, Text, @@ -17,8 +18,11 @@ import type { Settings } from '../types/Settings'; import type { WidgetItem } from '../types/Widget'; import { CCSTATUSLINE_COMMANDS, + getActiveClaudeSettingsPath, + getClaudeLocalSettingsPath, getClaudeSettingsPath, getExistingStatusLine, + hasLocalSettings, installStatusLine, isBunxAvailable, isInstalled, @@ -44,6 +48,7 @@ import { getPackageVersion } from '../utils/terminal'; import { ColorMenu, ConfirmDialog, + FileTargetSelect, GlobalOverridesMenu, InstallMenu, ItemsEditor, @@ -73,7 +78,8 @@ type AppScreen = 'main' | 'globalOverrides' | 'confirm' | 'powerline' - | 'install'; + | 'install' + | 'fileTarget'; interface ConfirmDialogState { message: string; @@ -112,6 +118,13 @@ export const App: React.FC = () => { const [existingStatusLine, setExistingStatusLine] = useState(null); const [flashMessage, setFlashMessage] = useState(null); const [previewIsTruncated, setPreviewIsTruncated] = useState(false); + const [localSettingsDetected, setLocalSettingsDetected] = useState(false); + const [pendingInstall, setPendingInstall] = useState<{ + command: string; + displayName: string; + useBunx: boolean; + targetPath?: string; + } | null>(null); useEffect(() => { // Load existing status line @@ -124,6 +137,7 @@ export const App: React.FC = () => { setOriginalSettings(cloneSettings(loadedSettings)); }); void isInstalled().then(setIsClaudeInstalled); + setLocalSettingsDetected(hasLocalSettings()); // Check for Powerline fonts on startup (use sync version that doesn't call execSync) const fontStatus = checkPowerlineFonts(); @@ -180,24 +194,39 @@ export const App: React.FC = () => { } }); - const handleInstallSelection = useCallback((command: string, displayName: string, useBunx: boolean) => { + const handleInstallSelection = useCallback((command: string, displayName: string, useBunx: boolean, targetPath?: string) => { + const effectiveTarget = targetPath ?? getClaudeSettingsPath(); + void getExistingStatusLine().then((existing) => { const isAlreadyInstalled = isKnownCommand(existing ?? ''); let message: string; if (existing && !isAlreadyInstalled) { - message = `This will modify ${getClaudeSettingsPath()}\n\nA status line is already configured: "${existing}"\nReplace it with ${command}?`; + message = `This will modify ${effectiveTarget}\n\nA status line is already configured: "${existing}"\nReplace it with ${command}?`; } else if (isAlreadyInstalled) { - message = `ccstatusline is already installed in ${getClaudeSettingsPath()}\nUpdate it with ${command}?`; + message = `ccstatusline is already installed in ${getActiveClaudeSettingsPath()}\nUpdate it with ${command} in ${effectiveTarget}?`; } else { - message = `This will modify ${getClaudeSettingsPath()} to add ccstatusline with ${displayName}.\nContinue?`; + message = `This will modify ${effectiveTarget} to add ccstatusline with ${displayName}.\nContinue?`; + } + + // Warn if installing to global while local has a statusLine (install won't take effect) + if (targetPath !== getClaudeLocalSettingsPath() && hasLocalSettings()) { + try { + const localContent = fs.readFileSync(getClaudeLocalSettingsPath(), 'utf-8'); + const localData = JSON.parse(localContent) as { statusLine?: { command?: string } }; + if (localData.statusLine?.command) { + message += '\n\n\u26a0 settings.local.json has a statusLine entry that will take precedence over this file. The install may not take effect.'; + } + } catch { + // Can't read local — skip warning + } } setConfirmDialog({ message, cancelScreen: 'install', action: async () => { - await installStatusLine(useBunx); + await installStatusLine(useBunx, targetPath); setIsClaudeInstalled(true); setExistingStatusLine(command); setScreen('main'); @@ -210,13 +239,23 @@ export const App: React.FC = () => { const handleNpxInstall = useCallback(() => { setMenuSelections(prev => ({ ...prev, install: 0 })); - handleInstallSelection(CCSTATUSLINE_COMMANDS.NPM, 'npx', false); - }, [handleInstallSelection]); + if (localSettingsDetected) { + setPendingInstall({ command: CCSTATUSLINE_COMMANDS.NPM, displayName: 'npx', useBunx: false }); + setScreen('fileTarget'); + } else { + handleInstallSelection(CCSTATUSLINE_COMMANDS.NPM, 'npx', false); + } + }, [handleInstallSelection, localSettingsDetected]); const handleBunxInstall = useCallback(() => { setMenuSelections(prev => ({ ...prev, install: 1 })); - handleInstallSelection(CCSTATUSLINE_COMMANDS.BUNX, 'bunx', true); - }, [handleInstallSelection]); + if (localSettingsDetected) { + setPendingInstall({ command: CCSTATUSLINE_COMMANDS.BUNX, displayName: 'bunx', useBunx: true }); + setScreen('fileTarget'); + } else { + handleInstallSelection(CCSTATUSLINE_COMMANDS.BUNX, 'bunx', true); + } + }, [handleInstallSelection, localSettingsDetected]); const handleInstallMenuCancel = useCallback(() => { setMenuSelections(clearInstallMenuSelection); @@ -231,7 +270,7 @@ export const App: React.FC = () => { if (isClaudeInstalled) { // Uninstall setConfirmDialog({ - message: `This will remove ccstatusline from ${getClaudeSettingsPath()}. Continue?`, + message: `This will remove ccstatusline from ${getActiveClaudeSettingsPath()}. Continue?`, action: async () => { await uninstallStatusLine(); setIsClaudeInstalled(false); @@ -493,6 +532,24 @@ export const App: React.FC = () => { onSelectBunx={handleBunxInstall} onCancel={handleInstallMenuCancel} initialSelection={menuSelections.install} + localSettingsDetected={localSettingsDetected} + /> + )} + {screen === 'fileTarget' && pendingInstall && ( + { + handleInstallSelection( + pendingInstall.command, + pendingInstall.displayName, + pendingInstall.useBunx, + targetPath + ); + setPendingInstall(null); + }} + onCancel={() => { + setPendingInstall(null); + setScreen('install'); + }} /> )} {screen === 'powerline' && ( diff --git a/src/tui/components/FileTargetSelect.tsx b/src/tui/components/FileTargetSelect.tsx index 2d422663a..0a4ba0911 100644 --- a/src/tui/components/FileTargetSelect.tsx +++ b/src/tui/components/FileTargetSelect.tsx @@ -1,5 +1,4 @@ import * as fs from 'fs'; - import { Box, Text, @@ -78,4 +77,4 @@ export const FileTargetSelect: React.FC = ({ ); -}; +}; \ No newline at end of file diff --git a/src/tui/components/InstallMenu.tsx b/src/tui/components/InstallMenu.tsx index 093ab5212..65c5a0f09 100644 --- a/src/tui/components/InstallMenu.tsx +++ b/src/tui/components/InstallMenu.tsx @@ -16,6 +16,7 @@ export interface InstallMenuProps { onSelectBunx: () => void; onCancel: () => void; initialSelection?: number; + localSettingsDetected?: boolean; } export const InstallMenu: React.FC = ({ @@ -24,7 +25,8 @@ export const InstallMenu: React.FC = ({ onSelectNpx, onSelectBunx, onCancel, - initialSelection = 0 + initialSelection = 0, + localSettingsDetected = false }) => { useInput((_, key) => { if (key.escape) { @@ -97,9 +99,9 @@ export const InstallMenu: React.FC = ({ - The selected command will be written to - {' '} - {getClaudeSettingsPath()} + {localSettingsDetected + ? 'You will be asked which settings file to target next.' + : `The selected command will be written to ${getClaudeSettingsPath()}`} From fd14944c409fd1d286bd7ee1528aebd170edb77d Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Sun, 12 Apr 2026 16:28:04 -0500 Subject: [PATCH 09/11] fix: lint errors in getExistingStatusLine and hooks test --- src/utils/__tests__/hooks.test.ts | 2 +- src/utils/claude-settings.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/hooks.test.ts b/src/utils/__tests__/hooks.test.ts index 9a39c5223..95ee308f3 100644 --- a/src/utils/__tests__/hooks.test.ts +++ b/src/utils/__tests__/hooks.test.ts @@ -133,7 +133,7 @@ describe('syncWidgetHooks', () => { if (fs.existsSync(globalPath)) { const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record }; const hasManagedHooks = Object.values(globalSaved.hooks ?? {}).some( - (entries: unknown[]) => (entries as Array<{ _tag?: string }>).some(e => e._tag === 'ccstatusline-managed') + (entries: unknown[]) => (entries as { _tag?: string }[]).some(e => e._tag === 'ccstatusline-managed') ); expect(hasManagedHooks).toBe(false); } diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index b02cc878f..1e5311880 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -354,6 +354,6 @@ export async function uninstallStatusLine(): Promise { } } -export async function getExistingStatusLine(): Promise { - return resolveStatusLine().command; +export function getExistingStatusLine(): Promise { + return Promise.resolve(resolveStatusLine().command); } \ No newline at end of file From 5cf0265db5827c15079e2f47d7488d8859f7a689 Mon Sep 17 00:00:00 2001 From: CorticalCode <212432458+CorticalCode@users.noreply.github.com> Date: Wed, 15 Apr 2026 05:35:13 -0500 Subject: [PATCH 10/11] fix: resolve lint errors in claude-settings test file Co-Authored-By: Claude Opus 4.6 (1M context) --- src/utils/__tests__/claude-settings.test.ts | 88 ++++++--------------- 1 file changed, 22 insertions(+), 66 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 0c4706b27..a79467d8d 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -389,9 +389,7 @@ describe('backup and error handling behavior', () => { describe('file-path-aware load/save', () => { it('loadClaudeSettings should load from specified filePath', async () => { const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); - fs.writeFileSync(localPath, JSON.stringify({ - statusLine: { type: 'command', command: 'local-command', padding: 0 } - }), 'utf-8'); + fs.writeFileSync(localPath, JSON.stringify({ statusLine: { type: 'command', command: 'local-command', padding: 0 } }), 'utf-8'); const settings = await loadClaudeSettings({ filePath: localPath }); expect(settings.statusLine?.command).toBe('local-command'); @@ -405,9 +403,7 @@ describe('file-path-aware load/save', () => { it('saveClaudeSettings should save to specified filePath', async () => { const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); - await saveClaudeSettings({ - statusLine: { type: 'command', command: 'test-cmd', padding: 0 } - }, localPath); + await saveClaudeSettings({ statusLine: { type: 'command', command: 'test-cmd', padding: 0 } }, localPath); const content = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { statusLine?: { command?: string } }; expect(content.statusLine?.command).toBe('test-cmd'); @@ -417,9 +413,7 @@ describe('file-path-aware load/save', () => { const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); fs.writeFileSync(localPath, JSON.stringify({ existing: true }), 'utf-8'); - await saveClaudeSettings({ - statusLine: { type: 'command', command: 'new-cmd', padding: 0 } - }, localPath); + await saveClaudeSettings({ statusLine: { type: 'command', command: 'new-cmd', padding: 0 } }, localPath); expect(fs.existsSync(`${localPath}.bak`)).toBe(true); const backup = JSON.parse(fs.readFileSync(`${localPath}.bak`, 'utf-8')) as { existing?: boolean }; @@ -429,52 +423,36 @@ describe('file-path-aware load/save', () => { describe('dual-file detection', () => { it('isInstalled should detect installation in settings.local.json', async () => { - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); await expect(isInstalled()).resolves.toBe(true); }); it('isInstalled should prefer local over global', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'other-command', padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'other-command', padding: 0 } })); // Local has non-ccstatusline command, so isInstalled should be false await expect(isInstalled()).resolves.toBe(false); }); it('isInstalled should fall back to global when local has no statusLine', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); writeLocalClaudeSettings(JSON.stringify({})); await expect(isInstalled()).resolves.toBe(true); }); it('getExistingStatusLine should return command from local first', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'global-command' } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'local-command' } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'global-command' } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'local-command' } })); await expect(getExistingStatusLine()).resolves.toBe('local-command'); }); it('getExistingStatusLine should fall back to global', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'global-command' } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'global-command' } })); await expect(getExistingStatusLine()).resolves.toBe('global-command'); }); it('getActiveClaudeSettingsPath should return local path when local has statusLine', () => { - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'local-command' } - })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'local-command' } })); expect(getActiveClaudeSettingsPath()).toBe( path.join(testClaudeConfigDir, 'settings.local.json') ); @@ -495,20 +473,14 @@ describe('dual-file detection', () => { it('should fall through to global when local file is malformed JSON', async () => { writeLocalClaudeSettings('{ invalid json'); - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); await expect(isInstalled()).resolves.toBe(true); await expect(getExistingStatusLine()).resolves.toBe(CCSTATUSLINE_COMMANDS.NPM); }); it('should treat ccstatusline as inactive when local has a different statusLine', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'some-other-tool' } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'some-other-tool' } })); // Local wins — and local is not ccstatusline await expect(isInstalled()).resolves.toBe(false); // But getExistingStatusLine returns whatever is effective @@ -548,9 +520,7 @@ describe('file-targeted installStatusLine', () => { describe('dual-file uninstallStatusLine', () => { it('should remove statusLine from settings.local.json', async () => { - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); await uninstallStatusLine(); @@ -560,12 +530,8 @@ describe('dual-file uninstallStatusLine', () => { }); it('should remove statusLine from both files when both have it', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.BUNX, padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.BUNX, padding: 0 } })); await uninstallStatusLine(); @@ -578,9 +544,7 @@ describe('dual-file uninstallStatusLine', () => { }); it('should not error when local file does not exist', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); await expect(uninstallStatusLine()).resolves.toBeUndefined(); @@ -589,12 +553,8 @@ describe('dual-file uninstallStatusLine', () => { }); it('should preserve non-ccstatusline statusLine in settings.json', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'company-managed-tool', padding: 0 } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'company-managed-tool', padding: 0 } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); await uninstallStatusLine(); @@ -609,12 +569,8 @@ describe('dual-file uninstallStatusLine', () => { }); it('should preserve non-ccstatusline statusLine in settings.local.json', async () => { - writeRawClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } - })); - writeLocalClaudeSettings(JSON.stringify({ - statusLine: { type: 'command', command: 'user-custom-tool' } - })); + writeRawClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: CCSTATUSLINE_COMMANDS.NPM, padding: 0 } })); + writeLocalClaudeSettings(JSON.stringify({ statusLine: { type: 'command', command: 'user-custom-tool' } })); await uninstallStatusLine(); From 25595a4384481f60716b27e4c6ab9b917138965d Mon Sep 17 00:00:00 2001 From: Matthew Breedlove Date: Sun, 26 Apr 2026 23:11:16 -0400 Subject: [PATCH 11/11] fix: sync hooks to installed settings target --- src/utils/__tests__/claude-settings.test.ts | 35 +++++++++++++ src/utils/__tests__/hooks.test.ts | 54 +++++++++++++++++++++ src/utils/claude-settings.ts | 2 +- src/utils/hooks.ts | 18 ++++--- 4 files changed, 102 insertions(+), 7 deletions(-) diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 311d87c81..15887a155 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -664,6 +664,41 @@ describe('file-targeted installStatusLine', () => { await installStatusLine(false); expect(readInstalledCommand()).toBe(CCSTATUSLINE_COMMANDS.NPM); }); + + it('should sync hooks to settings.json when settings.local.json overrides statusLine', async () => { + const configPath = path.join(testClaudeConfigDir, 'ccstatusline-settings.json'); + initConfigPath(configPath); + const settingsWithSkills = { + ...DEFAULT_SETTINGS, + lines: [[{ id: 'skills-1', type: 'skills' }], [], []] + }; + fs.writeFileSync(configPath, JSON.stringify(settingsWithSkills, null, 2), 'utf-8'); + writeLocalClaudeSettings(JSON.stringify({ + statusLine: { + type: 'command', + command: 'user-custom-tool', + padding: 0 + } + })); + + await installStatusLine(false, getClaudeSettingsPath()); + + const globalContent = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as { + hooks?: Record; + statusLine?: { command?: string }; + }; + const installedCommand = `${CCSTATUSLINE_COMMANDS.NPM} --config ${configPath}`; + expect(globalContent.statusLine?.command).toBe(installedCommand); + expect(globalContent.hooks?.PreToolUse?.[0]?.hooks?.[0]?.command).toBe(`${installedCommand} --hook`); + + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + const localContent = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { + hooks?: Record; + statusLine?: { command?: string }; + }; + expect(localContent.statusLine?.command).toBe('user-custom-tool'); + expect(localContent.hooks).toBeUndefined(); + }); }); describe('dual-file uninstallStatusLine', () => { diff --git a/src/utils/__tests__/hooks.test.ts b/src/utils/__tests__/hooks.test.ts index 4f41d6293..9e997771b 100644 --- a/src/utils/__tests__/hooks.test.ts +++ b/src/utils/__tests__/hooks.test.ts @@ -108,6 +108,60 @@ describe('syncWidgetHooks', () => { } }); + it('syncs hooks to an explicit target instead of an overriding local statusLine', async () => { + const globalPath = path.join(testClaudeConfigDir, 'settings.json'); + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(globalPath, JSON.stringify({ + statusLine: { + type: 'command', + command: CCSTATUSLINE_COMMANDS.NPM, + padding: 0 + } + }, null, 2), 'utf-8'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { + type: 'command', + command: 'user-custom-tool', + padding: 0 + } + }, null, 2), 'utf-8'); + + const settingsWithSkills = { + ...DEFAULT_SETTINGS, + lines: [[{ id: 'skills-1', type: 'skills' }], [], []] + }; + + await syncWidgetHooks(settingsWithSkills, { targetPath: globalPath }); + + const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record }; + expect(globalSaved.hooks?.PreToolUse?.[0]?.hooks?.[0]?.command).toBe(`${CCSTATUSLINE_COMMANDS.NPM} --hook`); + + const localSaved = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { hooks?: Record; statusLine?: { command?: string } }; + expect(localSaved.statusLine?.command).toBe('user-custom-tool'); + expect(localSaved.hooks).toBeUndefined(); + }); + + it('does not create hooks for a non-ccstatusline statusLine command', async () => { + const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); + fs.writeFileSync(localPath, JSON.stringify({ + statusLine: { + type: 'command', + command: 'user-custom-tool', + padding: 0 + } + }, null, 2), 'utf-8'); + + const settingsWithSkills = { + ...DEFAULT_SETTINGS, + lines: [[{ id: 'skills-1', type: 'skills' }], [], []] + }; + + await syncWidgetHooks(settingsWithSkills); + + const localSaved = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { hooks?: Record }; + expect(localSaved.hooks).toBeUndefined(); + }); + it('subsequent sync does not recreate hooks in old file', async () => { const localPath = path.join(testClaudeConfigDir, 'settings.local.json'); fs.writeFileSync(localPath, JSON.stringify({ diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index 0901e7b78..b58d02004 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -397,7 +397,7 @@ export async function installStatusLine( const savedSettings = await loadSavedSettingsForHookSync(); if (savedSettings) { const { syncWidgetHooks } = await import('./hooks'); - await syncWidgetHooks(savedSettings); + await syncWidgetHooks(savedSettings, { targetPath: settingsPath }); } } diff --git a/src/utils/hooks.ts b/src/utils/hooks.ts index 515b7ed54..4cc622d84 100644 --- a/src/utils/hooks.ts +++ b/src/utils/hooks.ts @@ -7,7 +7,7 @@ import { getActiveClaudeSettingsPath, getClaudeLocalSettingsPath, getClaudeSettingsPath, - getExistingStatusLine, + isKnownCommand, loadClaudeSettings, saveClaudeSettings } from './claude-settings'; @@ -26,6 +26,8 @@ interface HookEntry { hooks?: { type: string; command: string }[]; } +interface SyncWidgetHooksOptions { targetPath?: string } + function stripManagedHooks(hooks: Record): void { for (const event of Object.keys(hooks)) { hooks[event] = (hooks[event] ?? []).filter(entry => entry._tag !== HOOK_TAG); @@ -57,15 +59,19 @@ function getActiveHookDefs(settings: Settings): WidgetHookDef[] { return defs; } -export async function syncWidgetHooks(settings: Settings): Promise { +export async function syncWidgetHooks( + settings: Settings, + options: SyncWidgetHooksOptions = {} +): Promise { const needed = getActiveHookDefs(settings); - const activePath = getActiveClaudeSettingsPath(); + const activePath = options.targetPath ?? getActiveClaudeSettingsPath(); + const cleanNonTargetFile = options.targetPath === undefined; // Clean stale managed hooks from the non-active file to prevent duplicates const globalPath = getClaudeSettingsPath(); const localPath = getClaudeLocalSettingsPath(); const nonActivePath = activePath === localPath ? globalPath : localPath; - if (fs.existsSync(nonActivePath)) { + if (cleanNonTargetFile && fs.existsSync(nonActivePath)) { try { const otherSettings = await loadClaudeSettings({ logErrors: false, filePath: nonActivePath }); if (Object.keys(otherSettings).length > 0) { @@ -86,8 +92,8 @@ export async function syncWidgetHooks(settings: Settings): Promise { // Remove all ccstatusline-managed hooks stripManagedHooks(hooks); - const statusCommand = await getExistingStatusLine(); - if (!statusCommand) { + const statusCommand = claudeSettings.statusLine?.command ?? null; + if (!statusCommand || !isKnownCommand(statusCommand)) { claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined; await saveClaudeSettings(claudeSettings, activePath); return;