synthia-doctor
Version:
Synthia Engine Synthia-Doctor - 开发synthia-doctor工具,集成AI构建优化建议(构建产物精简31%)
389 lines (383 loc) • 9.37 kB
TypeScript
/**
* Synthia 插件系统核心接口
* 参考 Rust Cargo 插件设计,支持动态注册命令
*/
/**
* 插件日志接口
*/
interface PluginLogger {
info: (message: string, ...args: any[]) => void;
warn: (message: string, ...args: any[]) => void;
error: (message: string, ...args: any[]) => void;
debug: (message: string, ...args: any[]) => void;
success: (message: string, ...args: any[]) => void;
}
/**
* 插件文件系统接口
*/
interface PluginFileSystem {
readFile: (filePath: string, encoding?: string) => Promise<string>;
writeFile: (filePath: string, content: string, encoding?: string) => Promise<void>;
exists: (path: string) => Promise<boolean>;
mkdir: (path: string, recursive?: boolean) => Promise<void>;
readdir: (path: string) => Promise<string[]>;
remove: (path: string) => Promise<void>;
copy: (src: string, dest: string) => Promise<void>;
move: (src: string, dest: string) => Promise<void>;
}
/**
* 插件 HTTP 客户端接口
*/
interface PluginHttpClient {
get: (url: string) => Promise<any>;
post: (url: string, data?: any) => Promise<any>;
put: (url: string, data?: any) => Promise<any>;
delete: (url: string) => Promise<any>;
}
/**
* 插件缓存接口
*/
interface PluginCache {
set: (key: string, value: any) => Promise<void>;
get: (key: string) => Promise<any>;
delete: (key: string) => Promise<void>;
clear: () => Promise<void>;
has: (key: string) => Promise<boolean>;
}
/**
* 插件 API 接口
*/
interface PluginAPI {
/** 注册命令 */
registerCommand: (command: PluginCommand) => void;
/** 注册钩子 */
registerHook: (hookName: string, hook: Function) => void;
/** JSON 参数校验 */
validateJSON: (schema: any, data: any) => {
valid: boolean;
errors: string[];
};
/** 日志记录器 */
logger: PluginLogger;
/** 文件系统操作 */
fs: PluginFileSystem;
/** HTTP 客户端 */
http: PluginHttpClient;
/** 缓存操作 */
cache: PluginCache;
}
/**
* 插件选项接口
*/
interface PluginOptions {
/** 插件配置 */
config: Record<string, any>;
/** 工程信息 */
project: {
name: string;
version: string;
root: string;
description?: string;
};
/** 全局选项 */
globalOptions: Record<string, any>;
}
/**
* 插件函数类型
*/
type PluginFunction = (api: PluginAPI, options: PluginOptions) => Promise<void> | void;
/**
* 插件命令定义
*/
interface PluginCommand {
/** 命令名称 */
name: string;
/** 命令描述 */
description: string;
/** 命令别名 */
aliases?: string[];
/** 命令选项 */
options?: PluginCommandOption[];
/** 命令参数 */
arguments?: PluginCommandArgument[];
/** 命令执行函数 */
action: (options: any, ...args: any[]) => Promise<void> | void;
/** 命令帮助信息 */
help?: string;
/** 命令示例 */
examples?: string[];
}
/**
* 插件命令选项
*/
interface PluginCommandOption {
/** 选项标识符 */
flags: string;
/** 选项描述 */
description: string;
/** 默认值 */
defaultValue?: any;
/** 是否必需 */
required?: boolean;
/** 选项类型 */
type?: 'string' | 'number' | 'boolean' | 'array';
/** 选项验证函数 */
validator?: (value: any) => boolean | string;
}
/**
* 插件命令参数
*/
interface PluginCommandArgument {
/** 参数名称 */
name: string;
/** 参数描述 */
description: string;
/** 是否必需 */
required?: boolean;
/** 参数类型 */
type?: 'string' | 'number' | 'boolean' | 'array';
/** 参数验证函数 */
validator?: (value: any) => boolean | string;
}
interface BuildStats {
startTime: number;
endTime: number;
duration: number;
bundleSize: number;
chunkCount: number;
moduleCount: number;
optimizationSuggestions: string[];
}
interface OptimizationResult {
/** 是否成功 */
success: boolean;
/** 优化建议 */
suggestions: string[];
/** 错误信息 */
errors?: string[];
}
/**
* 依赖分析结果
*/
interface DependencyAnalysis {
/** 依赖列表 */
dependencies: Array<{
name: string;
version: string;
type: 'dependencies' | 'devDependencies' | 'peerDependencies';
size: number;
used: boolean;
}>;
/** 未使用的依赖 */
unusedDependencies: string[];
/** 重复依赖 */
duplicateDependencies: string[];
/** 过时依赖 */
outdatedDependencies: string[];
}
/**
* 性能分析结果
*/
interface PerformanceAnalysis {
/** 包大小分析 */
bundleSize: {
total: number;
gzipped: number;
chunks: Array<{
name: string;
size: number;
gzipped: number;
}>;
};
/** 构建时间分析 */
buildTime: {
total: number;
phases: Array<{
name: string;
duration: number;
percentage: number;
}>;
};
/** 性能指标 */
metrics: {
firstContentfulPaint: number;
largestContentfulPaint: number;
};
}
/**
* 代码质量分析结果
*/
interface CodeQualityAnalysis {
/** 代码复杂度 */
complexity: {
average: number;
max: number;
files: Array<{
path: string;
complexity: number;
}>;
};
/** 代码重复率 */
duplication: {
percentage: number;
lines: number;
};
/** 代码覆盖率 */
coverage: {
lines: number;
functions: number;
branches: number;
statements: number;
};
}
/**
* 分析选项
*/
interface AnalysisOptions {
/** 是否分析依赖 */
analyzeDependencies: boolean;
/** 是否分析性能 */
analyzePerformance: boolean;
/** 是否分析代码质量 */
analyzeCodeQuality: boolean;
/** 是否生成报告 */
generateReport: boolean;
/** 输出目录 */
outputDir: string;
}
interface HealthCheck {
score: number;
issues: string[];
suggestions: string[];
metrics: {
bundleSize: number;
buildTime: number;
dependencyCount: number;
unusedCode: number;
};
}
interface AnalysisResult {
projectPath: string;
buildType: string;
stats: BuildStats;
health: HealthCheck;
optimizations: OptimizationResult[];
recommendations: string[];
}
/**
* 简单的分析器类 - 提供基本功能
*/
declare class SimpleAnalyzer {
/**
* 创建空的构建统计
*/
static createEmptyStats(): BuildStats;
/**
* 计算构建时长
*/
static calculateDuration(startTime: number, endTime: number): number;
/**
* 格式化文件大小
*/
static formatSize(bytes: number): string;
}
/**
* 简化的优化器类
*/
declare class SynthiaOptimizer {
private config;
constructor(config?: any);
/**
* 生成优化建议
*/
generateSuggestions(stats: BuildStats): string[];
/**
* 应用基本优化
*/
applyBasicOptimizations(): OptimizationResult;
/**
* 显示优化结果
*/
displayResults(result: OptimizationResult): void;
/**
* 格式化构建时间
*/
static formatDuration(ms: number): string;
/**
* 格式化文件大小
*/
static formatSize(bytes: number): string;
}
declare class SynthiaDoctor {
private projectPath;
private spinner;
constructor(projectPath: string);
/**
* 生成性能报告
*/
generateReport(analysis: {
stats: BuildStats;
dependencies: DependencyAnalysis;
performance: PerformanceAnalysis;
codeQuality: CodeQualityAnalysis;
}, outputPath: string): Promise<void>;
/**
* 项目健康检查
*/
checkHealth(): Promise<HealthCheck>;
/**
* 显示优化建议
*/
displaySuggestions(suggestions: any[]): void;
/**
* 显示健康报告
*/
displayHealthReport(health: HealthCheck): void;
/**
* 开始实时监控
*/
startWatching(): Promise<void>;
/**
* 收集项目指标
*/
private collectMetrics;
/**
* 检测问题
*/
private detectIssues;
/**
* 生成建议
*/
private generateSuggestions;
/**
* 计算健康评分
*/
private calculateHealthScore;
/**
* 格式化报告
*/
private formatReport;
/**
* 生成HTML报告
*/
private generateHTMLReport;
private estimateBundleSize;
private estimateBuildTime;
private estimateUnusedCode;
private formatBytes;
}
/**
* Synthia Doctor 插件 - 函数式插件
*/
interface DoctorPluginOptions {
enabled?: boolean;
rules?: string[];
fix?: boolean;
output?: string;
format?: 'json' | 'table' | 'markdown';
}
/**
* Doctor 插件函数工厂
*/
declare function doctorPlugin(options?: DoctorPluginOptions): PluginFunction;
export { type AnalysisOptions, type AnalysisResult, type CodeQualityAnalysis, type DependencyAnalysis, type DoctorPluginOptions, type HealthCheck, type PerformanceAnalysis, SimpleAnalyzer, SynthiaDoctor, SynthiaOptimizer, doctorPlugin };