Files
automaker/apps/server/src/routes/setup/common.ts
Kacper 060a789b45 refactor: update all imports to use shared packages
- Updated 150+ files to import from @automaker/* packages
- Server imports now use @automaker/utils, @automaker/platform, @automaker/types, @automaker/model-resolver, @automaker/dependency-resolver, @automaker/git-utils
- UI imports now use @automaker/dependency-resolver and @automaker/types
- Deleted duplicate dependency-resolver files (222 lines eliminated)
- Updated dependency-resolver to use ES modules for Vite compatibility
- Added type annotation fix in auto-mode-service.ts
- Updated feature-loader to re-export Feature type from @automaker/types
- Both server and UI builds successfully verified

Phase 1 of server refactoring complete.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-19 23:46:27 +01:00

84 lines
1.9 KiB
TypeScript

/**
* Common utilities and state for setup routes
*/
import { createLogger } from "@automaker/utils";
import path from "path";
import fs from "fs/promises";
import {
getErrorMessage as getErrorMessageShared,
createLogError,
} from "../common.js";
const logger = createLogger("Setup");
// Storage for API keys (in-memory cache) - private
const apiKeys: Record<string, string> = {};
/**
* Get an API key for a provider
*/
export function getApiKey(provider: string): string | undefined {
return apiKeys[provider];
}
/**
* Set an API key for a provider
*/
export function setApiKey(provider: string, key: string): void {
apiKeys[provider] = key;
}
/**
* Get all API keys (for read-only access)
*/
export function getAllApiKeys(): Record<string, string> {
return { ...apiKeys };
}
/**
* Helper to persist API keys to .env file
*/
export async function persistApiKeyToEnv(
key: string,
value: string
): Promise<void> {
const envPath = path.join(process.cwd(), ".env");
try {
let envContent = "";
try {
envContent = await fs.readFile(envPath, "utf-8");
} catch {
// .env file doesn't exist, we'll create it
}
// Parse existing env content
const lines = envContent.split("\n");
const keyRegex = new RegExp(`^${key}=`);
let found = false;
const newLines = lines.map((line) => {
if (keyRegex.test(line)) {
found = true;
return `${key}=${value}`;
}
return line;
});
if (!found) {
// Add the key at the end
newLines.push(`${key}=${value}`);
}
await fs.writeFile(envPath, newLines.join("\n"));
logger.info(`[Setup] Persisted ${key} to .env file`);
} catch (error) {
logger.error(`[Setup] Failed to persist ${key} to .env:`, error);
throw error;
}
}
// Re-export shared utilities
export { getErrorMessageShared as getErrorMessage };
export const logError = createLogError(logger);