-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.ts
More file actions
115 lines (97 loc) · 2.62 KB
/
index.ts
File metadata and controls
115 lines (97 loc) · 2.62 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
import { findEnvFile } from '@/utils/find-env';
import { heading } from '@/utils/heading';
import * as p from '@clack/prompts';
import {
cancel,
confirm,
isCancel,
note,
outro,
password
} from '@clack/prompts';
import fs from 'fs/promises';
import open from 'open';
import path from 'path';
import color from 'picocolors';
export async function auth() {
p.intro(
heading({
text: 'Langbase Authentication',
sub: 'Auth by logging in to Langbase and getting your API key'
})
);
const shouldOpen = await confirm({
message: `Open the authentication page? ${color.dim(`— copy your API key from there and paste it here.`)}`
});
if (isCancel(shouldOpen)) {
cancel('Operation cancelled.');
process.exit(0);
}
if (shouldOpen) {
await open('https://langbase.com/settings/api');
note(
color.yellow(
'Please copy your API key from the opened page and paste it here.'
)
);
}
const apiKeyString = await password({
message: 'Paste your API key string:',
mask: '*'
});
if (isCancel(apiKeyString)) {
cancel('Operation cancelled.');
process.exit(0);
}
const [login, apiKey] = (apiKeyString as string).split(':');
if (!login || !apiKey) {
outro(
color.red(
'Invalid API key string. It should be in the format login:apiKey, when copied from https://langbase.com/settings/api it should be in the correct format.'
)
);
process.exit(1);
}
const envKeyName = 'LANGBASE_API_KEY';
const envContent = `\n# Langbase API key for https://langbase.com/${login}\n${envKeyName}=${apiKey}\n\n`;
let envFile = await findEnvFile();
if (!envFile) {
envFile = '.env';
await fs.writeFile(path.join(process.cwd(), envFile), envContent);
} else {
const envFileContent = await fs.readFile(envFile, 'utf-8');
const oldKey = envFileContent
.split('\n')
.reverse() // Reverse to get the latest key if there are multiple
.find(line => line.includes('LANGBASE_API_KEY'))
?.split('=')[1];
if (oldKey) {
const shouldOverwrite = await confirm({
message: `API key found in ${envFile}. Overwrite?`
});
if (isCancel(shouldOverwrite)) {
cancel('Operation cancelled.');
process.exit(0);
}
if (!shouldOverwrite) {
outro(color.yellow('API key is not overwritten.'));
process.exit(0);
}
const newEnvContent = envFileContent.replace(
new RegExp(`LANGBASE_API_KEY=${oldKey}`),
envContent.trim()
);
await fs.writeFile(
path.join(process.cwd(), envFile),
newEnvContent
);
} else {
await fs.appendFile(path.join(process.cwd(), envFile), envContent);
}
}
outro(
color.green(
`Authentication successful. API key is stored in ${envFile}`
)
);
}