openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
48 lines (41 loc) • 1.48 kB
JavaScript
import { readFileSync } from 'node:fs';
// right, we don't use dotenv ;)
export const loadDotEnv = (filePath = '.env') => {
try {
const envVariables = {};
const envContent = filePath ? readFileSync(filePath, 'utf-8') : '';
for (const line of envContent.split('\n')) {
// trim spaces and ignore comments
const trimmedLine = line.trim();
if (trimmedLine && !trimmedLine.startsWith('#')) {
const [key, value] = trimmedLine.split('=');
if (key && value !== undefined) {
// remove quotes from value if present
envVariables[key.trim()] = value.trim().replace(/^\"|\"$/g, '');
}
}
}
for (const [key, value] of Object.entries(envVariables)) {
if (!process.env[key]) {
process.env[key] = value;
}
}
return envVariables;
} catch (error) {
return {};
}
};
const secrets = loadDotEnv();
/**
* Retrieves an environment variable, agnostic to the ESM or CJS runtime environment.
* In CI mode (CI=true) returns the CI_${name} invariant.
* Also logs the env variable name and the first three characters of its value (if available).
*/
export const getEnv = (name) => {
const value = secrets[name] || process.env[name] || import.meta?.env?.[name];
console.log('[ENV] Name:', name, 'Value:', !value ? value : `"${String(value).slice(-3) || "(empty)"}" (last 3 characters)`);
return value;
};
export const setEnv = (name, value) => {
secrets[name] = value;
};