feat: add base url mechanism

This commit is contained in:
2026-03-24 19:15:47 +01:00
parent a9bbf21317
commit 4aeb108c55
5 changed files with 193 additions and 6 deletions

View File

@@ -0,0 +1,47 @@
import fs from 'fs-extra';
import path from 'path';
export interface ApiKeyInfo {
key: string;
url?: string;
}
const SKIP_DIRS = new Set(['node_modules', '.git', 'dist', '.angular', 'coverage', '.cache']);
export function findEnvironmentFile(dir: string, maxDepth = 8, currentDepth = 0): string | null {
if (currentDepth > maxDepth) return null;
try {
const entries = fs.readdirSync(dir, { withFileTypes: true });
for (const entry of entries) {
if (SKIP_DIRS.has(entry.name)) continue;
const fullPath = path.join(dir, entry.name);
if (entry.isFile() && entry.name === 'environment.ts') {
return fullPath;
}
if (entry.isDirectory()) {
const found = findEnvironmentFile(fullPath, maxDepth, currentDepth + 1);
if (found) return found;
}
}
} catch {}
return null;
}
/**
* Parses environment.ts content and returns all top-level keys that contain "api"
* (case-insensitive), along with their `url` value if present.
*/
export function parseApiKeys(content: string): ApiKeyInfo[] {
const result: ApiKeyInfo[] = [];
const keyRegex = /^ {2}(\w*[Aa][Pp][Ii]\w*)\s*:/gm;
let match: RegExpExecArray | null;
while ((match = keyRegex.exec(content)) !== null) {
const key = match[1];
const afterKey = content.slice(match.index);
const urlMatch = afterKey.match(/url:\s*['"`]([^'"`\n]+)['"`]/);
result.push({ key, url: urlMatch?.[1] });
}
return result;
}