@chongyan/autospec 1.0.1
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.en.md +472 -0
- package/README.md +476 -0
- package/bin/autospec.js +3 -0
- package/knowledge/README.md +144 -0
- package/knowledge/checklists/code.md +182 -0
- package/knowledge/checklists/design.md +196 -0
- package/knowledge/checklists/release.md +70 -0
- package/knowledge/checklists/requirement.md +169 -0
- package/knowledge/checklists/test.md +46 -0
- package/knowledge/config/README.en.md +44 -0
- package/knowledge/config/README.md +44 -0
- package/knowledge/config/role-composition.yaml +98 -0
- package/knowledge/config/role-extensions.yaml +140 -0
- package/knowledge/config/skill-compositions.yaml +142 -0
- package/knowledge/config/team-stage.yaml +95 -0
- package/knowledge/config/team-tasks.yaml +139 -0
- package/knowledge/config/team-triggers.yaml +198 -0
- package/knowledge/config/validation-patterns.yaml +137 -0
- package/knowledge/domain/README.md +115 -0
- package/knowledge/domain/flows/README.md +194 -0
- package/knowledge/domain/glossary.md +143 -0
- package/knowledge/domain/rules.md +138 -0
- package/knowledge/environment/README.en.md +36 -0
- package/knowledge/environment/README.md +87 -0
- package/knowledge/environment/component-knowledge.md +316 -0
- package/knowledge/environment/detection-patterns.yaml +502 -0
- package/knowledge/environment/middleware-knowledge.md +237 -0
- package/knowledge/environment/template-registry.md +321 -0
- package/knowledge/guides/domain-driven-design.md +345 -0
- package/knowledge/guides/knowledge-management.md +369 -0
- package/knowledge/guides/requirement-engineering.md +329 -0
- package/knowledge/guides/stages/ai-effect-evaluator.md +93 -0
- package/knowledge/guides/stages/code-implementer.md +205 -0
- package/knowledge/guides/stages/code-reviewer.md +111 -0
- package/knowledge/guides/stages/consistency-checker.md +177 -0
- package/knowledge/guides/stages/design-planner.md +401 -0
- package/knowledge/guides/stages/design-reviewer.md +83 -0
- package/knowledge/guides/stages/integration-test-runner.md +105 -0
- package/knowledge/guides/stages/release-checker.md +205 -0
- package/knowledge/guides/stages/requirement-analyzer.md +195 -0
- package/knowledge/guides/stages/requirement-reviewer.md +83 -0
- package/knowledge/guides/stages/security-reviewer.md +89 -0
- package/knowledge/guides/stages/test-context-analyzer.md +250 -0
- package/knowledge/guides/stages/test-generator.md +241 -0
- package/knowledge/guides/stages/test-planner.md +183 -0
- package/knowledge/guides/stages/test-reviewer.md +76 -0
- package/knowledge/guides/stages/unit-test-runner.md +83 -0
- package/knowledge/guides/support/ai-agent-analyzer.md +362 -0
- package/knowledge/guides/support/ai-anomaly-analyzer.md +213 -0
- package/knowledge/guides/support/ai-artifact-evaluator.md +192 -0
- package/knowledge/guides/support/ai-capability-analyzer.md +193 -0
- package/knowledge/guides/support/ai-component-analyzer.md +169 -0
- package/knowledge/guides/support/ai-data-validator.md +276 -0
- package/knowledge/guides/support/ai-evaluation-planner.md +374 -0
- package/knowledge/guides/support/ai-path-evaluator.md +274 -0
- package/knowledge/guides/support/ai-pipeline-evaluator.md +219 -0
- package/knowledge/guides/support/ai-rag-analyzer.md +339 -0
- package/knowledge/guides/support/ai-task-assessor.md +418 -0
- package/knowledge/guides/support/ai-test-diagnostics.md +133 -0
- package/knowledge/guides/support/complexity-assessor.md +268 -0
- package/knowledge/guides/support/component-discovery.md +183 -0
- package/knowledge/guides/support/environment-scanner.md +207 -0
- package/knowledge/guides/support/environment-validator.md +207 -0
- package/knowledge/guides/support/knowledge-generator.md +234 -0
- package/knowledge/guides/support/methodology-extractor.md +55 -0
- package/knowledge/guides/support/pipeline-protocol.md +438 -0
- package/knowledge/guides/support/practice-logger.md +359 -0
- package/knowledge/guides/support/scope-inference.md +174 -0
- package/knowledge/guides/support/skill-distiller.md +91 -0
- package/knowledge/guides/support/skill-updater.md +45 -0
- package/knowledge/guides/support/skill-validator.md +72 -0
- package/knowledge/guides/support/team-orchestrator.md +323 -0
- package/knowledge/guides/support/tech-stack-analyzer.md +139 -0
- package/knowledge/guides/support/test-runner.md +254 -0
- package/knowledge/guides/system-design.md +352 -0
- package/knowledge/organization/ai-native-team.md +318 -0
- package/knowledge/organization/team-metrics.md +228 -0
- package/knowledge/principles/constitution.md +134 -0
- package/knowledge/principles/core-principles.md +368 -0
- package/knowledge/principles/design-philosophy.md +877 -0
- package/knowledge/principles/evolution.md +553 -0
- package/knowledge/process/01-requirement.md +113 -0
- package/knowledge/process/02-design.md +123 -0
- package/knowledge/process/03-implementation.md +90 -0
- package/knowledge/process/04-review.md +80 -0
- package/knowledge/process/05-testing.md +90 -0
- package/knowledge/process/06-delivery.md +88 -0
- package/knowledge/process/README.en.md +38 -0
- package/knowledge/process/README.md +48 -0
- package/knowledge/process/ai-sdlc.md +475 -0
- package/knowledge/process/overview.md +319 -0
- package/knowledge/standards/code-review.md +876 -0
- package/knowledge/standards/coding-style.md +940 -0
- package/knowledge/standards/data-consistency.md +1085 -0
- package/knowledge/standards/document-versioning.md +210 -0
- package/knowledge/standards/risk-detection.md +186 -0
- package/knowledge/templates/ai-evaluation.md +150 -0
- package/knowledge/templates/api-design.md +117 -0
- package/knowledge/templates/database-design.md +132 -0
- package/knowledge/templates/domain-driven-design.md +321 -0
- package/knowledge/templates/product-proposal.md +201 -0
- package/knowledge/templates/system-design.md +227 -0
- package/knowledge/templates/task-breakdown.md +107 -0
- package/knowledge/templates/test-case.md +170 -0
- package/package.json +53 -0
- package/plugins/.claude-plugin/plugin.json +134 -0
- package/plugins/agents/roles/ai-engineer.md +129 -0
- package/plugins/agents/roles/backend-engineer.md +165 -0
- package/plugins/agents/roles/ceo.md +94 -0
- package/plugins/agents/roles/data-engineer.md +135 -0
- package/plugins/agents/roles/devops-engineer.md +181 -0
- package/plugins/agents/roles/frontend-engineer.md +129 -0
- package/plugins/agents/roles/product-owner.md +98 -0
- package/plugins/agents/roles/quality-engineer.md +129 -0
- package/plugins/agents/roles/security-engineer.md +180 -0
- package/plugins/agents/roles/tech-lead.md +97 -0
- package/plugins/agents/support/blind-comparator.md +88 -0
- package/plugins/agents/support/consistency-checker.md +103 -0
- package/plugins/agents/support/failure-diagnostician.md +141 -0
- package/plugins/agents/support/independent-reviewer.md +80 -0
- package/plugins/agents/support/safety-auditor.md +121 -0
- package/plugins/agents/support/skill-benchmarker.md +86 -0
- package/plugins/agents/support/skill-forger.md +105 -0
- package/plugins/agents/support/stage-gate-evaluator.md +121 -0
- package/plugins/agents/support/test-coverage-reviewer.md +73 -0
- package/plugins/benchmarks/templates/README.md +44 -0
- package/plugins/benchmarks/templates/commands/explore-template.yaml +48 -0
- package/plugins/benchmarks/templates/pipeline/agile-template.yaml +84 -0
- package/plugins/benchmarks/templates/pipeline/waterfall-template.yaml +106 -0
- package/plugins/benchmarks/templates/skills/requirement-analyzer-template.yaml +48 -0
- package/plugins/commands/README.en.md +96 -0
- package/plugins/commands/README.md +96 -0
- package/plugins/commands/apply.md +191 -0
- package/plugins/commands/archive.md +76 -0
- package/plugins/commands/env-export.md +79 -0
- package/plugins/commands/env-sync.md +640 -0
- package/plugins/commands/env-template.md +223 -0
- package/plugins/commands/env-update.md +264 -0
- package/plugins/commands/env-validate.md +176 -0
- package/plugins/commands/env.md +79 -0
- package/plugins/commands/explore.md +76 -0
- package/plugins/commands/field-evolve.md +536 -0
- package/plugins/commands/memory.md +249 -0
- package/plugins/commands/project-evolve.md +821 -0
- package/plugins/commands/propose.md +93 -0
- package/plugins/commands/review.md +140 -0
- package/plugins/commands/run.md +224 -0
- package/plugins/commands/status.md +62 -0
- package/plugins/commands/validate.md +108 -0
- package/plugins/hooks/README.en.md +56 -0
- package/plugins/hooks/README.md +56 -0
- package/plugins/hooks/ai-project-guard.js +329 -0
- package/plugins/hooks/artifact-evaluation-hook.js +237 -0
- package/plugins/hooks/constitution-guard.js +211 -0
- package/plugins/hooks/environment-autocommit.js +264 -0
- package/plugins/hooks/environment-manager.js +778 -0
- package/plugins/hooks/execution-tracker.js +354 -0
- package/plugins/hooks/frozen-zone-guard.js +140 -0
- package/plugins/hooks/layer1-validator.js +423 -0
- package/plugins/hooks/lib/artifact-evaluator.js +414 -0
- package/plugins/hooks/lib/benchmarks/change-detector.js +390 -0
- package/plugins/hooks/lib/benchmarks/evaluator.js +605 -0
- package/plugins/hooks/lib/benchmarks/integration-example.js +169 -0
- package/plugins/hooks/lib/data-and-ai-detector.js +275 -0
- package/plugins/hooks/lib/detection-pattern-loader.js +865 -0
- package/plugins/hooks/lib/directory-discovery.js +395 -0
- package/plugins/hooks/lib/environment-config-loader.js +341 -0
- package/plugins/hooks/lib/environment-detector.js +553 -0
- package/plugins/hooks/lib/environment-evolver.js +564 -0
- package/plugins/hooks/lib/environment-registry.js +813 -0
- package/plugins/hooks/lib/execution-path.js +427 -0
- package/plugins/hooks/lib/hook-error-recorder.js +245 -0
- package/plugins/hooks/lib/hook-logger.js +538 -0
- package/plugins/hooks/lib/hook-runner.js +97 -0
- package/plugins/hooks/lib/hook-runner.sh +44 -0
- package/plugins/hooks/lib/hook-state-manager.js +480 -0
- package/plugins/hooks/lib/memory-extractor.js +377 -0
- package/plugins/hooks/lib/memory-manager.js +673 -0
- package/plugins/hooks/lib/metrics-analyzer.js +489 -0
- package/plugins/hooks/lib/project-evolution/auto-fixer.js +511 -0
- package/plugins/hooks/lib/project-evolution/memory-manager.js +346 -0
- package/plugins/hooks/lib/project-evolution/pattern-detector.js +476 -0
- package/plugins/hooks/lib/project-evolution/semantic-indexer.js +480 -0
- package/plugins/hooks/lib/project-structure-detector.js +326 -0
- package/plugins/hooks/lib/rollback-tracker.js +346 -0
- package/plugins/hooks/lib/source-code-scanner.js +596 -0
- package/plugins/hooks/lib/technology-stack-detector.js +374 -0
- package/plugins/hooks/lib/test-failure-analyzer.js +375 -0
- package/plugins/hooks/lib/test-failure-fixer.js +268 -0
- package/plugins/hooks/lib/trace-context.js +277 -0
- package/plugins/hooks/lib/validation-patterns.js +415 -0
- package/plugins/hooks/memory-sync.js +171 -0
- package/plugins/hooks/pipeline-observer.js +413 -0
- package/plugins/hooks/scope-sentinel.js +204 -0
- package/plugins/hooks/trace-initialization.js +169 -0
- package/plugins/memory/templates/code-quality.yaml +149 -0
- package/plugins/memory/templates/multi-system.yaml +155 -0
- package/plugins/memory/templates/team-habits.yaml +119 -0
- package/plugins/memory/templates/testing.yaml +121 -0
- package/plugins/skills/README.en.md +47 -0
- package/plugins/skills/README.md +104 -0
- package/plugins/skills/benchmark-executor/README.md +93 -0
- package/plugins/skills/benchmark-executor/SKILL.md +647 -0
- package/plugins/skills/benchmark-generator/SKILL.md +349 -0
- package/plugins/skills/delivery-stage/SKILL.md +203 -0
- package/plugins/skills/design-stage/SKILL.md +216 -0
- package/plugins/skills/evolution-process/SKILL.md +291 -0
- package/plugins/skills/exploration-phase/SKILL.md +133 -0
- package/plugins/skills/implementation-stage/SKILL.md +179 -0
- package/plugins/skills/layer1-validation/SKILL.md +79 -0
- package/plugins/skills/pending-dashboard/SKILL.md +109 -0
- package/plugins/skills/project-evolution/SKILL.md +847 -0
- package/plugins/skills/requirement-stage/SKILL.md +183 -0
- package/plugins/skills/skill-forge/SKILL.md +223 -0
- package/plugins/skills/skill-forge/references/description-guide.md +92 -0
- package/plugins/skills/skill-forge/references/quality-rubric.md +104 -0
- package/plugins/skills/skill-forge/references/skill-template.md +106 -0
- package/plugins/skills/startup-guard/SKILL.md +38 -0
- package/plugins/skills/testing-stage/SKILL.md +195 -0
- package/scripts/cli/global-init.js +288 -0
- package/scripts/cli/global.js +324 -0
- package/scripts/cli/index.js +55 -0
- package/scripts/cli/init.js +382 -0
- package/scripts/cli/list.js +69 -0
- package/scripts/cli/org.js +340 -0
- package/scripts/cli/update.js +44 -0
- package/scripts/config/commands.config.js +145 -0
- package/scripts/config/hooks.config.js +197 -0
- package/scripts/evolution/evolution-router.js +273 -0
- package/scripts/evolution/evolution-signal-collector.js +307 -0
- package/scripts/evolution/knowledge-loader.js +346 -0
- package/scripts/evolution/marketplace.js +317 -0
- package/scripts/evolution/version-manager.js +371 -0
- package/scripts/install/agents.js +106 -0
- package/scripts/install/commands.js +133 -0
- package/scripts/install/constants.js +424 -0
- package/scripts/install/hook-logger.js +536 -0
- package/scripts/install/hooks.js +110 -0
- package/scripts/install/index.js +39 -0
- package/scripts/install/skills.js +95 -0
- package/scripts/postinstall.js +25 -0
- package/scripts/state.js +376 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Benchmarks Change Detector
|
|
3
|
+
* 改动检测器 - 负责检测阶段产出物变更,触发 benchmarks 重新评测
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const crypto = require('crypto');
|
|
9
|
+
const { EventEmitter } = require('events');
|
|
10
|
+
|
|
11
|
+
class ChangeDetector extends EventEmitter {
|
|
12
|
+
constructor(projectRoot) {
|
|
13
|
+
super();
|
|
14
|
+
this.projectRoot = projectRoot;
|
|
15
|
+
this.statePath = path.join(projectRoot, '.autospec', 'benchmarks', '.state');
|
|
16
|
+
this.watchedPaths = new Map();
|
|
17
|
+
this.debounceTimers = new Map();
|
|
18
|
+
this.isWatching = false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 初始化检测器
|
|
23
|
+
*/
|
|
24
|
+
init() {
|
|
25
|
+
if (!fs.existsSync(this.statePath)) {
|
|
26
|
+
fs.mkdirSync(this.statePath, { recursive: true });
|
|
27
|
+
}
|
|
28
|
+
this.loadState();
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 加载上次状态
|
|
34
|
+
*/
|
|
35
|
+
loadState() {
|
|
36
|
+
const stateFile = path.join(this.statePath, 'file-hashes.json');
|
|
37
|
+
if (fs.existsSync(stateFile)) {
|
|
38
|
+
try {
|
|
39
|
+
const data = JSON.parse(fs.readFileSync(stateFile, 'utf8'));
|
|
40
|
+
this.previousState = data;
|
|
41
|
+
} catch {
|
|
42
|
+
this.previousState = {};
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
this.previousState = {};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 保存当前状态
|
|
51
|
+
*/
|
|
52
|
+
saveState(currentState) {
|
|
53
|
+
const stateFile = path.join(this.statePath, 'file-hashes.json');
|
|
54
|
+
fs.writeFileSync(stateFile, JSON.stringify(currentState, null, 2));
|
|
55
|
+
this.previousState = currentState;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 计算文件哈希
|
|
60
|
+
*/
|
|
61
|
+
calculateHash(filePath) {
|
|
62
|
+
try {
|
|
63
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
64
|
+
return crypto.createHash('sha256').update(content).digest('hex');
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 获取阶段产出物清单
|
|
72
|
+
*/
|
|
73
|
+
getStageArtifacts(stage) {
|
|
74
|
+
const artifactPatterns = {
|
|
75
|
+
exploration: [
|
|
76
|
+
'specs/**/clarifications.md',
|
|
77
|
+
'specs/**/requirement.md'
|
|
78
|
+
],
|
|
79
|
+
requirement: [
|
|
80
|
+
'specs/**/requirement.md',
|
|
81
|
+
'specs/**/user-stories.md',
|
|
82
|
+
'specs/**/assumptions.md'
|
|
83
|
+
],
|
|
84
|
+
design: [
|
|
85
|
+
'specs/**/design.md',
|
|
86
|
+
'specs/**/design/*.md',
|
|
87
|
+
'specs/**/contracts/*.yaml',
|
|
88
|
+
'specs/**/contracts/*.json',
|
|
89
|
+
'specs/**/data-model.md'
|
|
90
|
+
],
|
|
91
|
+
implementation: [
|
|
92
|
+
'src/**/*',
|
|
93
|
+
'lib/**/*',
|
|
94
|
+
'app/**/*',
|
|
95
|
+
'tests/**/*',
|
|
96
|
+
'specs/**/docs/api.md'
|
|
97
|
+
],
|
|
98
|
+
testing: [
|
|
99
|
+
'tests/**/*',
|
|
100
|
+
'coverage/**/*',
|
|
101
|
+
'specs/**/test-reports/**/*'
|
|
102
|
+
]
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return artifactPatterns[stage] || [];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 扫描产出物文件
|
|
110
|
+
*/
|
|
111
|
+
scanArtifacts(patterns) {
|
|
112
|
+
const files = [];
|
|
113
|
+
|
|
114
|
+
for (const pattern of patterns) {
|
|
115
|
+
const matchedFiles = this.globSync(pattern);
|
|
116
|
+
files.push(...matchedFiles);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return [...new Set(files)]; // 去重
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 简单的 glob 实现
|
|
124
|
+
*/
|
|
125
|
+
globSync(pattern) {
|
|
126
|
+
const files = [];
|
|
127
|
+
const regex = this.globToRegex(pattern);
|
|
128
|
+
|
|
129
|
+
const traverse = (dir, relativePath = '') => {
|
|
130
|
+
const fullPath = path.join(this.projectRoot, dir);
|
|
131
|
+
|
|
132
|
+
if (!fs.existsSync(fullPath)) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
|
|
137
|
+
|
|
138
|
+
for (const entry of entries) {
|
|
139
|
+
const entryRelativePath = path.join(relativePath, entry.name);
|
|
140
|
+
const entryFullPath = path.join(fullPath, entry.name);
|
|
141
|
+
|
|
142
|
+
if (entry.isDirectory()) {
|
|
143
|
+
// 跳过特定目录
|
|
144
|
+
if (['node_modules', '.git', 'dist', 'build', '.autospec'].includes(entry.name)) {
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
traverse(entryFullPath, entryRelativePath);
|
|
148
|
+
} else if (entry.isFile()) {
|
|
149
|
+
if (regex.test(entryRelativePath)) {
|
|
150
|
+
files.push(entryRelativePath);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// 确定起始目录
|
|
157
|
+
const baseDir = pattern.split('/')[0];
|
|
158
|
+
if (fs.existsSync(path.join(this.projectRoot, baseDir))) {
|
|
159
|
+
traverse(path.join(this.projectRoot, baseDir), baseDir);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return files;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 将 glob 模式转换为正则表达式
|
|
167
|
+
*/
|
|
168
|
+
globToRegex(pattern) {
|
|
169
|
+
const escaped = pattern
|
|
170
|
+
.replace(/\*\*/g, '<<<DOUBLESTAR>>>')
|
|
171
|
+
.replace(/\*/g, '[^/]*')
|
|
172
|
+
.replace(/<<<DOUBLESTAR>>>/g, '.*')
|
|
173
|
+
.replace(/\?/g, '.')
|
|
174
|
+
.replace(/\./g, '\\.');
|
|
175
|
+
|
|
176
|
+
return new RegExp(`^${escaped}$`);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 检测变更
|
|
181
|
+
*/
|
|
182
|
+
detectChanges(stage) {
|
|
183
|
+
const patterns = this.getStageArtifacts(stage);
|
|
184
|
+
const currentFiles = this.scanArtifacts(patterns);
|
|
185
|
+
const currentState = {};
|
|
186
|
+
const changes = {
|
|
187
|
+
stage,
|
|
188
|
+
timestamp: new Date().toISOString(),
|
|
189
|
+
files_modified: [],
|
|
190
|
+
files_added: [],
|
|
191
|
+
files_deleted: [],
|
|
192
|
+
content_changed: [],
|
|
193
|
+
has_changes: false
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// 计算当前状态
|
|
197
|
+
for (const file of currentFiles) {
|
|
198
|
+
const fullPath = path.join(this.projectRoot, file);
|
|
199
|
+
const hash = this.calculateHash(fullPath);
|
|
200
|
+
if (hash) {
|
|
201
|
+
currentState[file] = hash;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// 对比状态
|
|
206
|
+
const previousFiles = Object.keys(this.previousState);
|
|
207
|
+
const currentFilesSet = new Set(currentFiles);
|
|
208
|
+
|
|
209
|
+
// 检测新增文件
|
|
210
|
+
for (const file of currentFiles) {
|
|
211
|
+
if (!this.previousState[file]) {
|
|
212
|
+
changes.files_added.push(file);
|
|
213
|
+
changes.has_changes = true;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 检测删除文件
|
|
218
|
+
for (const file of previousFiles) {
|
|
219
|
+
if (!currentFilesSet.has(file)) {
|
|
220
|
+
changes.files_deleted.push(file);
|
|
221
|
+
changes.has_changes = true;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// 检测内容变更
|
|
226
|
+
for (const file of currentFiles) {
|
|
227
|
+
if (this.previousState[file] && this.previousState[file] !== currentState[file]) {
|
|
228
|
+
changes.content_changed.push(file);
|
|
229
|
+
changes.files_modified.push(file);
|
|
230
|
+
changes.has_changes = true;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 保存当前状态
|
|
235
|
+
this.saveState(currentState);
|
|
236
|
+
|
|
237
|
+
return changes;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 检测特定文件的变更
|
|
242
|
+
*/
|
|
243
|
+
detectFileChange(filePath) {
|
|
244
|
+
const relativePath = path.relative(this.projectRoot, filePath);
|
|
245
|
+
const currentHash = this.calculateHash(filePath);
|
|
246
|
+
const previousHash = this.previousState[relativePath];
|
|
247
|
+
|
|
248
|
+
if (!previousHash) {
|
|
249
|
+
return { type: 'added', file: relativePath };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (currentHash !== previousHash) {
|
|
253
|
+
return { type: 'modified', file: relativePath };
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 启动文件监听
|
|
261
|
+
*/
|
|
262
|
+
watch(stage, options = {}) {
|
|
263
|
+
if (this.isWatching) {
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const patterns = this.getStageArtifacts(stage);
|
|
268
|
+
const debounceMs = options.debounceMs || 5000;
|
|
269
|
+
|
|
270
|
+
// 使用轮询方式监听(跨平台兼容)
|
|
271
|
+
const pollInterval = options.pollInterval || 2000;
|
|
272
|
+
|
|
273
|
+
this.watchInterval = setInterval(() => {
|
|
274
|
+
const changes = this.detectChanges(stage);
|
|
275
|
+
|
|
276
|
+
if (changes.has_changes) {
|
|
277
|
+
// 防抖处理
|
|
278
|
+
if (this.debounceTimers.has(stage)) {
|
|
279
|
+
clearTimeout(this.debounceTimers.get(stage));
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const timer = setTimeout(() => {
|
|
283
|
+
this.emit('change', changes);
|
|
284
|
+
this.debounceTimers.delete(stage);
|
|
285
|
+
}, debounceMs);
|
|
286
|
+
|
|
287
|
+
this.debounceTimers.set(stage, timer);
|
|
288
|
+
}
|
|
289
|
+
}, pollInterval);
|
|
290
|
+
|
|
291
|
+
this.isWatching = true;
|
|
292
|
+
this.watchedStage = stage;
|
|
293
|
+
|
|
294
|
+
return this;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 停止监听
|
|
299
|
+
*/
|
|
300
|
+
unwatch() {
|
|
301
|
+
if (this.watchInterval) {
|
|
302
|
+
clearInterval(this.watchInterval);
|
|
303
|
+
this.watchInterval = null;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// 清除所有防抖定时器
|
|
307
|
+
for (const timer of this.debounceTimers.values()) {
|
|
308
|
+
clearTimeout(timer);
|
|
309
|
+
}
|
|
310
|
+
this.debounceTimers.clear();
|
|
311
|
+
|
|
312
|
+
this.isWatching = false;
|
|
313
|
+
this.watchedStage = null;
|
|
314
|
+
|
|
315
|
+
return this;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 手动触发检测
|
|
320
|
+
*/
|
|
321
|
+
triggerCheck(stage) {
|
|
322
|
+
const changes = this.detectChanges(stage);
|
|
323
|
+
|
|
324
|
+
if (changes.has_changes) {
|
|
325
|
+
this.emit('change', changes);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return changes;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* 获取文件统计信息
|
|
333
|
+
*/
|
|
334
|
+
getFileStats(filePath) {
|
|
335
|
+
try {
|
|
336
|
+
const stats = fs.statSync(filePath);
|
|
337
|
+
return {
|
|
338
|
+
size: stats.size,
|
|
339
|
+
mtime: stats.mtime.toISOString(),
|
|
340
|
+
ctime: stats.ctime.toISOString()
|
|
341
|
+
};
|
|
342
|
+
} catch {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 生成变更摘要
|
|
349
|
+
*/
|
|
350
|
+
generateChangeSummary(changes) {
|
|
351
|
+
const parts = [];
|
|
352
|
+
|
|
353
|
+
if (changes.files_added.length > 0) {
|
|
354
|
+
parts.push(`新增 ${changes.files_added.length} 个文件`);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (changes.content_changed.length > 0) {
|
|
358
|
+
parts.push(`修改 ${changes.content_changed.length} 个文件`);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (changes.files_deleted.length > 0) {
|
|
362
|
+
parts.push(`删除 ${changes.files_deleted.length} 个文件`);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return parts.join(',') || '无变更';
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* 重置状态
|
|
370
|
+
*/
|
|
371
|
+
reset() {
|
|
372
|
+
this.previousState = {};
|
|
373
|
+
this.saveState({});
|
|
374
|
+
return this;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 获取监听状态
|
|
379
|
+
*/
|
|
380
|
+
getStatus() {
|
|
381
|
+
return {
|
|
382
|
+
isWatching: this.isWatching,
|
|
383
|
+
watchedStage: this.watchedStage,
|
|
384
|
+
watchedFilesCount: Object.keys(this.previousState).length,
|
|
385
|
+
debouncePending: this.debounceTimers.size
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
module.exports = ChangeDetector;
|