-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetupEnv.ts
More file actions
56 lines (49 loc) · 1.82 KB
/
Copy pathsetupEnv.ts
File metadata and controls
56 lines (49 loc) · 1.82 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
/**
* @file setupEnv.ts
* @description Helpers for merging workspace .env and optional provider API key prompts.
*/
import fs from 'fs';
export type ProviderApiKeys = {
cursorApiKey?: string;
anthropicApiKey?: string;
};
/** Merge vars into an existing KEY=value .env file (create or update keys). */
export function mergeEnvFile(envFile: string, vars: Record<string, string>): void {
const map = new Map<string, string>();
if (fs.existsSync(envFile)) {
for (const line of fs.readFileSync(envFile, 'utf8').split('\n')) {
const m = line.match(/^([A-Z_][A-Z0-9_]*)=(.*)$/);
if (m) map.set(m[1], m[2]);
}
}
for (const [key, value] of Object.entries(vars)) {
if (value) map.set(key, value);
}
const lines = [...map.entries()].map(([k, v]) => `${k}=${v}`);
fs.writeFileSync(envFile, lines.join('\n') + '\n', 'utf8');
}
/** Ask optionally for Cursor and Claude API keys (after workspace is chosen). */
export async function promptProviderApiKeys(
ask: (question: string) => Promise<string>,
): Promise<ProviderApiKeys> {
console.log('');
console.log('Optional: provider API keys');
console.log(' For Cursor and Claude SDK agents. Answer N or press Enter to skip.');
console.log(' You can add them later in workspace/.ctrlnode/.env');
console.log('');
let cursorApiKey = '';
const useCursor = await ask('Configure Cursor API key (CURSOR_API_KEY)? (y/N): ');
if (/^y(es)?$/i.test(useCursor)) {
cursorApiKey = await ask('CURSOR_API_KEY: ');
}
let anthropicApiKey = '';
const useClaude = await ask('Configure Claude API key (ANTHROPIC_API_KEY)? (y/N): ');
if (/^y(es)?$/i.test(useClaude)) {
anthropicApiKey = await ask('ANTHROPIC_API_KEY: ');
}
console.log('');
return {
...(cursorApiKey ? { cursorApiKey } : {}),
...(anthropicApiKey ? { anthropicApiKey } : {}),
};
}