import * as readline from 'readline'; import chalk from 'chalk'; import type { PermissionContext, PermissionDecision } from './types.js'; /** * 在终端中提示用户确认权限 */ export async function promptPermission(ctx: PermissionContext): Promise { const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); return new Promise((resolve) => { console.log(''); console.log(chalk.yellow('⚠️ 权限确认')); console.log(chalk.cyan('命令: ') + chalk.white(ctx.command)); console.log(chalk.cyan('目录: ') + chalk.gray(ctx.workdir)); if (ctx.externalPaths && ctx.externalPaths.length > 0) { console.log(chalk.red('⚠️ 此命令访问项目目录外的路径:')); ctx.externalPaths.forEach(p => { console.log(chalk.red(' • ') + chalk.gray(p)); }); } if (ctx.patterns && ctx.patterns.length > 0) { console.log(chalk.gray('匹配模式: ') + ctx.patterns.join(', ')); } console.log(''); console.log(chalk.white('选择操作:')); console.log(chalk.green(' [y] ') + '允许执行'); console.log(chalk.green(' [Y] ') + '允许执行,并记住此类命令(本次会话)'); console.log(chalk.red(' [n] ') + '拒绝执行'); console.log(chalk.red(' [N] ') + '拒绝执行,并记住此类命令(本次会话)'); console.log(''); rl.question(chalk.yellow('请选择 [y/Y/n/N]: '), (answer) => { rl.close(); const choice = answer.trim(); switch (choice) { case 'y': resolve({ allow: true, remember: false }); break; case 'Y': resolve({ allow: true, remember: true }); break; case 'N': resolve({ allow: false, remember: true }); break; case 'n': default: resolve({ allow: false, remember: false }); break; } }); }); } /** * 显示权限被拒绝的消息 */ export function showPermissionDenied(command: string, reason: string): void { console.log(''); console.log(chalk.red('🚫 权限被拒绝')); console.log(chalk.cyan('命令: ') + chalk.white(command)); console.log(chalk.cyan('原因: ') + chalk.gray(reason)); console.log(''); } /** * 显示权限允许的消息 */ export function showPermissionAllowed(command: string): void { console.log(chalk.green('✓ ') + chalk.gray(`执行: ${command}`)); }