UNPKG

pr-desc-cli

Version:
52 lines (51 loc) 1.6 kB
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs"; import { join } from "path"; import { homedir } from "os"; import { config } from "dotenv"; const CONFIG_DIR = join(homedir(), ".pr-desc"); const CONFIG_FILE = join(CONFIG_DIR, "config.json"); const ENV_FILE = join(CONFIG_DIR, ".env"); export function loadConfig() { // global config if (existsSync(ENV_FILE)) { config({ path: ENV_FILE }); } config(); let userConfig = {}; if (existsSync(CONFIG_FILE)) { try { const configContent = readFileSync(CONFIG_FILE, "utf-8"); userConfig = JSON.parse(configContent); } catch (error) { console.warn("Warning: Could not parse config file"); } } return userConfig; } export function saveConfig(config) { if (!existsSync(CONFIG_DIR)) { mkdirSync(CONFIG_DIR, { recursive: true }); } writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2)); } export function getApiKey(provider) { const config = loadConfig(); switch (provider) { case "groq": return process.env.GROQ_API_KEY || config.apiKeys?.groq; case "deepinfra": return process.env.DEEPINFRA_API_KEY || config.apiKeys?.deepinfra; default: return undefined; } } export function setApiKey(provider, apiKey) { const config = loadConfig(); if (!config.apiKeys) { config.apiKeys = {}; } config.apiKeys[provider] = apiKey; saveConfig(config); console.log(`✅ API key for ${provider} saved to global config`); }