@deriv-com/shiftai-cli
Version:
A comprehensive AI code detection and analysis CLI tool for tracking AI-generated code in projects
145 lines (119 loc) • 4.9 kB
JavaScript
const chalk = require('chalk');
const crypto = require('crypto');
const path = require('path');
const RemoteSelector = require('../core/remote-selector');
/**
* Remote Command - Manage remote selection for projects
*/
async function remoteCommand(options = {}) {
try {
// Use same project hash logic as hooks to ensure consistent config paths
const projectRoot = process.cwd();
const projectHash = crypto.createHash('sha256').update(path.resolve(projectRoot)).digest('hex').substring(0, 16);
const remoteSelector = new RemoteSelector({
projectRoot: projectRoot,
projectHash: projectHash
});
if (options.list) {
await listRemotes(remoteSelector);
} else if (options.select) {
await selectRemote(remoteSelector);
} else if (options.reset) {
await resetRemote(remoteSelector);
} else if (options.status) {
await showRemoteStatus(remoteSelector);
} else {
// Default: show current status
await showRemoteStatus(remoteSelector);
}
} catch (error) {
console.error(chalk.red('❌ Remote command failed:'), error.message);
process.exit(1);
}
}
/**
* List all available remotes
*/
async function listRemotes(remoteSelector) {
console.log(chalk.blue('📡 Available Git Remotes\n'));
const remotes = remoteSelector.getAvailableRemotes();
if (remotes.length === 0) {
console.log(chalk.yellow('⚠ No git remotes found in this project'));
return;
}
const selectedRemote = await remoteSelector.getSelectedRemote();
remotes.forEach((remote, index) => {
const isSelected = remote.name === selectedRemote;
const marker = isSelected ? chalk.green('✓') : ' ';
const nameColor = isSelected ? chalk.green : chalk.cyan;
console.log(`${marker} ${nameColor(remote.name)} (${remote.organization}/${remote.repository})`);
console.log(` ${chalk.gray(remote.url)}`);
if (isSelected) {
console.log(` ${chalk.green('Currently selected for diff comparisons')}`);
}
console.log();
});
}
/**
* Select/change the main remote
*/
async function selectRemote(remoteSelector) {
console.log(chalk.blue('🔄 Selecting Main Remote for Diff Comparisons\n'));
const selectedRemote = await remoteSelector.promptForRemoteSelection();
console.log();
console.log(chalk.green('✅ Remote selection updated successfully!'));
console.log(chalk.blue(`All git diff comparisons will now use: ${selectedRemote}`));
}
/**
* Reset remote selection
*/
async function resetRemote(remoteSelector) {
console.log(chalk.yellow('🔄 Resetting remote selection...\n'));
const currentRemote = await remoteSelector.getSelectedRemote();
if (!currentRemote) {
console.log(chalk.yellow('⚠ No remote selection found for this project'));
return;
}
console.log(chalk.gray(`Current selection: ${currentRemote}`));
const success = await remoteSelector.resetRemoteSelection();
if (success) {
console.log(chalk.green('✅ Remote selection reset successfully'));
console.log(chalk.blue('ShiftAI will prompt for remote selection on next diff operation'));
} else {
console.log(chalk.red('❌ Failed to reset remote selection'));
}
}
/**
* Show current remote status
*/
async function showRemoteStatus(remoteSelector) {
console.log(chalk.blue('📊 Remote Selection Status\n'));
const selectedRemote = await remoteSelector.getSelectedRemote();
const availableRemotes = remoteSelector.getAvailableRemotes();
console.log(`Available remotes: ${chalk.cyan(availableRemotes.length)}`);
if (selectedRemote) {
const remoteInfo = availableRemotes.find(r => r.name === selectedRemote);
if (remoteInfo) {
console.log(`Selected remote: ${chalk.green(selectedRemote)} (${remoteInfo.organization}/${remoteInfo.repository})`);
console.log(`Status: ${chalk.green('✓ Configured')}`);
} else {
console.log(`Selected remote: ${chalk.red(selectedRemote)} (not found)`);
console.log(`Status: ${chalk.red('❌ Invalid - remote no longer exists')}`);
}
} else {
console.log(`Selected remote: ${chalk.yellow('None')}`);
if (availableRemotes.length > 1) {
console.log(`Status: ${chalk.yellow('⚠ Will prompt on next operation')}`);
} else if (availableRemotes.length === 1) {
console.log(`Status: ${chalk.blue('ℹ Will auto-select only remote')}`);
} else {
console.log(`Status: ${chalk.red('❌ No remotes found')}`);
}
}
console.log();
console.log(chalk.gray('Commands:'));
console.log(chalk.gray(' shai remote --list List all remotes'));
console.log(chalk.gray(' shai remote --select Change remote selection'));
console.log(chalk.gray(' shai remote --reset Reset selection'));
}
module.exports = remoteCommand;