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

91
src/utils/prompt.ts Normal file
View 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.`);
}
}
}