1
1
import { Command } from "commander"
2
+ import dotenv from "dotenv"
2
3
import OpenAI from "openai"
3
4
import { checkMissing } from "./commands/check-missing"
4
5
import { replaceTranslation } from "./commands/replace"
@@ -17,10 +18,6 @@ program
17
18
. option ( "-c, --config <path>" , "path to config file" )
18
19
. option ( "-e, --env <path>" , "path to .env file" )
19
20
20
- require ( "dotenv" ) . config ( {
21
- path : program . opts ( ) . env || ".env" ,
22
- } )
23
-
24
21
const commands : CommandType [ ] = [
25
22
{
26
23
name : "scan" ,
@@ -47,19 +44,25 @@ for (const command of commands) {
47
44
. command ( command . name )
48
45
. description ( command . description )
49
46
. action ( async ( ) => {
47
+ const res = dotenv . config ( {
48
+ path : program . opts ( ) . env || ".env" ,
49
+ } )
50
+
51
+ const key = res . parsed ?. OPENAI_API_KEY
52
+
50
53
const config = await loadConfig ( {
51
54
configPath : program . opts ( ) . config ,
52
55
} )
53
56
54
- if ( ! process . env . OPENAI_API_KEY ) {
57
+ if ( ! key ) {
55
58
console . error (
56
59
"Please provide an OpenAI API key in your .env file, called OPENAI_API_KEY." ,
57
60
)
58
61
process . exit ( 0 )
59
62
}
60
63
61
64
const openai = new OpenAI ( {
62
- apiKey : process . env . OPENAI_API_KEY ,
65
+ apiKey : key ,
63
66
} )
64
67
65
68
command . action ( { ...config , openai } )
0 commit comments