scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** > **100% local • No token cost • Private by design • GDPR-friendly** — made in Denmark/EU with ❤️.
78 lines (77 loc) • 2.93 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;
}
// Otherwise, show interactive selection
console.log('\n📁 Available Repositories:\n');
console.log('\n📁 Available Repositories:\n');
keys.forEach((key, i) => {
const isActive = config.activeRepo === key ? chalk.green('(active)') : '';
const dir = config.repos[key]?.indexDir ?? '';
console.log(`${chalk.blue(`${i + 1})`)} ${key} ${isActive}`);
console.log(` ↳ ${chalk.grey(dir)}`);
});
console.log(chalk.yellow(`\n0) Cancel`));
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('\n👉 Select a repository number to activate: ', (answer) => {
rl.close();
const choice = parseInt(answer.trim(), 10);
if (choice === 0) {
console.log('ℹ️ Cancelled.');
return;
}
const index = choice - 1;
if (isNaN(index) || index < 0 || index >= keys.length) {
console.log('❌ Invalid selection.');
return;
}
runSwitchCommand(keys[index]);
});
}
export async function statusIndex() {
const config = Config.getRaw();
const activeRepo = config.activeRepo;
if (!activeRepo) {
console.log(chalk.red('🔴 No active repository'));
console.log(' Run `scai index list` to select one');
return;
}
const repo = config.repos?.[activeRepo];
const dir = repo?.indexDir ?? '(unknown path)';
console.log(chalk.green('🟢 Active repository'));
console.log(` ↳ ${activeRepo}`);
console.log(` ↳ ${dir}`);
}