-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.ts
More file actions
178 lines (154 loc) · 6.13 KB
/
Copy pathinit.ts
File metadata and controls
178 lines (154 loc) · 6.13 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
import { Command } from 'commander';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import { getBaseUrl, resolveCredentials } from '../lib/auth';
import { runBrowserAuthFlow } from '../lib/browser-auth';
import { output, outputError } from '../lib/output';
import { cliCapture } from '../lib/analytics';
// ─── Editor detection ────────────────────────────────────────────────────────
interface Editor {
name: string;
configPath: string;
}
function detectEditors(): Editor[] {
const home = os.homedir();
const editors: Editor[] = [];
// Claude Code
const claudeConfig = path.join(home, '.claude.json');
if (fs.existsSync(path.join(home, '.claude'))) {
editors.push({ name: 'Claude Code', configPath: claudeConfig });
}
// Cursor
const cursorConfig = path.join(process.cwd(), '.cursor', 'mcp.json');
if (fs.existsSync(path.join(process.cwd(), '.cursor'))) {
editors.push({ name: 'Cursor', configPath: cursorConfig });
}
// VS Code Copilot (agent mode)
const vscodeConfig = path.join(process.cwd(), '.vscode', 'mcp.json');
if (fs.existsSync(path.join(process.cwd(), '.vscode'))) {
editors.push({ name: 'VS Code', configPath: vscodeConfig });
}
// Windsurf
const windsurfConfig = path.join(home, '.windsurf', 'mcp.json');
if (fs.existsSync(path.join(home, '.windsurf'))) {
editors.push({ name: 'Windsurf', configPath: windsurfConfig });
}
return editors;
}
function writeMcpConfig(configPath: string, apiKey: string, userId?: string): void {
const dir = path.dirname(configPath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
let existing: Record<string, unknown> = {};
if (fs.existsSync(configPath)) {
try {
existing = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch {
// Start fresh if invalid
}
}
const mcpServers = (existing.mcpServers || {}) as Record<string, unknown>;
mcpServers['graspful'] = {
command: 'npx',
args: ['-y', '@graspful/mcp'],
env: {
GRASPFUL_API_KEY: apiKey,
...(userId ? { GRASPFUL_USER_ID: userId } : {}),
},
};
existing.mcpServers = mcpServers;
fs.writeFileSync(configPath, JSON.stringify(existing, null, 2) + '\n');
}
export function registerInitCommand(program: Command) {
program
.command('init')
.description('Set up Graspful: authenticate in the browser and configure MCP for your editor')
.option('--email <email>', 'Prefill the browser sign-up form')
.option('--password <password>', 'Deprecated. Password entry now happens in the browser.')
.option('--api-url <url>', 'API base URL')
.option('--no-mcp', 'Skip MCP configuration')
.option('--no-browser', 'Print the sign-up URL instead of opening it automatically')
.action(async (opts: { email?: string; password?: string; apiUrl?: string; mcp: boolean; browser?: boolean }) => {
const baseUrl = (opts.apiUrl || getBaseUrl()).replace(/\/$/, '');
// ── Check if already authenticated ──────────────────────────────────
const existingCreds = resolveCredentials();
if (existingCreds.apiKey) {
console.log('Already authenticated (credentials found).');
console.log('Skipping registration. To re-register, delete ~/.graspful/credentials.json first.');
// Still configure MCP if requested
if (opts.mcp) {
configureMcp(existingCreds.apiKey, existingCreds.userId);
}
output(
{ authenticated: true, baseUrl },
'Ready. Run graspful create course --topic "Your Topic" to get started.',
);
return;
}
// ── Browser sign-up ────────────────────────────────────────────────
try {
if (opts.password) {
console.log('`--password` is deprecated. Complete the password and verification steps in your browser.');
}
const data = await runBrowserAuthFlow({
apiUrl: baseUrl,
mode: 'sign-up',
email: opts.email,
noBrowser: opts.browser === false,
});
console.log(`\nAccount created!`);
console.log(` Org: ${data.orgSlug}`);
console.log(` API key: ${data.apiKey} (saved to ~/.graspful/credentials.json)`);
// ── Configure MCP ───────────────────────────────────────────────
if (opts.mcp) {
configureMcp(data.apiKey, data.userId);
}
output(
{
userId: data.userId,
orgSlug: data.orgSlug,
apiKey: data.apiKey,
baseUrl,
},
[
'',
'You\'re ready. Next steps:',
` graspful create course --topic "Your Topic" -o course.yaml`,
` graspful fill concept course.yaml <concept-id>`,
` graspful review course.yaml`,
` graspful import course.yaml --org ${data.orgSlug} --publish`,
].join('\n'),
);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
outputError(`Registration failed: ${msg}`);
process.exit(1);
}
});
}
function configureMcp(apiKey: string, userId?: string): void {
const editors = detectEditors();
if (editors.length === 0) {
console.log('\nNo supported editors detected. Add MCP manually:');
console.log(JSON.stringify({
mcpServers: {
graspful: {
command: 'npx',
args: ['-y', '@graspful/mcp'],
env: {
GRASPFUL_API_KEY: apiKey,
...(userId ? { GRASPFUL_USER_ID: userId } : {}),
},
},
},
}, null, 2));
return;
}
for (const editor of editors) {
writeMcpConfig(editor.configPath, apiKey, userId);
cliCapture('cli initialized', { editor: editor.name });
console.log(`\nMCP configured for ${editor.name}: ${editor.configPath}`);
}
}