54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
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']);
|
|
|
|
/**
|
|
* Recursively searches for an `environment.ts` file starting from `dir`,
|
|
* up to `maxDepth` directory levels deep.
|
|
*/
|
|
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 {
|
|
//bypass errors
|
|
}
|
|
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;
|
|
}
|