-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.ts
More file actions
251 lines (225 loc) · 7.92 KB
/
install.ts
File metadata and controls
251 lines (225 loc) · 7.92 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env bun
/**
* gstack-industrial Installation Script
*
* Installs template system and skill router to Claude Code
*/
import { copyFileSync, mkdirSync, existsSync, writeFileSync, readFileSync } from 'fs';
import { join } from 'path';
import { homedir } from 'os';
const CLAUDE_DIR = join(homedir(), '.claude');
const SKILLS_TEMPLATES = join(CLAUDE_DIR, 'skills', 'templates');
const HOOKS_DIR = join(CLAUDE_DIR, 'hooks');
const CONFIG_DIR = join(CLAUDE_DIR, 'config');
function ensureDir(dir: string) {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
console.log(`✅ Created directory: ${dir}`);
}
}
function copyFile(src: string, dest: string) {
try {
copyFileSync(src, dest);
console.log(`✅ Copied: ${dest}`);
} catch (error) {
console.error(`❌ Failed to copy ${src}:`, error);
}
}
async function main() {
console.log('\n🚀 Installing gstack-industrial...\n');
// 1. Ensure directories exist
console.log('📁 Creating directories...');
ensureDir(SKILLS_TEMPLATES);
ensureDir(HOOKS_DIR);
ensureDir(CONFIG_DIR);
ensureDir(join(CONFIG_DIR, 'sessions'));
console.log('');
// 2. Copy skill-router
console.log('📦 Installing Skill Router...');
const routerDest = join(SKILLS_TEMPLATES, 'skill-router');
ensureDir(routerDest);
const routerFiles = [
'types.ts',
'context-extractor.ts',
'matcher-engine.ts',
'suggestion-formatter.ts',
'index.ts',
'gen-skill-docs.ts',
'auto-discover.ts',
'test-cli.ts',
'matchers.json',
'README.md'
];
routerFiles.forEach(file => {
const src = join(process.cwd(), 'skill-router', file);
const dest = join(routerDest, file);
if (existsSync(src)) {
copyFile(src, dest);
}
});
console.log('');
// 3. Copy standard sections
console.log('📄 Installing Standard Sections...');
const sectionsFiles = [
'universal-preamble-section.md',
'askuserquestion-standard-section.md',
'completeness-principle-section.md'
];
sectionsFiles.forEach(file => {
const src = join(process.cwd(), 'standard-sections', file);
const dest = join(SKILLS_TEMPLATES, file);
if (existsSync(src)) {
copyFile(src, dest);
}
});
console.log('');
// 4. Copy hooks
console.log('🎣 Installing Hooks...');
const hookFiles = [
'skill-router-before-message.ts',
'skill-discovery-session-start.sh',
];
for (const hookFile of hookFiles) {
const src = join(process.cwd(), 'hooks', hookFile);
const dest = join(HOOKS_DIR, hookFile);
if (existsSync(src)) {
copyFile(src, dest);
try {
await Bun.spawn(['chmod', '+x', dest]).exited;
console.log(`✅ Made executable: ${dest}`);
} catch (error) {
console.error('❌ Failed to make hook executable:', error);
}
}
}
const hookDest = join(HOOKS_DIR, 'skill-router-before-message.ts');
const sessionHookDest = join(HOOKS_DIR, 'skill-discovery-session-start.sh');
console.log('');
// 5. Create or migrate config
console.log('⚙️ Setting up configuration...');
const configPath = join(CONFIG_DIR, 'skill-router.json');
const defaultConfig = {
enabled: true,
threshold: 80,
maxSuggestionsPerSession: 500,
cooldownMinutes: 5,
disabledSkills: [],
priorityBoosts: {},
quietHours: { enabled: false, start: "22:00", end: "08:00" },
// Defaults for v0.2+ fields (migration-safe)
repoModeThresholds: { solo: 60, collaborative: 85, unknown: 80 },
showLimitWarnings: true,
feedbackBoost: 20,
feedbackPenalty: 30,
};
if (!existsSync(configPath)) {
writeFileSync(configPath, JSON.stringify(defaultConfig, null, 2));
console.log(`✅ Created default config: ${configPath}`);
} else {
// Migration: add any missing fields to existing config
const existing = JSON.parse(readFileSync(configPath, 'utf-8'));
let migrated = false;
for (const [key, value] of Object.entries(defaultConfig)) {
if (existing[key] === undefined) {
existing[key] = value;
migrated = true;
}
}
if (migrated) {
writeFileSync(configPath, JSON.stringify(existing, null, 2));
console.log(`✅ Migrated config with new fields: ${configPath}`);
} else {
console.log(`ℹ️ Config already up to date: ${configPath}`);
}
}
console.log('');
// 6. Register hook in settings.json (idempotent)
console.log('🔧 Registering hook in settings.json...');
const settingsPath = join(CLAUDE_DIR, 'settings.json');
try {
let settings: any = {};
if (existsSync(settingsPath)) {
settings = JSON.parse(readFileSync(settingsPath, 'utf-8'));
}
let changed = false;
if (!settings.hooks) settings.hooks = {};
if (!settings.hooks.UserPromptSubmit) settings.hooks.UserPromptSubmit = [];
const hookCommand = `bun run ${hookDest}`;
const hasHook = settings.hooks.UserPromptSubmit.some((entry: any) =>
entry.hooks?.some((h: any) => h.command?.includes('skill-router-before-message'))
);
if (!hasHook) {
settings.hooks.UserPromptSubmit.push({
matcher: '*',
hooks: [{ type: 'command', command: hookCommand }],
});
changed = true;
console.log(`✅ Registered UserPromptSubmit hook`);
} else {
console.log(`ℹ️ UserPromptSubmit hook already registered`);
}
// Register SessionStart hook for auto-discovery
if (!settings.hooks.SessionStart) settings.hooks.SessionStart = [];
const sessionHookCommand = `bash ${sessionHookDest}`;
const hasSessionHook = settings.hooks.SessionStart.some((entry: any) =>
entry.hooks?.some((h: any) => h.command?.includes('skill-discovery-session-start'))
);
if (!hasSessionHook) {
settings.hooks.SessionStart.push({
matcher: 'startup',
hooks: [{ type: 'command', command: sessionHookCommand }],
});
changed = true;
console.log(`✅ Registered SessionStart hook (auto-discovery)`);
} else {
console.log(`ℹ️ SessionStart hook already registered`);
}
if (changed) {
writeFileSync(settingsPath, JSON.stringify(settings, null, 2));
console.log(`✅ settings.json updated`);
}
} catch (error) {
console.error('❌ Failed to register hook:', error);
console.log(' Manual step: add to ~/.claude/settings.json under hooks');
}
console.log('');
// 7. Run auto-discovery to populate matchers.json
console.log('🔍 Running skill auto-discovery...');
try {
const autoDiscoverPath = join(routerDest, 'auto-discover.ts');
if (existsSync(autoDiscoverPath)) {
const proc = Bun.spawn(['bun', 'run', autoDiscoverPath], {
cwd: routerDest,
stdout: 'pipe',
stderr: 'pipe',
});
await proc.exited;
const stdout = await new Response(proc.stdout).text();
if (stdout.trim()) console.log(stdout.trim());
}
} catch (error) {
console.error('⚠️ Auto-discovery failed (non-critical):', error);
}
console.log('');
// 8. Success message
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('✅ Installation complete!');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('');
console.log('📖 Next steps:');
console.log('');
console.log('1. Test the router:');
console.log(' cd ~/.claude/skills/templates/skill-router');
console.log(' bun run test-cli.ts "I need to brainstorm" --debug');
console.log('');
console.log('2. Generate template-based skills:');
console.log(' cd ~/.claude/skills/templates');
console.log(' bun run skill-router/gen-skill-docs.ts');
console.log('');
console.log('📚 Documentation: https://github.com/kevintseng/gstack-industrial');
console.log('');
}
main().catch(error => {
console.error('❌ Installation failed:', error);
process.exit(1);
});