Skip to content

Commit 043e4b9

Browse files
committed
add config
1 parent efbe907 commit 043e4b9

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "i18n-magic",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"license": "MIT",
55
"description": "CLI to help you manage your locales JSON with translations, replacements, etc. with OpenAI",
66
"repository": {

src/index.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Command } from "commander"
2+
import dotenv from "dotenv"
23
import OpenAI from "openai"
34
import { checkMissing } from "./commands/check-missing"
45
import { replaceTranslation } from "./commands/replace"
@@ -17,10 +18,6 @@ program
1718
.option("-c, --config <path>", "path to config file")
1819
.option("-e, --env <path>", "path to .env file")
1920

20-
require("dotenv").config({
21-
path: program.opts().env || ".env",
22-
})
23-
2421
const commands: CommandType[] = [
2522
{
2623
name: "scan",
@@ -47,19 +44,25 @@ for (const command of commands) {
4744
.command(command.name)
4845
.description(command.description)
4946
.action(async () => {
47+
const res = dotenv.config({
48+
path: program.opts().env || ".env",
49+
})
50+
51+
const key = res.parsed?.OPENAI_API_KEY
52+
5053
const config = await loadConfig({
5154
configPath: program.opts().config,
5255
})
5356

54-
if (!process.env.OPENAI_API_KEY) {
57+
if (!key) {
5558
console.error(
5659
"Please provide an OpenAI API key in your .env file, called OPENAI_API_KEY.",
5760
)
5861
process.exit(0)
5962
}
6063

6164
const openai = new OpenAI({
62-
apiKey: process.env.OPENAI_API_KEY,
65+
apiKey: key,
6366
})
6467

6568
command.action({ ...config, openai })

0 commit comments

Comments
 (0)