1export async function checkProject(projectPath: string): Promise<ProjectInfo> {
2 const result: ProjectInfo = {
3 isValid: false,
4 isVue: false,
5 isVite: false,
6 hasTypeScript: false,
7 packageManager: "npm",
8 errors: [],
9 };
10
11 try {
12 // 检查package.json是否存在
13 const packageJsonPath = path.join(projectPath, "package.json");
14 if (!fs.existsSync(packageJsonPath)) {
15 result.errors?.push(
16 "未找到package.json文件,请确保在Vue项目根目录中运行此工具"
17 );
18 return result;
19 }
20
21 // 读取package.json
22 const packageJson = await fs.readJSON(packageJsonPath);
23
24 // 检查是否为Vue项目
25 if (!packageJson.dependencies?.vue && !packageJson.devDependencies?.vue) {
26 result.errors?.push("未检测到Vue依赖,请确保这是一个Vue项目");
27 } else {
28 result.isVue = true;
29 }
30
31 // 检查是否使用Vite
32 if (!packageJson.devDependencies?.vite) {
33 result.errors?.push("未检测到Vite依赖,此工具仅支持Vite项目");
34 } else {
35 result.isVite = true;
36
37 // 查找vite.config文件
38 const viteConfigPaths = [
39 path.join(projectPath, "vite.config.ts"),
40 path.join(projectPath, "vite.config.js"),
41 ];
42
43 for (const configPath of viteConfigPaths) {
44 if (fs.existsSync(configPath)) {
45 result.viteConfigPath = configPath;
46 break;
47 }
48 }
49
50 if (!result.viteConfigPath) {
51 result.errors?.push("未找到vite.config.ts或vite.config.js文件");
52 }
53 }
54
55 // 检查是否使用TypeScript
56 const tsConfigPath = path.join(projectPath, "tsconfig.json");
57 if (fs.existsSync(tsConfigPath)) {
58 result.hasTypeScript = true;
59 result.tsConfigPath = tsConfigPath;
60 }
61
62 // 检测包管理器
63 if (fs.existsSync(path.join(projectPath, "yarn.lock"))) {
64 result.packageManager = "yarn";
65 } else if (fs.existsSync(path.join(projectPath, "pnpm-lock.yaml"))) {
66 result.packageManager = "pnpm";
67 } else {
68 result.packageManager = "npm";
69 }
70
71 // 判断项目是否有效
72 result.isValid = result.isVue && result.isVite && !!result.viteConfigPath;
73
74 return result;
75 } catch (error) {
76 result.errors?.push(`检查项目结构时出错: ${error}`);
77 return result;
78 }
79}