info.ts
8.28 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { findWorkspaceRoot } from "../utils/workspace";
/**
* 项目信息
*/
export interface ProjectInfo {
author?: string;
configs: ConfigInfo;
dependencies: DependencyInfo;
description?: string;
devDependencies: DependencyInfo;
inWorkspace: boolean;
license?: string;
name: string;
path: string;
template?: string;
version: string;
workspaceRoot?: string;
}
/**
* 依赖信息
*/
export interface DependencyInfo {
catalogDeps: number;
list?: string[];
regularDeps: number;
total: number;
}
/**
* 配置信息
*/
export interface ConfigInfo {
eslint: boolean;
exists: string[];
git: boolean;
missing: string[];
prettier: boolean;
typeScript: boolean;
vite: boolean;
workspace: boolean;
}
/**
* 获取项目信息
*/
export async function getProjectInfo(projectPath?: string): Promise<ProjectInfo> {
const cwd = projectPath || process.cwd();
const packageJsonPath = path.join(cwd, "package.json");
if (!fs.existsSync(packageJsonPath)) {
throw new Error(`不是有效的项目,package.json 不存在: ${packageJsonPath}`);
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
const workspaceRoot = findWorkspaceRoot(cwd);
// 分析配置文件
const configInfo = analyzeConfigFiles(cwd);
// 分析依赖
const deps = analyzeDependencies(packageJson);
// 检测模板类型
const template = detectTemplate(packageJson);
return {
name: packageJson.name || "unknown",
version: packageJson.version || "0.0.0",
description: packageJson.description,
author: packageJson.author ? String(packageJson.author) : undefined,
license: packageJson.license,
template,
path: cwd,
workspaceRoot: workspaceRoot || undefined,
inWorkspace: !!workspaceRoot,
dependencies: deps.dependencies,
devDependencies: deps.devDependencies,
configs: configInfo,
};
}
/**
* 分析配置文件
*/
function analyzeConfigFiles(projectPath: string): ConfigInfo {
const configFiles = {
typescript: ["tsconfig.json", "tsconfig.ts"],
vite: ["vite.config.ts", "vite.config.mjs", "vite.config.js"],
eslint: [".eslintrc.js", "eslint.config.mjs", ".eslintrc.cjs"],
prettier: [".prettierrc", "prettier.config.mjs", ".prettierrc.json"],
git: [".git"],
workspace: ["pnpm-workspace.yaml"],
};
const exists: string[] = [];
const missing: string[] = [];
// 检查配置文件
for (const [configType, files] of Object.entries(configFiles)) {
const hasFile = files.some((file) => fs.existsSync(path.join(projectPath, file)));
if (hasFile) {
exists.push(configType);
} else {
missing.push(configType);
}
}
return {
typeScript: exists.includes("typescript"),
vite: exists.includes("vite"),
eslint: exists.includes("eslint"),
prettier: exists.includes("prettier"),
git: exists.includes("git"),
workspace: exists.includes("workspace"),
exists,
missing,
};
}
/**
* 分析依赖
*/
function analyzeDependencies(
packageJson: any,
): { dependencies: DependencyInfo; devDependencies: DependencyInfo } {
const countDeps = (deps: Record<string, string>) => {
const list = Object.keys(deps);
const catalogCount = Object.values(deps).filter((dep) => dep === "catalog:").length;
return {
total: list.length,
catalogDeps: catalogCount,
regularDeps: list.length - catalogCount,
list,
};
};
const dependencies = countDeps(packageJson.dependencies || {});
const devDependencies = countDeps(packageJson.devDependencies || {});
return { dependencies, devDependencies };
}
/**
* 检测模板类型
*/
function detectTemplate(packageJson: any): string {
const deps = packageJson.dependencies || {};
// 检查是否是 React 项目
if (deps.react || deps["react-dom"]) {
if (deps.antd) {
return "React + Ant Design";
}
return "React";
}
// 检查是否是 Vue 项目
if (deps.vue) {
if (deps["element-plus"]) {
return "Vue + Element Plus";
}
if (deps["ant-design-vue"]) {
return "Vue + Ant Design Vue";
}
return "Vue";
}
// 检查其他特征
if (deps.uniapp) {
return "UniApp";
}
return "未知";
}
/**
* 格式化项目信息
*/
export function formatProjectInfo(info: ProjectInfo): string {
const lines: string[] = [];
lines.push("📋 项目信息\n", `名称: ${info.name}`, `版本: ${info.version}`);
if (info.description) {
lines.push(`描述: ${info.description}`);
}
if (info.author) {
lines.push(`作者: ${info.author}`);
}
if (info.license) {
lines.push(`许可证: ${info.license}`);
}
if (info.template) {
lines.push(`模板: ${info.template}`);
}
lines.push(`路径: ${info.path}`, `Workspace: ${info.inWorkspace ? "是" : "否"}`);
if (info.workspaceRoot) {
lines.push(`Workspace 根目录: ${info.workspaceRoot}`);
}
// 依赖信息
lines.push("\n📦 依赖信息", `生产依赖: ${info.dependencies.total}`, ` - Catalog: ${info.dependencies.catalogDeps}`, ` - 常规: ${info.dependencies.regularDeps}`, `开发依赖: ${info.devDependencies.total}`, ` - Catalog: ${info.devDependencies.catalogDeps}`, ` - 常规: ${info.devDependencies.regularDeps}`, "\n⚙️ 配置信息", `TypeScript: ${info.configs.typeScript ? "✓" : "✗"}`, `Vite: ${info.configs.vite ? "✓" : "✗"}`, `ESLint: ${info.configs.eslint ? "✓" : "✗"}`, `Prettier: ${info.configs.prettier ? "✓" : "✗"}`, `Git: ${info.configs.git ? "✓" : "✗"}`, `Workspace: ${info.configs.workspace ? "✓" : "✗"}`);
return lines.join("\n");
}
/**
* 显示依赖列表
*/
export function showDependencyList(info: ProjectInfo): string {
const lines: string[] = [];
lines.push("📦 依赖详情\n");
if (info.dependencies.list && info.dependencies.list.length > 0) {
lines.push("生产依赖:");
info.dependencies.list.forEach((dep) => {
lines.push(` - ${dep}`);
});
}
if (info.devDependencies.list && info.devDependencies.list.length > 0) {
lines.push("\n开发依赖:");
info.devDependencies.list.forEach((dep) => {
lines.push(` - ${dep}`);
});
}
return lines.join("\n");
}
/**
* 比较两个项目的依赖差异
*/
export function compareDependencies(
projectA: ProjectInfo,
projectB: ProjectInfo,
): string {
const lines: string[] = [];
lines.push("📊 依赖对比\n", `项目 A: ${projectA.name}`, `项目 B: ${projectB.name}`);
// 对比生产依赖
const depsA = new Set(projectA.dependencies.list || []);
const depsB = new Set(projectB.dependencies.list || []);
const onlyInA = [...depsA].filter((dep) => !depsB.has(dep));
const onlyInB = [...depsB].filter((dep) => !depsA.has(dep));
const common = [...depsA].filter((dep) => depsB.has(dep));
lines.push(`\n仅在项目 A 中 (${onlyInA.length}):`);
if (onlyInA.length > 0) {
onlyInA.forEach((dep) => lines.push(` - ${dep}`));
} else {
lines.push(" 无");
}
lines.push(`\n仅在项目 B 中 (${onlyInB.length}):`);
if (onlyInB.length > 0) {
onlyInB.forEach((dep) => lines.push(` - ${dep}`));
} else {
lines.push(" 无");
}
lines.push(`\n共同依赖 (${common.length}):`);
if (common.length > 0) {
common.forEach((dep) => lines.push(` - ${dep}`));
} else {
lines.push(" 无");
}
return lines.join("\n");
}
/**
* 生成 JSON 格式的项目信息
*/
export function toJson(info: ProjectInfo): string {
return JSON.stringify(
{
name: info.name,
version: info.version,
description: info.description,
template: info.template,
dependencies: {
production: info.dependencies.total,
development: info.devDependencies.total,
},
configs: info.configs.exists,
inWorkspace: info.inWorkspace,
},
null,
2,
);
}
/**
* 验证项目配置
*/
export function validateProjectConfig(info: ProjectInfo): boolean {
// 检查必需配置
const requiredConfigs = ["eslint", "prettier"];
const missingRequired = requiredConfigs.filter((config) => !info.configs.exists.includes(config));
if (missingRequired.length > 0) {
console.log(`❌ 缺少必需配置: ${missingRequired.join(", ")}`);
return false;
}
// 检查依赖
if (info.dependencies.total === 0 && info.devDependencies.total === 0) {
console.log("❌ 项目没有定义任何依赖");
return false;
}
// 检查 TypeScript(推荐)
if (!info.configs.typeScript) {
console.log("⚠️ 建议启用 TypeScript");
}
// 检查 Git(推荐)
if (!info.configs.git) {
console.log("⚠️ 建议初始化 Git");
}
return true;
}