-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
141 lines (122 loc) · 3.97 KB
/
install.js
File metadata and controls
141 lines (122 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const os = require('os');
const { execSync } = require('child_process');
const isLocal = process.argv.includes('--local');
function eprintf(...args) {
process.stderr.write(args.join(' ') + '\n');
}
function getPaths() {
if (isLocal) {
const cwd = process.cwd();
return {
claudeDir: path.join(cwd, '.claude'),
hooksDir: path.join(cwd, '.claude', 'hooks'),
settingsFile: path.join(cwd, '.claude', 'settings.local.json'),
hooksDirRel: path.join('.claude', 'hooks'),
};
}
const home = os.homedir();
const claudeDir = path.join(home, '.claude');
return {
claudeDir,
hooksDir: path.join(claudeDir, 'hooks'),
settingsFile: path.join(claudeDir, 'settings.json'),
hooksDirRel: path.join(claudeDir, 'hooks'),
};
}
function getHookCommand(hooksDir, pattern) {
const normalize = (p) => p.split(path.sep).join('/');
if (isLocal) {
return `node ".claude/hooks/beep.js" ${pattern}`;
}
const scriptPath = normalize(path.join(hooksDir, 'beep.js'));
return `node "${scriptPath}" ${pattern}`;
}
function readSettings(file) {
try {
const raw = fs.readFileSync(file, 'utf-8');
return JSON.parse(raw);
} catch (err) {
if (err.code === 'ENOENT') return {};
eprintf('Error: failed to parse', file);
eprintf('Backing up to', file + '.bak');
try {
fs.copyFileSync(file, file + '.bak');
} catch {}
eprintf(err.message);
process.exit(1);
}
}
function addHook(settings, event, command) {
if (!settings.hooks) settings.hooks = {};
if (!Array.isArray(settings.hooks[event])) settings.hooks[event] = [];
if (
settings.hooks[event].some(
(entry) =>
entry.matcher === '*' &&
Array.isArray(entry.hooks) &&
entry.hooks.some(
(h) => h.type === 'command' && h.command === command
)
)
) {
return false;
}
// Try to find an existing "*" matcher entry to append to
const wildcard = settings.hooks[event].find(
(e) => e.matcher === '*'
);
if (wildcard) {
if (
!wildcard.hooks.some((h) => h.type === 'command' && h.command === command)
) {
wildcard.hooks.push({ type: 'command', command });
}
return false;
}
settings.hooks[event].push({
matcher: '*',
hooks: [{ type: 'command', command }],
});
return true;
}
function main() {
if (isLocal) {
const srcCheck = path.join(__dirname, 'beep.js');
if (!fs.existsSync(srcCheck)) {
eprintf('Error: beep.js not found in current directory.');
eprintf('Run this script from the bibiReminder4cc project root.');
process.exit(1);
}
}
const { claudeDir, hooksDir, settingsFile, hooksDirRel } = getPaths();
const cmdDone = getHookCommand(hooksDirRel, 'done');
const cmdAllow = getHookCommand(hooksDirRel, 'allow');
// Create directories
if (!fs.existsSync(claudeDir)) fs.mkdirSync(claudeDir, { recursive: true });
if (!fs.existsSync(hooksDir)) fs.mkdirSync(hooksDir, { recursive: true });
// Copy beep.js
const src = path.join(__dirname, 'beep.js');
const dest = path.join(hooksDir, 'beep.js');
fs.copyFileSync(src, dest);
// Read and update settings
const settings = readSettings(settingsFile);
const addedDone = addHook(settings, 'Stop', cmdDone);
const addedAllow = addHook(settings, 'PermissionRequest', cmdAllow);
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2) + '\n');
// Summary
console.log('✓ bibiReminder4cc installed' + (isLocal ? ' (project-level)' : ' (global)'));
console.log(' Settings:', settingsFile);
if (addedDone) console.log(' + Stop hook: ' + cmdDone);
else console.log(' ~ Stop hook: already present');
if (addedAllow) console.log(' + PermissionRequest hook: ' + cmdAllow);
else console.log(' ~ PermissionRequest hook: already present');
console.log('');
console.log('Testing beep...');
try {
execSync(`node "${dest}" --test`, { stdio: 'inherit', timeout: 15000 });
} catch {}
}
main();