scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ❤️.
71 lines (70 loc) • 2.71 kB
JavaScript
// File: src/commands/switch.ts
import readline from 'readline';
import { Config, writeConfig } from '../config.js';
import { getRepoKeyForPath } from '../utils/contentUtils.js';
import chalk from 'chalk';
export function runSwitchCommand(inputPathOrKey) {
const config = Config.getRaw();
// Direct key match
if (config.repos[inputPathOrKey]) {
config.activeRepo = inputPathOrKey;
Config.setGitHubToken(config.repos[inputPathOrKey].githubToken ?? '');
console.log(`✅ Switched active repo to key: ${inputPathOrKey}`);
}
else {
// Path match only if key match failed
const repoKey = getRepoKeyForPath(inputPathOrKey, config);
if (!repoKey) {
console.error(`❌ No repo found matching path or key: "${inputPathOrKey}"`);
process.exit(1);
}
config.activeRepo = repoKey;
Config.setGitHubToken(config.repos[repoKey]?.githubToken ?? '');
console.log(`✅ Switched active repo to path match: ${repoKey}`);
}
// Persist change
writeConfig(config);
}
export async function runInteractiveSwitch() {
const config = Config.getRaw();
const keys = Object.keys(config.repos || {});
if (!keys.length) {
console.log('⚠️ No repositories configured.');
return;
}
// Auto-switch to the other repo if only 2 are present
if (keys.length === 2) {
const current = config.activeRepo;
const other = keys.find(k => k !== current);
if (other) {
runSwitchCommand(other);
return;
}
}
// Otherwise, show interactive selection
console.log('\n📁 Available Repositories:\n');
keys.forEach((key, i) => {
const isActive = config.activeRepo === key ? chalk.green('(active)') : '';
const dir = config.repos[key]?.indexDir ?? '';
// Color the number using chalk.blue and make active repo green
const numberedRepo = chalk.blue(`${i + 1})`);
// Highlight the active repo in green and list it
console.log(`${numberedRepo} ${key} ${isActive}`);
// Use light grey for the indexDir
console.log(` ↳ ${chalk.grey(dir)}`);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('\n👉 Select a repository number to activate: ', (answer) => {
rl.close();
const index = parseInt(answer.trim(), 10) - 1;
if (isNaN(index) || index < 0 || index >= keys.length) {
console.log('❌ Invalid selection.');
return;
}
const selectedKey = keys[index];
runSwitchCommand(selectedKey);
});
}