()
| 15 | import color from 'picocolors'; |
| 16 | |
| 17 | export async function auth() { |
| 18 | p.intro( |
| 19 | heading({ |
| 20 | text: 'Langbase Authentication', |
| 21 | sub: 'Auth by logging in to Langbase and getting your API key' |
| 22 | }) |
| 23 | ); |
| 24 | |
| 25 | const shouldOpen = await confirm({ |
| 26 | message: `Open the authentication page? ${color.dim(`— copy your API key from there and paste it here.`)}` |
| 27 | }); |
| 28 | |
| 29 | if (isCancel(shouldOpen)) { |
| 30 | cancel('Operation cancelled.'); |
| 31 | process.exit(0); |
| 32 | } |
| 33 | |
| 34 | if (shouldOpen) { |
| 35 | await open('https://langbase.com/settings/api'); |
| 36 | |
| 37 | note( |
| 38 | color.yellow( |
| 39 | 'Please copy your API key from the opened page and paste it here.' |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | const apiKeyString = await password({ |
| 45 | message: 'Paste your API key string:', |
| 46 | mask: '*' |
| 47 | }); |
| 48 | |
| 49 | if (isCancel(apiKeyString)) { |
| 50 | cancel('Operation cancelled.'); |
| 51 | process.exit(0); |
| 52 | } |
| 53 | |
| 54 | const [login, apiKey] = (apiKeyString as string).split(':'); |
| 55 | |
| 56 | if (!login || !apiKey) { |
| 57 | outro( |
| 58 | color.red( |
| 59 | '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.' |
| 60 | ) |
| 61 | ); |
| 62 | process.exit(1); |
| 63 | } |
| 64 | |
| 65 | const envKeyName = 'LANGBASE_API_KEY'; |
| 66 | const envContent = `\n# Langbase API key for https://langbase.com/${login}\n${envKeyName}=${apiKey}\n\n`; |
| 67 | |
| 68 | // TODO: Do we need this now? |
| 69 | // const envFiles = ['.env', '.env.local', '.dev.vars']; |
| 70 | // let envFile = envFiles.find(file => |
| 71 | // fs.existsSync(path.join(process.cwd(), file)) |
| 72 | // ); |
| 73 | |
| 74 | // if (!envFile) { |
no test coverage detected