diagnose.ts
5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import child_process from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { formatDiagnostics, runDiagnostics } from "../utils/diagnostics";
/**
* 诊断命令选项
*/
export interface DiagnoseOptions {
fix?: boolean;
projectPath?: string;
verbose?: boolean;
}
/**
* 运行项目诊断
*/
export async function runDiagnose(options: DiagnoseOptions = {}): Promise<number> {
const { projectPath = process.cwd(), verbose, fix } = options;
// 验证项目路径
if (!fs.existsSync(projectPath)) {
console.error(`✗ 项目路径不存在: ${projectPath}`);
return 1;
}
const packageJsonPath = path.join(projectPath, "package.json");
if (!fs.existsSync(packageJsonPath)) {
console.error(`✗ 不是有效的 Venus 项目,package.json 不存在`);
return 1;
}
// 显示诊断开始
console.log("\n🔍 开始诊断项目...\n");
try {
// 运行诊断
const result = runDiagnostics(projectPath);
// 显示结果
console.log(formatDiagnostics(result));
// 详细模式
if (verbose) {
console.log("\n📊 详细信息:");
console.log(`项目路径: ${result.projectPath}`);
console.log(`发现问题数: ${result.findings.length}`);
console.log(`错误数: ${result.findings.filter((f) => f.severity === "error").length}`);
console.log(`警告数: ${result.findings.filter((f) => f.severity === "warning").length}`);
}
// 询问是否修复
if (fix && !result.success) {
console.log("\n🔧 自动修复问题...");
try {
// 调用修复功能
const { runFix } = await import("./fix");
const fixExitCode = await runFix(projectPath, { dependencies: true, config: true });
if (fixExitCode !== 0) {
return fixExitCode;
}
// 重新诊断
console.log("\n🔍 重新诊断...");
const newResult = runDiagnostics(projectPath);
console.log(formatDiagnostics(newResult));
if (newResult.success) {
console.log("\n✅ 所有问题已修复!");
} else {
console.log("\n⚠️ 部分问题需要手动处理");
}
} catch (error) {
console.error(`\n✗ 自动修复失败: ${error instanceof Error ? error.message : String(error)}`);
console.log("请手动处理上述问题");
}
} else if (!result.success && !fix) {
console.log("\n💡 提示: 使用 'venus fix --all' 可以自动修复部分问题");
}
// 返回适当的退出码
return result.success ? 0 : 1;
} catch (error) {
console.error(`\n✗ 诊断失败: ${error instanceof Error ? error.message : String(error)}`);
if (verbose && error instanceof Error) {
console.error(error.stack);
}
return 1;
}
}
/**
* 运行依赖检查
*/
export function runDependencyCheck(projectPath: string): void {
console.log("🔍 检查依赖...");
const packageJsonPath = path.join(projectPath, "package.json");
if (!fs.existsSync(packageJsonPath)) {
console.error("✗ package.json 不存在");
return;
}
try {
// 检查依赖是否正确安装
const nodeModulesPath = path.join(projectPath, "node_modules");
if (!fs.existsSync(nodeModulesPath)) {
console.error("✗ node_modules 不存在,请运行 'pnpm install'");
return;
}
// 检查常见依赖
const commonDeps = ["react", "vue", "typescript", "vite", "eslint"];
for (const dep of commonDeps) {
const depPath = path.join(nodeModulesPath, dep);
if (fs.existsSync(depPath)) {
const stats = fs.lstatSync(depPath);
if (stats.isSymbolicLink()) {
console.log(`✓ ${dep}: 软链接`);
} else {
console.warn(`⚠️ ${dep}: 完整副本`);
}
}
}
console.log("\n💡 如需修复依赖问题,运行: venus fix --dependencies");
} catch (error) {
console.error(`✗ 依赖检查失败: ${error instanceof Error ? error.message : String(error)}`);
}
}
/**
* 运行配置检查
*/
export function runConfigCheck(projectPath: string): void {
console.log("🔍 检查配置...");
const configFiles = [
"package.json",
"tsconfig.json",
"vite.config.ts",
".eslintrc.js",
".prettierrc",
];
let missingFiles = 0;
let existingFiles = 0;
for (const configFile of configFiles) {
const configPath = path.join(projectPath, configFile);
if (fs.existsSync(configPath)) {
console.log(`✓ ${configFile}`);
existingFiles++;
} else {
console.log(`✗ ${configFile} (缺失)`);
missingFiles++;
}
}
console.log(`\n📊 配置文件: ${existingFiles} 个存在, ${missingFiles} 个缺失`);
if (missingFiles > 0) {
console.log("💡 建议补充缺失的配置文件");
}
}
/**
* 运行环境检查
*/
export function runEnvironmentCheck(): void {
console.log("🔍 检查开发环境...\n");
// Node.js 版本
const nodeVersion = process.version;
const nodeRequired = ">=20.10.0";
const nodeVersionOk = nodeVersion >= "v20.10.0";
console.log(`Node.js: ${nodeVersion} ${nodeVersionOk ? "✓" : "✗"} (需要 ${nodeRequired})`);
// pnpm 版本
try {
const pnpmVersion = child_process
.execSync("pnpm --version", { encoding: "utf8" })
.trim();
const pnpmRequired = ">=9.12.0";
// 简化的版本比较
const pnpmVersionOk = pnpmVersion >= "9.12.0";
console.log(`pnpm: ${pnpmVersion} ${pnpmVersionOk ? "✓" : "✗"} (需要 ${pnpmRequired})`);
} catch {
console.log("pnpm: ✗ (未安装或无法获取版本)");
}
// Git
try {
child_process.execSync("git --version", { encoding: "utf8", stdio: ["ignore"] });
console.log("Git: ✓");
} catch {
console.log("Git: ✗ (未安装)");
}
// 检查当前目录
const cwd = process.cwd();
const packageJsonPath = path.join(cwd, "package.json");
if (fs.existsSync(packageJsonPath)) {
console.log(`当前目录: ✓ (Venus 项目)`);
} else {
console.log(`当前目录: ✗ (不是 Venus 项目)`);
}
console.log("\n💡 如需修复环境问题,请按照上述提示操作");
}