claude-everything-workflow 0.1.3 → 0.1.5
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/hooks/check-code-size.js +111 -0
- package/package.json +1 -1
- package/rules/02-code-size.md +11 -2
- package/scripts/verify-harness.js +1 -0
- package/settings.json +5 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Check Code Size Hook
|
|
4
|
+
*
|
|
5
|
+
* 编辑后只检查本次改动的代码文件,正常静默,避免增加上下文噪音。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict'
|
|
9
|
+
|
|
10
|
+
const fs = require('fs')
|
|
11
|
+
|
|
12
|
+
const DEFAULT_MAX_LINES = 600
|
|
13
|
+
const TEST_MAX_LINES = 1000
|
|
14
|
+
const CODE_EXTENSIONS = new Set([
|
|
15
|
+
'.c',
|
|
16
|
+
'.cc',
|
|
17
|
+
'.cpp',
|
|
18
|
+
'.cs',
|
|
19
|
+
'.css',
|
|
20
|
+
'.go',
|
|
21
|
+
'.h',
|
|
22
|
+
'.hpp',
|
|
23
|
+
'.html',
|
|
24
|
+
'.java',
|
|
25
|
+
'.js',
|
|
26
|
+
'.jsx',
|
|
27
|
+
'.kt',
|
|
28
|
+
'.lua',
|
|
29
|
+
'.mjs',
|
|
30
|
+
'.php',
|
|
31
|
+
'.py',
|
|
32
|
+
'.rb',
|
|
33
|
+
'.rs',
|
|
34
|
+
'.sh',
|
|
35
|
+
'.swift',
|
|
36
|
+
'.ts',
|
|
37
|
+
'.tsx',
|
|
38
|
+
'.vue'
|
|
39
|
+
])
|
|
40
|
+
|
|
41
|
+
function getExtension(filePath) {
|
|
42
|
+
const match = filePath.match(/(\.[^.\\/]+)$/)
|
|
43
|
+
return match ? match[1].toLowerCase() : ''
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isCodeFile(filePath) {
|
|
47
|
+
return CODE_EXTENSIONS.has(getExtension(filePath))
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isTestFile(filePath) {
|
|
51
|
+
return /(^|[\\/])__tests__([\\/]|$)/.test(filePath) ||
|
|
52
|
+
/\.(test|spec)\.[^.\\/]+$/.test(filePath)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getMaxLines(filePath) {
|
|
56
|
+
return isTestFile(filePath) ? TEST_MAX_LINES : DEFAULT_MAX_LINES
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function countLines(content) {
|
|
60
|
+
if (content.length === 0) return 0
|
|
61
|
+
const lines = content.split(/\r?\n/)
|
|
62
|
+
return content.endsWith('\n') ? lines.length - 1 : lines.length
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function run(raw) {
|
|
66
|
+
let input
|
|
67
|
+
try {
|
|
68
|
+
input = JSON.parse(raw)
|
|
69
|
+
} catch {
|
|
70
|
+
return { exitCode: 0 }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const filePath = input.tool_input?.file_path
|
|
74
|
+
if (!filePath || !isCodeFile(filePath) || !fs.existsSync(filePath)) {
|
|
75
|
+
return { exitCode: 0 }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let content
|
|
79
|
+
try {
|
|
80
|
+
content = fs.readFileSync(filePath, 'utf8')
|
|
81
|
+
} catch {
|
|
82
|
+
return { exitCode: 0 }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const maxLines = getMaxLines(filePath)
|
|
86
|
+
const lineCount = countLines(content)
|
|
87
|
+
if (lineCount <= maxLines) {
|
|
88
|
+
return { exitCode: 0 }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
exitCode: 0,
|
|
93
|
+
stderr:
|
|
94
|
+
`[Hook] 代码文件超过 ${maxLines} 行: ` +
|
|
95
|
+
`${filePath} (${lineCount} 行),请拆分后继续。\n`
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = { run }
|
|
100
|
+
|
|
101
|
+
if (require.main === module) {
|
|
102
|
+
let data = ''
|
|
103
|
+
process.stdin.setEncoding('utf8')
|
|
104
|
+
process.stdin.on('data', chunk => { data += chunk })
|
|
105
|
+
process.stdin.on('end', () => {
|
|
106
|
+
const result = run(data)
|
|
107
|
+
if (result.stderr) process.stderr.write(result.stderr)
|
|
108
|
+
if (result.stdout) process.stdout.write(result.stdout)
|
|
109
|
+
process.exit(result.exitCode || 0)
|
|
110
|
+
})
|
|
111
|
+
}
|
package/package.json
CHANGED
package/rules/02-code-size.md
CHANGED
|
@@ -4,10 +4,19 @@
|
|
|
4
4
|
|
|
5
5
|
| 约束项 | 限制 | 违反后果 |
|
|
6
6
|
| ----------- | -------------- | ---------------------- |
|
|
7
|
-
|
|
|
7
|
+
| 单代码文件行数 | **≤ 600 行** | 必须拆分文件 |
|
|
8
|
+
| 单测试文件行数 | **≤ 1000 行** | 必须拆分测试场景 |
|
|
8
9
|
| 单函数/方法 | **≤ 80 行** | 必须提取子函数 |
|
|
9
10
|
| 函数参数 | **≤ 5 个** | 必须封装为对象 |
|
|
10
11
|
| 嵌套层级 | **≤ 4 层** | 必须提前返回或提取函数 |
|
|
11
12
|
| 单行长度 | **≤ 120 字符** | 必须换行 |
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
**超限处理**:代码文件过长→拆分 | 测试文件过长→拆分场景 | 函数过长→提取 | 参数过多→封装对象 | 嵌套过深→早返回
|
|
15
|
+
|
|
16
|
+
## 执行要求
|
|
17
|
+
|
|
18
|
+
- 编辑代码后必须触发轻量代码规模检查,只检查本次编辑的代码文件。
|
|
19
|
+
- `*.test.*`、`*.spec.*` 和 `__tests__/` 下的测试文件上限为 1000 行。
|
|
20
|
+
- 文档、设计稿、规则说明等非代码文件不受 600 行限制。
|
|
21
|
+
- 检查通过时保持静默;只有代码文件超过对应上限时输出短警告。
|
|
22
|
+
- 收到超限警告后,先拆分职责、提取模块或拆分测试场景,再继续实现。
|
|
@@ -643,6 +643,7 @@ function checkScriptLayout() {
|
|
|
643
643
|
"hooks/session-start.js",
|
|
644
644
|
"hooks/session-end.js",
|
|
645
645
|
"hooks/commit-quality.js",
|
|
646
|
+
"hooks/check-code-size.js",
|
|
646
647
|
"scripts/learning/review-confidence.js",
|
|
647
648
|
"scripts/learning/project-utils.js",
|
|
648
649
|
"scripts/learning/projects.js",
|
package/settings.json
CHANGED
|
@@ -22,6 +22,11 @@
|
|
|
22
22
|
"type": "command",
|
|
23
23
|
"command": "node \"$HOME/.claude/hooks/runtime/run-with-flags.js\" post:edit:console-log \"$HOME/.claude/hooks/check-console-log.js\" standard,strict",
|
|
24
24
|
"timeout": 5
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"type": "command",
|
|
28
|
+
"command": "node \"$HOME/.claude/hooks/runtime/run-with-flags.js\" post:edit:code-size \"$HOME/.claude/hooks/check-code-size.js\" standard,strict",
|
|
29
|
+
"timeout": 5
|
|
25
30
|
}
|
|
26
31
|
]
|
|
27
32
|
},
|