@@ -9,21 +9,11 @@ import {
99 outro ,
1010 password
1111} from '@clack/prompts' ;
12- import Conf from 'conf' ;
13- import fs from 'fs' ;
12+ import fs from 'fs/promises' ;
1413import open from 'open' ;
1514import path from 'path' ;
1615import color from 'picocolors' ;
1716
18- const config = new Conf ( {
19- projectName : 'baseai'
20- } ) ;
21-
22- interface Account {
23- login : string ;
24- apiKey : string ;
25- }
26-
2717export async function auth ( ) {
2818 p . intro (
2919 heading ( {
@@ -72,17 +62,6 @@ export async function auth() {
7262 process . exit ( 1 ) ;
7363 }
7464
75- // Store in Conf (old functionality)
76- const newAccount : Account = { login, apiKey } ;
77- const existingAccounts = ( config . get ( 'accounts' ) as Account [ ] ) || [ ] ;
78- const updatedAccounts = [ ...existingAccounts , newAccount ] ;
79- config . set ( 'accounts' , updatedAccounts ) ;
80-
81- // Store in .env file (new functionality)
82- // const envKeyName = apiKey.startsWith('user_')
83- // ? 'LANGBASE_USER_API_KEY'
84- // : 'LANGBASE_ORG_API_KEY';
85-
8665 const envKeyName = 'LANGBASE_API_KEY' ;
8766 const envContent = `\n# Langbase API key for https://langbase.com/${ login } \n${ envKeyName } =${ apiKey } \n\n` ;
8867
@@ -99,37 +78,45 @@ export async function auth() {
9978 const baiConfig = await loadConfig ( ) ;
10079 let envFile = baiConfig . envFilePath || '.env' ;
10180
102- fs . appendFileSync ( path . join ( process . cwd ( ) , envFile ) , envContent ) ;
81+ const envFileContent = await fs . readFile ( envFile , 'utf-8' ) ;
82+
83+ const oldKey = envFileContent
84+ . split ( '\n' )
85+ . reverse ( ) // Reverse to get the latest key if there are multiple
86+ . find ( line => line . includes ( 'LANGBASE_API_KEY' ) )
87+ ?. split ( '=' ) [ 1 ] ;
88+
89+ if ( oldKey ) {
90+ const shouldOverwrite = await confirm ( {
91+ message : `API key found in ${ envFile } . Overwrite?`
92+ } ) ;
93+
94+ if ( isCancel ( shouldOverwrite ) ) {
95+ cancel ( 'Operation cancelled.' ) ;
96+ process . exit ( 0 ) ;
97+ }
98+
99+ if ( ! shouldOverwrite ) {
100+ outro (
101+ color . yellow ( 'Operation cancelled. API key not overwritten.' )
102+ ) ;
103+ process . exit ( 0 ) ;
104+ }
105+
106+ const newEnvContent = envFileContent . replace (
107+ new RegExp ( `LANGBASE_API_KEY=${ oldKey } ` ) ,
108+ envContent . trim ( )
109+ ) ;
110+
111+ await fs . writeFile ( path . join ( process . cwd ( ) , envFile ) , newEnvContent ) ;
112+ } else {
113+ await fs . appendFile ( path . join ( process . cwd ( ) , envFile ) , envContent ) ;
114+ }
103115
104116 outro (
105117 color . green (
106- `Authentication successful. Credentials stored in config and ${ envFile } `
118+ `Authentication successful. Credentials stored in ${ envFile } `
107119 )
108120 ) ;
109- console . log ( color . dim ( `Config file location: ${ config . path } ` ) ) ;
110121 process . exit ( 0 ) ;
111122}
112-
113- export function getStoredAuth ( ) : Account | undefined {
114- const accounts = ( config . get ( 'accounts' ) as Account [ ] ) || [ ] ;
115- const currentLogin = config . get ( 'currentAccount' ) as string | undefined ;
116-
117- if ( currentLogin ) {
118- return accounts . find ( account => account . login === currentLogin ) ;
119- }
120-
121- return accounts [ 0 ] ; // Return the first account if no current account is set
122- }
123-
124- export function getStoredAccounts ( ) : Account [ ] {
125- return ( config . get ( 'accounts' ) as Account [ ] ) || [ ] ;
126- }
127-
128- export function setCurrentAccount ( login : string ) : boolean {
129- const accounts = getStoredAccounts ( ) ;
130- if ( accounts . some ( account => account . login === login ) ) {
131- config . set ( 'currentAccount' , login ) ;
132- return true ;
133- }
134- return false ;
135- }
0 commit comments