2025-04-19 19:45:42 +01:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as dotenv from 'dotenv';
|
|
|
|
|
import * as fs from 'node:fs';
|
|
|
|
|
import * as path from 'node:path';
|
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
|
|
export class Config {
|
|
|
|
|
private apiKey: string;
|
|
|
|
|
private model: string;
|
|
|
|
|
private targetDir: string;
|
2025-04-20 20:20:40 +01:00
|
|
|
private debugMode: boolean;
|
2025-04-19 19:45:42 +01:00
|
|
|
|
2025-04-20 20:20:40 +01:00
|
|
|
constructor(
|
|
|
|
|
apiKey: string,
|
|
|
|
|
model: string,
|
|
|
|
|
targetDir: string,
|
|
|
|
|
debugMode: boolean,
|
|
|
|
|
) {
|
2025-04-19 19:45:42 +01:00
|
|
|
this.apiKey = apiKey;
|
|
|
|
|
this.model = model;
|
|
|
|
|
this.targetDir = targetDir;
|
2025-04-20 20:20:40 +01:00
|
|
|
this.debugMode = debugMode;
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getApiKey(): string {
|
|
|
|
|
return this.apiKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getModel(): string {
|
|
|
|
|
return this.model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTargetDir(): string {
|
|
|
|
|
return this.targetDir;
|
|
|
|
|
}
|
2025-04-20 20:20:40 +01:00
|
|
|
|
|
|
|
|
getDebugMode(): boolean {
|
|
|
|
|
return this.debugMode;
|
|
|
|
|
}
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function findEnvFile(startDir: string): string | null {
|
|
|
|
|
let currentDir = path.resolve(startDir);
|
|
|
|
|
while (true) {
|
|
|
|
|
const envPath = path.join(currentDir, '.env');
|
|
|
|
|
if (fs.existsSync(envPath)) {
|
|
|
|
|
return envPath;
|
|
|
|
|
}
|
|
|
|
|
const parentDir = path.dirname(currentDir);
|
|
|
|
|
if (parentDir === currentDir || !parentDir) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
currentDir = parentDir;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function loadEnvironment(): void {
|
|
|
|
|
const envFilePath = findEnvFile(process.cwd());
|
|
|
|
|
if (!envFilePath) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dotenv.config({ path: envFilePath });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createServerConfig(
|
|
|
|
|
apiKey: string,
|
|
|
|
|
model: string,
|
|
|
|
|
targetDir: string,
|
2025-04-20 20:20:40 +01:00
|
|
|
debugMode: boolean,
|
2025-04-19 19:45:42 +01:00
|
|
|
): Config {
|
2025-04-20 20:20:40 +01:00
|
|
|
return new Config(apiKey, model, path.resolve(targetDir), debugMode);
|
2025-04-19 19:45:42 +01:00
|
|
|
}
|