polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
245 lines • 10.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthDiagnostics = void 0;
class AuthDiagnostics {
constructor(authProvider) {
this.events = [];
this.authProvider = authProvider;
}
runDiagnostics(options, includeVerbose = false) {
this.events = [];
this.logEvent('source_detected', '开始认证诊断');
const diagnostics = this.authProvider.getDiagnostics(options);
const context = this.authProvider.getAuthenticationContext(options);
const issues = [];
const recommendations = [];
if (context && context.source) {
this.logEvent('source_selected', `选择认证源: ${context.source.metadata.source}`, context.source.type);
this.analyzeSelectedSource(context.source, issues, recommendations);
}
else {
this.logEvent('error', '未找到有效的认证源');
this.analyzeNoAuthenticationFound(diagnostics, issues, recommendations);
}
this.analyzeAvailableSources(diagnostics.availableSources, issues, recommendations);
const status = this.determineOverallStatus(context, issues);
const summary = this.generateSummary(status, context, issues);
const result = {
status,
summary,
details: {
sources: diagnostics.availableSources,
...(diagnostics.selectedSource && { selectedSource: diagnostics.selectedSource }),
issues,
recommendations,
},
};
if (includeVerbose) {
result.verbose = {
timeline: [...this.events],
environment: this.getEnvironmentInfo(),
configuration: this.getConfigurationInfo(options),
};
}
return result;
}
generateReport(options, includeVerbose = false) {
const result = this.runDiagnostics(options, includeVerbose);
const lines = [];
lines.push('=== PolyV CLI 认证诊断报告 ===\n');
const statusIcon = result.status === 'success' ? '✅' : result.status === 'warning' ? '⚠️' : '❌';
lines.push(`状态: ${statusIcon} ${result.summary}\n`);
if (result.details.selectedSource) {
const source = result.details.selectedSource;
lines.push('当前认证源:');
lines.push(` 类型: ${source.metadata.source}`);
if (source.metadata.accountName) {
lines.push(` 账号: ${source.metadata.accountName}`);
}
lines.push(` 优先级: ${source.priority} (1=最高, 4=最低)`);
lines.push('');
}
if (result.details.sources.length > 0) {
lines.push('可用认证源:');
result.details.sources.forEach((source, index) => {
const complete = !!(source.appId && source.appSecret);
const status = complete ? '✅' : '❌';
lines.push(` ${index + 1}. ${status} ${source.metadata.source} (优先级 ${source.priority})`);
if (source.metadata.accountName) {
lines.push(` 账号: ${source.metadata.accountName}`);
}
if (!complete) {
const missing = [];
if (!source.appId)
missing.push('appId');
if (!source.appSecret)
missing.push('appSecret');
lines.push(` 缺少: ${missing.join(', ')}`);
}
});
lines.push('');
}
if (result.details.issues.length > 0) {
lines.push('发现的问题:');
result.details.issues.forEach((issue, index) => {
const icon = issue.severity === 'error' ? '❌' : issue.severity === 'warning' ? '⚠️' : 'ℹ️';
lines.push(` ${index + 1}. ${icon} ${issue.message}`);
if (issue.suggestions.length > 0) {
issue.suggestions.forEach(suggestion => {
lines.push(` 建议: ${suggestion}`);
});
}
});
lines.push('');
}
if (result.details.recommendations.length > 0) {
lines.push('建议操作:');
result.details.recommendations.forEach((rec, index) => {
lines.push(` ${index + 1}. ${rec}`);
});
lines.push('');
}
if (includeVerbose && result.verbose) {
lines.push('详细信息:');
if (result.verbose.timeline.length > 0) {
lines.push(' 诊断时间线:');
result.verbose.timeline.forEach(event => {
const time = event.timestamp.toLocaleTimeString();
lines.push(` ${time} - ${event.message}`);
});
lines.push('');
}
lines.push(' 环境信息:');
Object.entries(result.verbose.environment).forEach(([key, value]) => {
lines.push(` ${key}: ${value}`);
});
lines.push('');
}
return lines.join('\n');
}
logEvent(type, message, source, data) {
const event = {
timestamp: new Date(),
type,
message,
};
if (source !== undefined) {
event.source = source;
}
if (data !== undefined) {
event.data = data;
}
this.events.push(event);
}
analyzeSelectedSource(source, issues, recommendations) {
if (source.type === 'session' && source.metadata.context?.['error']) {
issues.push({
severity: 'warning',
type: 'session_account_issue',
message: source.metadata.context['message'] || '会话账号存在问题',
source: source.type,
suggestions: ['使用 \'polyv-live-cli use <account-name>\' 切换到有效账号'],
});
}
if (!source.appId || !source.appSecret) {
issues.push({
severity: 'error',
type: 'incomplete_authentication',
message: '认证信息不完整',
source: source.type,
suggestions: ['确保提供完整的 appId 和 appSecret'],
});
}
switch (source.type) {
case 'command-line':
recommendations.push('命令行参数具有最高优先级,将覆盖其他认证源');
break;
case 'session':
recommendations.push('会话账号仅在当前终端有效,其他终端需要单独设置');
break;
case 'environment':
recommendations.push('环境变量在整个系统会话中有效');
break;
case 'config':
recommendations.push('全局配置是默认的认证方式');
break;
}
}
analyzeNoAuthenticationFound(_diagnostics, issues, recommendations) {
issues.push({
severity: 'error',
type: 'no_authentication',
message: '未找到有效的认证信息',
suggestions: [
'使用 \'polyv-live-cli account add <account-name>\' 添加账号',
'设置环境变量: POLYV_APP_ID, POLYV_APP_SECRET',
'使用命令行参数: --app-id, --app-secret',
],
});
recommendations.push('建议首先添加账号配置,然后使用会话切换功能');
recommendations.push('对于临时使用,可以使用命令行参数或环境变量');
}
analyzeAvailableSources(sources, issues, recommendations) {
const incompleteCount = sources.filter(s => !s.appId || !s.appSecret).length;
if (incompleteCount > 0) {
issues.push({
severity: 'info',
type: 'incomplete_sources',
message: `发现 ${incompleteCount} 个不完整的认证源`,
suggestions: ['检查并完善认证配置'],
});
}
if (sources.length > 1) {
recommendations.push('检测到多个认证源,系统将按优先级自动选择');
recommendations.push('使用 --verbose 选项查看详细的认证源信息');
}
}
determineOverallStatus(context, issues) {
if (!context) {
return 'error';
}
const hasErrors = issues.some(i => i.severity === 'error');
const hasWarnings = issues.some(i => i.severity === 'warning');
if (hasErrors)
return 'error';
if (hasWarnings)
return 'warning';
return 'success';
}
generateSummary(status, context, issues) {
if (status === 'success' && context) {
return `认证配置正常,使用 ${context.source.metadata.source}`;
}
if (status === 'warning' && context) {
const warningCount = issues.filter(i => i.severity === 'warning').length;
return `认证可用但存在 ${warningCount} 个警告,使用 ${context.source.metadata.source}`;
}
const errorCount = issues.filter(i => i.severity === 'error').length;
return `认证配置存在 ${errorCount} 个错误,无法正常使用`;
}
getEnvironmentInfo() {
return {
nodeVersion: process.version,
platform: process.platform,
arch: process.arch,
cwd: process.cwd(),
envVarsSet: {
POLYV_APP_ID: !!process.env['POLYV_APP_ID'],
POLYV_APP_SECRET: !!process.env['POLYV_APP_SECRET'],
POLYV_USER_ID: !!process.env['POLYV_USER_ID'],
},
};
}
getConfigurationInfo(options) {
return {
commandLineOptions: {
appId: !!(options?.['appId']),
appSecret: !!(options?.['appSecret']),
userId: !!(options?.['userId']),
},
timestamp: new Date().toISOString(),
};
}
}
exports.AuthDiagnostics = AuthDiagnostics;
//# sourceMappingURL=auth-diagnostics.js.map