refactor: split config module

This commit is contained in:
Peter Steinberger
2026-01-04 07:05:04 +01:00
parent 5e36e2c3f3
commit c9504a6f20
16 changed files with 2236 additions and 2082 deletions

45
src/config/talk.ts Normal file
View File

@@ -0,0 +1,45 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
type TalkApiKeyDeps = {
fs?: typeof fs;
os?: typeof os;
path?: typeof path;
};
export function readTalkApiKeyFromProfile(
deps: TalkApiKeyDeps = {},
): string | null {
const fsImpl = deps.fs ?? fs;
const osImpl = deps.os ?? os;
const pathImpl = deps.path ?? path;
const home = osImpl.homedir();
const candidates = [".profile", ".zprofile", ".zshrc", ".bashrc"].map(
(name) => pathImpl.join(home, name),
);
for (const candidate of candidates) {
if (!fsImpl.existsSync(candidate)) continue;
try {
const text = fsImpl.readFileSync(candidate, "utf-8");
const match = text.match(
/(?:^|\n)\s*(?:export\s+)?ELEVENLABS_API_KEY\s*=\s*["']?([^\n"']+)["']?/,
);
const value = match?.[1]?.trim();
if (value) return value;
} catch {
// Ignore profile read errors.
}
}
return null;
}
export function resolveTalkApiKey(
env: NodeJS.ProcessEnv = process.env,
deps: TalkApiKeyDeps = {},
): string | null {
const envValue = (env.ELEVENLABS_API_KEY ?? "").trim();
if (envValue) return envValue;
return readTalkApiKeyFromProfile(deps);
}