feat: add base url mechanism
This commit is contained in:
47
src/utils/environment-finder.ts
Normal file
47
src/utils/environment-finder.ts
Normal 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;
|
||||
}
|
||||
91
src/utils/prompt.ts
Normal file
91
src/utils/prompt.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import readline from 'readline';
|
||||
import { ApiKeyInfo } from './environment-finder';
|
||||
import { colors } from './logger';
|
||||
|
||||
function ask(rl: readline.Interface, query: string): Promise<string> {
|
||||
return new Promise((resolve) => rl.question(query, resolve));
|
||||
}
|
||||
|
||||
/**
|
||||
* Interactively asks the user which environment API key to use for each tag.
|
||||
* Returns a map of tag → environment key (e.g. { "SupplyingMaintenances": "suppliyingMaintenancesApi" }).
|
||||
*/
|
||||
export async function askApiKeysForTags(
|
||||
tags: string[],
|
||||
apiKeys: ApiKeyInfo[]
|
||||
): Promise<Record<string, string>> {
|
||||
const result: Record<string, string> = {};
|
||||
|
||||
if (tags.length === 0) return result;
|
||||
|
||||
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
||||
|
||||
console.log('\n' + '─'.repeat(60));
|
||||
console.log(`${colors.bright}🔑 Configuración de URLs base para repositorios${colors.reset}`);
|
||||
console.log('─'.repeat(60));
|
||||
|
||||
for (const tag of tags) {
|
||||
result[tag] = await askApiKeyForTag(rl, tag, apiKeys);
|
||||
}
|
||||
|
||||
rl.close();
|
||||
return result;
|
||||
}
|
||||
|
||||
async function askApiKeyForTag(
|
||||
rl: readline.Interface,
|
||||
tagName: string,
|
||||
apiKeys: ApiKeyInfo[]
|
||||
): Promise<string> {
|
||||
console.log(
|
||||
`\n Repositorio: ${colors.cyan}${tagName}RepositoryImpl${colors.reset}`
|
||||
);
|
||||
|
||||
if (apiKeys.length > 0) {
|
||||
console.log(` Selecciona la clave de environment para la URL base:\n`);
|
||||
apiKeys.forEach((k, i) => {
|
||||
const urlText = k.url ? `\n ${colors.cyan}${k.url}${colors.reset}` : '';
|
||||
console.log(` ${colors.bright}${i + 1})${colors.reset} ${k.key}${urlText}`);
|
||||
});
|
||||
console.log(` ${colors.bright}${apiKeys.length + 1})${colors.reset} Escribir manualmente`);
|
||||
} else {
|
||||
console.log(` No se encontraron claves de API en environment.ts.`);
|
||||
console.log(` Escribe la clave manualmente (ej: myApi):\n`);
|
||||
}
|
||||
|
||||
while (true) {
|
||||
const answer = (await ask(rl, `\n > `)).trim();
|
||||
|
||||
if (apiKeys.length > 0) {
|
||||
const num = parseInt(answer, 10);
|
||||
if (!isNaN(num) && num >= 1 && num <= apiKeys.length) {
|
||||
const chosen = apiKeys[num - 1].key;
|
||||
console.log(` ✅ ${colors.bright}environment.${chosen}.url${colors.reset}`);
|
||||
return chosen;
|
||||
}
|
||||
if (num === apiKeys.length + 1 || answer === '') {
|
||||
const manual = (await ask(rl, ` Escribe la clave (ej: myApi): `)).trim();
|
||||
if (manual) {
|
||||
console.log(` ✅ ${colors.bright}environment.${manual}.url${colors.reset}`);
|
||||
return manual;
|
||||
}
|
||||
console.log(` ⚠️ La clave no puede estar vacía.`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (answer && isNaN(num)) {
|
||||
console.log(` ✅ ${colors.bright}environment.${answer}.url${colors.reset}`);
|
||||
return answer;
|
||||
}
|
||||
console.log(
|
||||
` ⚠️ Opción inválida. Elige un número del 1 al ${apiKeys.length + 1} o escribe la clave directamente.`
|
||||
);
|
||||
} else {
|
||||
if (answer) {
|
||||
console.log(` ✅ ${colors.bright}environment.${answer}.url${colors.reset}`);
|
||||
return answer;
|
||||
}
|
||||
console.log(` ⚠️ La clave no puede estar vacía.`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user