code-simplifier 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +302 -0
- package/bin/code-simplifier.js +216 -0
- package/lib/auto-update.js +256 -0
- package/lib/config.js +258 -0
- package/lib/improvement.js +298 -0
- package/lib/knowledge-base.js +254 -0
- package/lib/master.js +359 -0
- package/lib/quality-analyzer.js +327 -0
- package/lib/quality-monitor.js +397 -0
- package/lib/report-generator.js +254 -0
- package/package.json +61 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Code-Simplifier 持续改进
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs-extra')
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const chalk = require('chalk')
|
|
10
|
+
const ora = require('ora')
|
|
11
|
+
|
|
12
|
+
class ContinuousImprovement {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.historyDir = 'quality-history'
|
|
15
|
+
this.reportsDir = 'improvement-reports'
|
|
16
|
+
this.thresholds = {
|
|
17
|
+
qualityScore: 70,
|
|
18
|
+
complexity: 15,
|
|
19
|
+
fileSize: 500,
|
|
20
|
+
debtHours: 10
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 运行每日检查
|
|
26
|
+
*/
|
|
27
|
+
async runDailyCheck() {
|
|
28
|
+
console.log(chalk.blue.bold('\n🔍 开始每日质量检查...'))
|
|
29
|
+
console.log(chalk.gray('='.repeat(60)))
|
|
30
|
+
|
|
31
|
+
const timestamp = new Date().toISOString()
|
|
32
|
+
const date = timestamp.split('T')[0]
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
// 1. 运行代码分析
|
|
36
|
+
const analysisResult = await this.runAnalysis()
|
|
37
|
+
if (!analysisResult) {
|
|
38
|
+
console.error(chalk.red('❌ 分析失败'))
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 2. 保存历史数据
|
|
43
|
+
this.saveHistory(analysisResult)
|
|
44
|
+
|
|
45
|
+
// 3. 分析趋势
|
|
46
|
+
const trends = this.analyzeTrends()
|
|
47
|
+
|
|
48
|
+
// 4. 生成报告
|
|
49
|
+
const report = this.generateReport(analysisResult, trends)
|
|
50
|
+
|
|
51
|
+
// 5. 检查预警
|
|
52
|
+
await this.checkAlerts(analysisResult, trends)
|
|
53
|
+
|
|
54
|
+
console.log(chalk.green('\n✅ 每日检查完成'))
|
|
55
|
+
console.log(chalk.cyan(`📅 日期: ${date}`))
|
|
56
|
+
console.log(chalk.cyan(`📊 质量评分: ${analysisResult.avgScore}/100`))
|
|
57
|
+
console.log(chalk.cyan(`⚠️ 问题数量: ${analysisResult.totalIssues}`))
|
|
58
|
+
console.log(chalk.cyan(`📈 趋势: ${trends.overall}`))
|
|
59
|
+
|
|
60
|
+
} catch (error) {
|
|
61
|
+
console.error(chalk.red('❌ 检查失败:'), error.message)
|
|
62
|
+
throw error
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 运行分析
|
|
68
|
+
*/
|
|
69
|
+
async runAnalysis() {
|
|
70
|
+
// 模拟分析结果
|
|
71
|
+
const score = Math.floor(Math.random() * 30) + 70
|
|
72
|
+
const totalIssues = Math.floor(Math.random() * 20)
|
|
73
|
+
const debtHours = Math.floor(Math.random() * 30)
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
timestamp: new Date().toISOString(),
|
|
77
|
+
avgScore: score,
|
|
78
|
+
totalIssues: totalIssues,
|
|
79
|
+
debtHours: debtHours,
|
|
80
|
+
reportPath: `quality-report-${Date.now()}.md`
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 保存历史数据
|
|
86
|
+
*/
|
|
87
|
+
saveHistory(result) {
|
|
88
|
+
const date = result.timestamp.split('T')[0]
|
|
89
|
+
const historyFile = path.join(this.historyDir, `${date}.json`)
|
|
90
|
+
|
|
91
|
+
const history = {
|
|
92
|
+
date: date,
|
|
93
|
+
timestamp: result.timestamp,
|
|
94
|
+
qualityScore: result.avgScore,
|
|
95
|
+
totalIssues: result.totalIssues,
|
|
96
|
+
debtHours: result.debtHours,
|
|
97
|
+
reportPath: result.reportPath
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
fs.ensureDirSync(this.historyDir)
|
|
101
|
+
fs.writeJsonSync(historyFile, history, { spaces: 2 })
|
|
102
|
+
console.log(chalk.gray(`📁 历史数据已保存: ${historyFile}`))
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* 分析趋势
|
|
107
|
+
*/
|
|
108
|
+
analyzeTrends() {
|
|
109
|
+
if (!fs.existsSync(this.historyDir)) {
|
|
110
|
+
return {
|
|
111
|
+
overall: 'stable',
|
|
112
|
+
score: 'stable',
|
|
113
|
+
issues: 'stable',
|
|
114
|
+
debt: 'stable'
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const historyFiles = fs.readdirSync(this.historyDir)
|
|
119
|
+
.filter(f => f.endsWith('.json'))
|
|
120
|
+
.sort()
|
|
121
|
+
|
|
122
|
+
if (historyFiles.length < 2) {
|
|
123
|
+
return {
|
|
124
|
+
overall: 'stable',
|
|
125
|
+
score: 'stable',
|
|
126
|
+
issues: 'stable',
|
|
127
|
+
debt: 'stable'
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 读取最近7天的数据
|
|
132
|
+
const recentData = historyFiles.slice(-7).map(f => {
|
|
133
|
+
const content = fs.readJsonSync(path.join(this.historyDir, f))
|
|
134
|
+
return content
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
// 计算趋势
|
|
138
|
+
const scoreTrend = this.calculateTrend(recentData.map(d => d.qualityScore))
|
|
139
|
+
const issuesTrend = this.calculateTrend(recentData.map(d => d.totalIssues))
|
|
140
|
+
const debtTrend = this.calculateTrend(recentData.map(d => d.debtHours))
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
overall: scoreTrend > 5 ? 'improving' : scoreTrend < -5 ? 'declining' : 'stable',
|
|
144
|
+
score: scoreTrend > 0 ? 'up' : scoreTrend < 0 ? 'down' : 'stable',
|
|
145
|
+
issues: issuesTrend > 0 ? 'up' : issuesTrend < 0 ? 'down' : 'stable',
|
|
146
|
+
debt: debtTrend > 0 ? 'up' : debtTrend < 0 ? 'down' : 'stable',
|
|
147
|
+
scoreChange: scoreTrend.toFixed(1),
|
|
148
|
+
recentData: recentData
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 计算趋势
|
|
154
|
+
*/
|
|
155
|
+
calculateTrend(values) {
|
|
156
|
+
if (values.length < 2) return 0
|
|
157
|
+
const first = values[0]
|
|
158
|
+
const last = values[values.length - 1]
|
|
159
|
+
return ((last - first) / first) * 100
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 生成报告
|
|
164
|
+
*/
|
|
165
|
+
generateReport(result, trends) {
|
|
166
|
+
const date = result.timestamp.split('T')[0]
|
|
167
|
+
const reportFile = path.join(this.reportsDir, `daily-${date}.md`)
|
|
168
|
+
|
|
169
|
+
const report = `# 每日质量报告 - ${date}
|
|
170
|
+
|
|
171
|
+
## 概览
|
|
172
|
+
|
|
173
|
+
- **检查时间**: ${new Date().toLocaleString()}
|
|
174
|
+
- **质量评分**: ${result.avgScore}/100
|
|
175
|
+
- **问题总数**: ${result.totalIssues}
|
|
176
|
+
- **技术债务**: ${result.debtHours}小时
|
|
177
|
+
|
|
178
|
+
## 趋势分析
|
|
179
|
+
|
|
180
|
+
### 最近7天趋势
|
|
181
|
+
|
|
182
|
+
| 日期 | 质量评分 | 问题数 | 技术债务 |
|
|
183
|
+
|------|----------|--------|----------|
|
|
184
|
+
${trends.recentData.map(d => `| ${d.date} | ${d.qualityScore} | ${d.totalIssues} | ${d.debtHours}h |`).join('\n')}
|
|
185
|
+
|
|
186
|
+
### 趋势评估
|
|
187
|
+
|
|
188
|
+
- **整体趋势**: ${trends.overall}
|
|
189
|
+
- **评分变化**: ${trends.scoreChange}% (${trends.score})
|
|
190
|
+
- **问题趋势**: ${trends.issues}
|
|
191
|
+
- **债务趋势**: ${trends.debt}
|
|
192
|
+
|
|
193
|
+
## 预警状态
|
|
194
|
+
|
|
195
|
+
${this.getAlertStatus(result, trends)}
|
|
196
|
+
|
|
197
|
+
## 改进建议
|
|
198
|
+
|
|
199
|
+
${this.getRecommendations(result, trends)}
|
|
200
|
+
|
|
201
|
+
## 详细报告
|
|
202
|
+
|
|
203
|
+
- 分析报告: ${result.reportPath}
|
|
204
|
+
- 历史数据: ${this.historyDir}/${date}.json
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
*自动生成于 ${new Date().toISOString()}*
|
|
209
|
+
`
|
|
210
|
+
|
|
211
|
+
fs.ensureDirSync(this.reportsDir)
|
|
212
|
+
fs.writeFileSync(reportFile, report, 'utf8')
|
|
213
|
+
console.log(chalk.gray(`📋 报告已生成: ${reportFile}`))
|
|
214
|
+
|
|
215
|
+
return reportFile
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 获取预警状态
|
|
220
|
+
*/
|
|
221
|
+
getAlertStatus(result, trends) {
|
|
222
|
+
const alerts = []
|
|
223
|
+
|
|
224
|
+
if (result.avgScore < this.thresholds.qualityScore) {
|
|
225
|
+
alerts.push(`⚠️ 质量评分低于阈值 (${result.avgScore} < ${this.thresholds.qualityScore})`)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (trends.score === 'down') {
|
|
229
|
+
alerts.push('📉 质量评分呈下降趋势')
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (trends.overall === 'declining') {
|
|
233
|
+
alerts.push('🔴 整体质量呈下降趋势')
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (alerts.length === 0) {
|
|
237
|
+
return '✅ 无预警'
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return alerts.join('\n')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 获取改进建议
|
|
245
|
+
*/
|
|
246
|
+
getRecommendations(result, trends) {
|
|
247
|
+
const recommendations = []
|
|
248
|
+
|
|
249
|
+
if (result.avgScore < this.thresholds.qualityScore) {
|
|
250
|
+
recommendations.push('1. **立即处理高优先级问题**')
|
|
251
|
+
recommendations.push(' - 运行: code-simplifier quality')
|
|
252
|
+
recommendations.push(' - 查看详细报告')
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (trends.overall === 'declining') {
|
|
256
|
+
recommendations.push('2. **分析下降原因**')
|
|
257
|
+
recommendations.push(' - 检查最近的代码变更')
|
|
258
|
+
recommendations.push(' - 审查代码质量门禁')
|
|
259
|
+
recommendations.push(' - 加强代码审查')
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (result.debtHours > this.thresholds.debtHours) {
|
|
263
|
+
recommendations.push('3. **技术债务处理**')
|
|
264
|
+
recommendations.push(' - 优先处理高债务模块')
|
|
265
|
+
recommendations.push(' - 制定重构计划')
|
|
266
|
+
recommendations.push(' - 分配专门时间处理')
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
recommendations.push('4. **持续改进**')
|
|
270
|
+
recommendations.push(' - 每日运行质量检查')
|
|
271
|
+
recommendations.push(' - 定期团队回顾')
|
|
272
|
+
recommendations.push(' - 持续学习和改进')
|
|
273
|
+
|
|
274
|
+
if (recommendations.length === 0) {
|
|
275
|
+
return '✅ 质量良好,继续保持!'
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return recommendations.join('\n')
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 检查预警
|
|
283
|
+
*/
|
|
284
|
+
async checkAlerts(result, trends) {
|
|
285
|
+
if (result.avgScore < this.thresholds.qualityScore) {
|
|
286
|
+
console.log(chalk.yellow('\n⚠️ 质量预警!'))
|
|
287
|
+
console.log(chalk.gray(` 当前评分: ${result.avgScore}/100 (阈值: ${this.thresholds.qualityScore})`))
|
|
288
|
+
console.log(chalk.gray(' 请及时处理!'))
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (trends.overall === 'declining') {
|
|
292
|
+
console.log(chalk.yellow('\n📉 趋势预警!'))
|
|
293
|
+
console.log(chalk.gray(' 质量呈下降趋势,请分析原因!'))
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
module.exports = new ContinuousImprovement()
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Code-Simplifier 知识库管理
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const fs = require('fs-extra')
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const chalk = require('chalk')
|
|
10
|
+
const inquirer = require('inquirer')
|
|
11
|
+
|
|
12
|
+
class KnowledgeBase {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.dataDir = '.code-simplifier'
|
|
15
|
+
this.dbPath = path.join(this.dataDir, 'knowledge-base.json')
|
|
16
|
+
this.data = {
|
|
17
|
+
successCases: [],
|
|
18
|
+
failureCases: [],
|
|
19
|
+
bestPractices: [],
|
|
20
|
+
lessons: []
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 运行知识库管理
|
|
26
|
+
*/
|
|
27
|
+
async run(options = {}) {
|
|
28
|
+
await this.load()
|
|
29
|
+
|
|
30
|
+
if (options.add) {
|
|
31
|
+
await this.addCase(options.add)
|
|
32
|
+
} else if (options.search) {
|
|
33
|
+
await this.search(options.search)
|
|
34
|
+
} else if (options.stats) {
|
|
35
|
+
await this.showStats()
|
|
36
|
+
} else if (options.export) {
|
|
37
|
+
await this.export(options.export)
|
|
38
|
+
} else {
|
|
39
|
+
await this.showStats()
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 加载知识库
|
|
45
|
+
*/
|
|
46
|
+
async load() {
|
|
47
|
+
await fs.ensureDir(this.dataDir)
|
|
48
|
+
if (await fs.pathExists(this.dbPath)) {
|
|
49
|
+
try {
|
|
50
|
+
this.data = await fs.readJson(this.dbPath)
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.warn(chalk.yellow('⚠️ 知识库文件损坏,使用空库'))
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 保存知识库
|
|
59
|
+
*/
|
|
60
|
+
async save() {
|
|
61
|
+
await fs.writeJson(this.dbPath, this.data, { spaces: 2 })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 添加案例
|
|
66
|
+
*/
|
|
67
|
+
async addCase(type) {
|
|
68
|
+
console.log(chalk.cyan(`\n➕ 添加${type}案例\n`))
|
|
69
|
+
|
|
70
|
+
const questions = [
|
|
71
|
+
{
|
|
72
|
+
type: 'input',
|
|
73
|
+
name: 'title',
|
|
74
|
+
message: '标题:',
|
|
75
|
+
validate: val => val.length > 0 || '请输入标题'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
type: 'input',
|
|
79
|
+
name: 'description',
|
|
80
|
+
message: '描述:',
|
|
81
|
+
validate: val => val.length > 0 || '请输入描述'
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
type: 'input',
|
|
85
|
+
name: 'problem',
|
|
86
|
+
message: '问题:'
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
type: 'input',
|
|
90
|
+
name: 'solution',
|
|
91
|
+
message: '解决方案:',
|
|
92
|
+
when: () => type === 'success' || type === 'practice'
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: 'input',
|
|
96
|
+
name: 'tags',
|
|
97
|
+
message: '标签 (逗号分隔):',
|
|
98
|
+
filter: val => val.split(',').map(t => t.trim()).filter(Boolean)
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
const answers = await inquirer.prompt(questions)
|
|
103
|
+
|
|
104
|
+
const caseItem = {
|
|
105
|
+
id: Date.now(),
|
|
106
|
+
title: answers.title,
|
|
107
|
+
description: answers.description,
|
|
108
|
+
problem: answers.problem,
|
|
109
|
+
solution: answers.solution || '',
|
|
110
|
+
tags: answers.tags || [],
|
|
111
|
+
createdAt: new Date().toISOString()
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (type === 'success') {
|
|
115
|
+
this.data.successCases.push(caseItem)
|
|
116
|
+
} else if (type === 'failure') {
|
|
117
|
+
this.data.failureCases.push(caseItem)
|
|
118
|
+
} else if (type === 'practice') {
|
|
119
|
+
this.data.bestPractices.push(caseItem)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
await this.save()
|
|
123
|
+
console.log(chalk.green(`\n✅ ${type}案例已添加`))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 搜索案例
|
|
128
|
+
*/
|
|
129
|
+
async search(keyword) {
|
|
130
|
+
console.log(chalk.cyan(`\n🔍 搜索: "${keyword}"\n`))
|
|
131
|
+
|
|
132
|
+
const results = {
|
|
133
|
+
success: this.searchInArray(this.data.successCases, keyword),
|
|
134
|
+
failure: this.searchInArray(this.data.failureCases, keyword),
|
|
135
|
+
practices: this.searchInArray(this.data.bestPractices, keyword)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let total = 0
|
|
139
|
+
for (const [type, items] of Object.entries(results)) {
|
|
140
|
+
if (items.length > 0) {
|
|
141
|
+
total += items.length
|
|
142
|
+
console.log(chalk.yellow(`${type} (${items.length}):`))
|
|
143
|
+
items.forEach(item => {
|
|
144
|
+
console.log(chalk.gray(` - ${item.title}`))
|
|
145
|
+
})
|
|
146
|
+
console.log('')
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (total === 0) {
|
|
151
|
+
console.log(chalk.gray('未找到相关案例'))
|
|
152
|
+
} else {
|
|
153
|
+
console.log(chalk.green(`\n找到 ${total} 个相关案例`))
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* 在数组中搜索
|
|
159
|
+
*/
|
|
160
|
+
searchInArray(arr, keyword) {
|
|
161
|
+
const lower = keyword.toLowerCase()
|
|
162
|
+
return arr.filter(item =>
|
|
163
|
+
item.title.toLowerCase().includes(lower) ||
|
|
164
|
+
item.description.toLowerCase().includes(lower) ||
|
|
165
|
+
item.tags.some(tag => tag.toLowerCase().includes(lower))
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 显示统计
|
|
171
|
+
*/
|
|
172
|
+
async showStats() {
|
|
173
|
+
const stats = this.getStats()
|
|
174
|
+
|
|
175
|
+
console.log(chalk.cyan.bold('\n📚 知识库统计'))
|
|
176
|
+
console.log(chalk.gray('='.repeat(50)))
|
|
177
|
+
console.log(chalk.green(` 成功案例: ${stats.success}`))
|
|
178
|
+
console.log(chalk.red(` 失败案例: ${stats.failure}`))
|
|
179
|
+
console.log(chalk.blue(` 最佳实践: ${stats.practices}`))
|
|
180
|
+
console.log(chalk.yellow(` 经验教训: ${stats.lessons}`))
|
|
181
|
+
console.log(chalk.cyan(`\n 总计: ${stats.total}`))
|
|
182
|
+
console.log('')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 获取统计信息
|
|
187
|
+
*/
|
|
188
|
+
getStats() {
|
|
189
|
+
return {
|
|
190
|
+
success: this.data.successCases.length,
|
|
191
|
+
failure: this.data.failureCases.length,
|
|
192
|
+
practices: this.data.bestPractices.length,
|
|
193
|
+
lessons: this.data.lessons.length,
|
|
194
|
+
total: this.data.successCases.length + this.data.failureCases.length + this.data.bestPractices.length + this.data.lessons.length
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 导出知识库
|
|
200
|
+
*/
|
|
201
|
+
async export(filePath) {
|
|
202
|
+
console.log(chalk.cyan(`\n📤 导出知识库到: ${filePath}\n`))
|
|
203
|
+
|
|
204
|
+
const content = JSON.stringify(this.data, null, 2)
|
|
205
|
+
await fs.writeFile(filePath, content)
|
|
206
|
+
|
|
207
|
+
console.log(chalk.green('✅ 导出完成'))
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 添加最佳实践
|
|
212
|
+
*/
|
|
213
|
+
async addBestPractice(practice) {
|
|
214
|
+
this.data.bestPractices.push({
|
|
215
|
+
...practice,
|
|
216
|
+
id: Date.now(),
|
|
217
|
+
createdAt: new Date().toISOString()
|
|
218
|
+
})
|
|
219
|
+
await this.save()
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 从分析结果更新知识库
|
|
224
|
+
*/
|
|
225
|
+
async updateFromAnalysis() {
|
|
226
|
+
// 这里可以添加基于质量分析的自动更新逻辑
|
|
227
|
+
// 例如:低分代码自动记录为改进案例
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 获取所有案例
|
|
232
|
+
*/
|
|
233
|
+
getAllCases(type) {
|
|
234
|
+
if (type === 'success') return this.data.successCases
|
|
235
|
+
if (type === 'failure') return this.data.failureCases
|
|
236
|
+
return []
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 获取所有最佳实践
|
|
241
|
+
*/
|
|
242
|
+
getAllBestPractices() {
|
|
243
|
+
return this.data.bestPractices
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 获取所有经验教训
|
|
248
|
+
*/
|
|
249
|
+
getAllLessons() {
|
|
250
|
+
return this.data.lessons
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
module.exports = new KnowledgeBase()
|