claude-switcher
Version:
Cross-platform CLI tool for switching between different Claude AI model configurations. Supports automatic backup, rollback, and multi-platform configuration management for Claude API integrations.
208 lines (207 loc) • 7.79 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserConfirmation = void 0;
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
class UserConfirmation {
static async confirm(options) {
if (options.warningMessage) {
console.log(chalk_1.default.yellow('⚠️ ') + chalk_1.default.yellow.bold('警告:'));
console.log(chalk_1.default.yellow(` ${options.warningMessage}`));
console.log();
}
if (options.details && options.details.length > 0) {
console.log(chalk_1.default.blue('详细信息:'));
options.details.forEach(detail => {
console.log(chalk_1.default.gray(` • ${detail}`));
});
console.log();
}
if (options.requireExplicitConfirmation) {
const { confirmText } = await inquirer_1.default.prompt([
{
type: 'input',
name: 'confirmText',
message: `请输入 "${chalk_1.default.red('确认')}" 以继续操作:`,
validate: (input) => {
if (input.trim() === '确认') {
return true;
}
return '请输入 "确认" 以继续操作';
}
}
]);
return confirmText.trim() === '确认';
}
const { confirmed } = await inquirer_1.default.prompt([
{
type: 'confirm',
name: 'confirmed',
message: options.message,
default: options.default ?? false
}
]);
return confirmed;
}
static async select(message, choices, options) {
const formattedChoices = choices.map(choice => ({
name: choice.description
? `${choice.name} ${chalk_1.default.gray(`- ${choice.description}`)}`
: choice.name,
value: choice.value,
disabled: choice.disabled || false
}));
if (options?.allowCancel) {
formattedChoices.push({
name: chalk_1.default.gray('取消操作'),
value: null,
disabled: false
});
}
if (options?.showHelp) {
console.log(chalk_1.default.blue('💡 提示: 使用方向键选择,回车确认'));
console.log();
}
const result = await inquirer_1.default.prompt({
type: 'list',
name: 'selected',
message,
choices: formattedChoices,
pageSize: options?.pageSize ?? 10
});
return result.selected;
}
static async multiSelect(message, choices, options) {
const formattedChoices = choices.map(choice => ({
name: choice.description
? `${choice.name} ${chalk_1.default.gray(`- ${choice.description}`)}`
: choice.name,
value: choice.value,
disabled: choice.disabled || false
}));
const result = await inquirer_1.default.prompt({
type: 'checkbox',
name: 'selected',
message,
choices: formattedChoices,
pageSize: options?.pageSize ?? 10
});
return result.selected;
}
static async input(message, options) {
const result = await inquirer_1.default.prompt({
type: options?.mask ? 'password' : 'input',
name: 'input',
message,
default: options?.default
});
return result.input;
}
static async confirmWithSummary(title, items, confirmMessage = '确认执行此操作?') {
console.log(chalk_1.default.blue.bold(`\n${title}:`));
console.log();
Object.entries(items).forEach(([key, value]) => {
console.log(` ${chalk_1.default.cyan(key + ':')} ${value}`);
});
console.log();
return await UserConfirmation.confirm({
message: confirmMessage,
default: false
});
}
static async confirmDangerousOperation(operation, consequences, confirmMessage = '我了解风险并确认执行此操作') {
console.log(chalk_1.default.red.bold('⚠️ 危险操作警告'));
console.log();
console.log(chalk_1.default.yellow(`即将执行: ${operation}`));
console.log();
if (consequences.length > 0) {
console.log(chalk_1.default.red('此操作可能导致:'));
consequences.forEach(consequence => {
console.log(chalk_1.default.red(` • ${consequence}`));
});
console.log();
}
return await UserConfirmation.confirm({
message: confirmMessage,
default: false,
requireExplicitConfirmation: true
});
}
static async confirmSteps(steps) {
console.log(chalk_1.default.blue.bold('\n操作步骤确认:'));
console.log();
for (let i = 0; i < steps.length; i++) {
const step = steps[i];
const stepNumber = i + 1;
console.log(chalk_1.default.cyan(`步骤 ${stepNumber}: ${step.title}`));
console.log(chalk_1.default.gray(` ${step.description}`));
if (step.required !== false) {
const confirmed = await UserConfirmation.confirm({
message: `确认执行步骤 ${stepNumber}?`,
default: true
});
if (!confirmed) {
console.log(chalk_1.default.yellow('操作已取消'));
return false;
}
}
console.log();
}
return true;
}
static async confirmWithProgress(operation, estimatedTime, steps) {
console.log(chalk_1.default.blue.bold(`\n即将执行: ${operation}`));
console.log(chalk_1.default.gray(`预计耗时: ${estimatedTime} 秒`));
if (steps && steps.length > 0) {
console.log(chalk_1.default.gray('\n执行步骤:'));
steps.forEach((step, index) => {
console.log(chalk_1.default.gray(` ${index + 1}. ${step}`));
});
}
console.log();
return await UserConfirmation.confirm({
message: '确认开始执行?',
default: true
});
}
static async showInfo(title, content, type = 'info') {
let icon;
let color;
switch (type) {
case 'warning':
icon = '⚠️';
color = chalk_1.default.yellow;
break;
case 'error':
icon = '❌';
color = chalk_1.default.red;
break;
default:
icon = 'ℹ️';
color = chalk_1.default.blue;
}
console.log(chalk_1.default.bold(color(`\n${icon} ${title}`)));
console.log();
if (Array.isArray(content)) {
content.forEach(line => {
console.log(color(` ${line}`));
});
}
else {
console.log(color(` ${content}`));
}
console.log();
await inquirer_1.default.prompt([
{
type: 'input',
name: 'acknowledge',
message: '按回车键继续...',
default: ''
}
]);
}
}
exports.UserConfirmation = UserConfirmation;