preview-project.mjs
3.73 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
// ============================================================================
// Preview Project Module - 预览项目管理
// 职责:项目列表、文件扫描、bundle 管理
// ============================================================================
import fs from "node:fs";
import path from "node:path";
import { PREVIEW_DIR } from "./config.mjs";
/**
* 列出所有预览项目
* @param {string} repoRoot - 仓库根目录
* @returns {Array<{name: string, packageName: string, path: string}>}
*/
export function listPreviewProjects(repoRoot) {
const dir = path.join(repoRoot, PREVIEW_DIR);
if (!fs.existsSync(dir)) return [];
return fs.readdirSync(dir, { withFileTypes: true })
.filter((e) => e.isDirectory())
.map((e) => {
const pkgPath = path.join(dir, e.name, "package.json");
let packageName = `@venus/${e.name}`;
if (fs.existsSync(pkgPath)) {
try {
const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
packageName = pkg.name || packageName;
} catch {
// ignore
}
}
return {
name: e.name,
packageName,
path: path.join(dir, e.name),
};
});
}
/**
* 列出项目中的生成文件
* @param {string} projectRoot - 项目根目录
* @returns {Array<{path: string, fullPath: string}>}
*/
export function listGeneratedFiles(projectRoot) {
if (!projectRoot || !fs.existsSync(projectRoot)) return [];
const viewsDir = path.join(projectRoot, "src", "views");
if (!fs.existsSync(viewsDir)) return [];
const results = [];
walkViews(viewsDir, "", results);
return results;
}
/**
* 递归扫描 views 目录
*/
function walkViews(dir, prefix, results) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const relPath = prefix ? `${prefix}/${entry.name}` : entry.name;
if (entry.isDirectory()) {
walkViews(path.join(dir, entry.name), relPath, results);
} else if (/\.(?:vue|ts|tsx|css|scss)$/.test(entry.name)) {
results.push({
path: `src/views/${relPath}`,
fullPath: path.join(dir, entry.name),
});
}
}
}
/**
* 列出所有 bundle 页面
* @param {string} repoRoot - 仓库根目录
* @returns {Array<{slug: string, bundleFile: string}>}
*/
export function listBundlePages(repoRoot) {
const previewDir = path.join(repoRoot, PREVIEW_DIR);
if (!fs.existsSync(previewDir)) return [];
const results = [];
const projects = fs.readdirSync(previewDir, { withFileTypes: true })
.filter((e) => e.isDirectory());
for (const project of projects) {
const aiDir = path.join(previewDir, project.name, ".ai-direct");
if (!fs.existsSync(aiDir)) continue;
const pages = fs.readdirSync(aiDir, { withFileTypes: true })
.filter((e) => e.isDirectory());
for (const page of pages) {
const pageDir = path.join(aiDir, page.name);
const files = fs.readdirSync(pageDir);
const bundle = files.find((f) => f.endsWith(".ai-direct-context-bundle.json"));
if (bundle) {
results.push({
slug: `${project.name}/${page.name}`,
bundleFile: path.join(pageDir, bundle),
});
}
}
}
return results;
}
/**
* 根据配置查找目标项目
* @param {string} repoRoot - 仓库根目录
* @param {Object} config - 配置对象
* @returns {Object|null} - 匹配的项目
*/
export function findTargetProject(repoRoot, config) {
const projects = listPreviewProjects(repoRoot);
let target = null;
if (config.existingProject) {
target = projects.find((p) => p.name === config.existingProject);
}
if (!target && config.pageName) {
target = projects.find((p) => p.name.includes(config.pageName.toLowerCase()));
}
if (!target && projects.length > 0) {
target = projects.sort((a, b) => {
const aStat = fs.statSync(a.path);
const bStat = fs.statSync(b.path);
return bStat.mtimeMs - aStat.mtimeMs;
})[0];
}
return target;
}