Skip to content

Commit 25595a4

Browse files
committed
fix: sync hooks to installed settings target
1 parent e549096 commit 25595a4

4 files changed

Lines changed: 102 additions & 7 deletions

File tree

‎src/utils/__tests__/claude-settings.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,41 @@ describe('file-targeted installStatusLine', () => {
664664
await installStatusLine(false);
665665
expect(readInstalledCommand()).toBe(CCSTATUSLINE_COMMANDS.NPM);
666666
});
667+
668+
it('should sync hooks to settings.json when settings.local.json overrides statusLine', async () => {
669+
const configPath = path.join(testClaudeConfigDir, 'ccstatusline-settings.json');
670+
initConfigPath(configPath);
671+
const settingsWithSkills = {
672+
...DEFAULT_SETTINGS,
673+
lines: [[{ id: 'skills-1', type: 'skills' }], [], []]
674+
};
675+
fs.writeFileSync(configPath, JSON.stringify(settingsWithSkills, null, 2), 'utf-8');
676+
writeLocalClaudeSettings(JSON.stringify({
677+
statusLine: {
678+
type: 'command',
679+
command: 'user-custom-tool',
680+
padding: 0
681+
}
682+
}));
683+
684+
await installStatusLine(false, getClaudeSettingsPath());
685+
686+
const globalContent = JSON.parse(fs.readFileSync(getClaudeSettingsPath(), 'utf-8')) as {
687+
hooks?: Record<string, { hooks?: { command?: string }[] }[]>;
688+
statusLine?: { command?: string };
689+
};
690+
const installedCommand = `${CCSTATUSLINE_COMMANDS.NPM} --config ${configPath}`;
691+
expect(globalContent.statusLine?.command).toBe(installedCommand);
692+
expect(globalContent.hooks?.PreToolUse?.[0]?.hooks?.[0]?.command).toBe(`${installedCommand} --hook`);
693+
694+
const localPath = path.join(testClaudeConfigDir, 'settings.local.json');
695+
const localContent = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as {
696+
hooks?: Record<string, unknown[]>;
697+
statusLine?: { command?: string };
698+
};
699+
expect(localContent.statusLine?.command).toBe('user-custom-tool');
700+
expect(localContent.hooks).toBeUndefined();
701+
});
667702
});
668703

669704
describe('dual-file uninstallStatusLine', () => {

‎src/utils/__tests__/hooks.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,60 @@ describe('syncWidgetHooks', () => {
108108
}
109109
});
110110

111+
it('syncs hooks to an explicit target instead of an overriding local statusLine', async () => {
112+
const globalPath = path.join(testClaudeConfigDir, 'settings.json');
113+
const localPath = path.join(testClaudeConfigDir, 'settings.local.json');
114+
fs.writeFileSync(globalPath, JSON.stringify({
115+
statusLine: {
116+
type: 'command',
117+
command: CCSTATUSLINE_COMMANDS.NPM,
118+
padding: 0
119+
}
120+
}, null, 2), 'utf-8');
121+
fs.writeFileSync(localPath, JSON.stringify({
122+
statusLine: {
123+
type: 'command',
124+
command: 'user-custom-tool',
125+
padding: 0
126+
}
127+
}, null, 2), 'utf-8');
128+
129+
const settingsWithSkills = {
130+
...DEFAULT_SETTINGS,
131+
lines: [[{ id: 'skills-1', type: 'skills' }], [], []]
132+
};
133+
134+
await syncWidgetHooks(settingsWithSkills, { targetPath: globalPath });
135+
136+
const globalSaved = JSON.parse(fs.readFileSync(globalPath, 'utf-8')) as { hooks?: Record<string, { hooks?: { command?: string }[] }[]> };
137+
expect(globalSaved.hooks?.PreToolUse?.[0]?.hooks?.[0]?.command).toBe(`${CCSTATUSLINE_COMMANDS.NPM} --hook`);
138+
139+
const localSaved = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { hooks?: Record<string, unknown[]>; statusLine?: { command?: string } };
140+
expect(localSaved.statusLine?.command).toBe('user-custom-tool');
141+
expect(localSaved.hooks).toBeUndefined();
142+
});
143+
144+
it('does not create hooks for a non-ccstatusline statusLine command', async () => {
145+
const localPath = path.join(testClaudeConfigDir, 'settings.local.json');
146+
fs.writeFileSync(localPath, JSON.stringify({
147+
statusLine: {
148+
type: 'command',
149+
command: 'user-custom-tool',
150+
padding: 0
151+
}
152+
}, null, 2), 'utf-8');
153+
154+
const settingsWithSkills = {
155+
...DEFAULT_SETTINGS,
156+
lines: [[{ id: 'skills-1', type: 'skills' }], [], []]
157+
};
158+
159+
await syncWidgetHooks(settingsWithSkills);
160+
161+
const localSaved = JSON.parse(fs.readFileSync(localPath, 'utf-8')) as { hooks?: Record<string, unknown[]> };
162+
expect(localSaved.hooks).toBeUndefined();
163+
});
164+
111165
it('subsequent sync does not recreate hooks in old file', async () => {
112166
const localPath = path.join(testClaudeConfigDir, 'settings.local.json');
113167
fs.writeFileSync(localPath, JSON.stringify({

‎src/utils/claude-settings.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ export async function installStatusLine(
397397
const savedSettings = await loadSavedSettingsForHookSync();
398398
if (savedSettings) {
399399
const { syncWidgetHooks } = await import('./hooks');
400-
await syncWidgetHooks(savedSettings);
400+
await syncWidgetHooks(savedSettings, { targetPath: settingsPath });
401401
}
402402
}
403403

‎src/utils/hooks.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getActiveClaudeSettingsPath,
88
getClaudeLocalSettingsPath,
99
getClaudeSettingsPath,
10-
getExistingStatusLine,
10+
isKnownCommand,
1111
loadClaudeSettings,
1212
saveClaudeSettings
1313
} from './claude-settings';
@@ -26,6 +26,8 @@ interface HookEntry {
2626
hooks?: { type: string; command: string }[];
2727
}
2828

29+
interface SyncWidgetHooksOptions { targetPath?: string }
30+
2931
function stripManagedHooks(hooks: Record<string, HookEntry[]>): void {
3032
for (const event of Object.keys(hooks)) {
3133
hooks[event] = (hooks[event] ?? []).filter(entry => entry._tag !== HOOK_TAG);
@@ -57,15 +59,19 @@ function getActiveHookDefs(settings: Settings): WidgetHookDef[] {
5759
return defs;
5860
}
5961

60-
export async function syncWidgetHooks(settings: Settings): Promise<void> {
62+
export async function syncWidgetHooks(
63+
settings: Settings,
64+
options: SyncWidgetHooksOptions = {}
65+
): Promise<void> {
6166
const needed = getActiveHookDefs(settings);
62-
const activePath = getActiveClaudeSettingsPath();
67+
const activePath = options.targetPath ?? getActiveClaudeSettingsPath();
68+
const cleanNonTargetFile = options.targetPath === undefined;
6369

6470
// Clean stale managed hooks from the non-active file to prevent duplicates
6571
const globalPath = getClaudeSettingsPath();
6672
const localPath = getClaudeLocalSettingsPath();
6773
const nonActivePath = activePath === localPath ? globalPath : localPath;
68-
if (fs.existsSync(nonActivePath)) {
74+
if (cleanNonTargetFile && fs.existsSync(nonActivePath)) {
6975
try {
7076
const otherSettings = await loadClaudeSettings({ logErrors: false, filePath: nonActivePath });
7177
if (Object.keys(otherSettings).length > 0) {
@@ -86,8 +92,8 @@ export async function syncWidgetHooks(settings: Settings): Promise<void> {
8692
// Remove all ccstatusline-managed hooks
8793
stripManagedHooks(hooks);
8894

89-
const statusCommand = await getExistingStatusLine();
90-
if (!statusCommand) {
95+
const statusCommand = claudeSettings.statusLine?.command ?? null;
96+
if (!statusCommand || !isKnownCommand(statusCommand)) {
9197
claudeSettings.hooks = Object.keys(hooks).length > 0 ? hooks : undefined;
9298
await saveClaudeSettings(claudeSettings, activePath);
9399
return;

0 commit comments

Comments
 (0)