@leocm/flowcraft 0.0.0-stage → 2.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.
Files changed (269) hide show
  1. package/README.md +846 -3
  2. package/bin/cli.js +102 -0
  3. package/flowcraft.yaml +30 -0
  4. package/install.sh +24 -0
  5. package/lib/args.js +34 -0
  6. package/lib/config.js +20 -0
  7. package/lib/copy.js +131 -0
  8. package/lib/fix.js +110 -0
  9. package/lib/fsutil.js +104 -0
  10. package/lib/global.js +229 -0
  11. package/lib/init.js +43 -0
  12. package/lib/runtime.js +84 -0
  13. package/lib/version.js +55 -0
  14. package/lib/wizard.js +125 -0
  15. package/package.json +31 -4
  16. package/presets/backend.yaml +20 -0
  17. package/presets/default.yaml +15 -0
  18. package/presets/frontend.yaml +20 -0
  19. package/presets/quick-fix.yaml +15 -0
  20. package/setup/.claude/commands/P0.5.md +1 -0
  21. package/setup/.claude/commands/P0.md +1 -0
  22. package/setup/.claude/commands/P1.md +1 -0
  23. package/setup/.claude/commands/P2.md +1 -0
  24. package/setup/.claude/commands/P3.md +1 -0
  25. package/setup/.claude/commands/P4.md +1 -0
  26. package/setup/.claude/commands/P5.md +1 -0
  27. package/setup/.claude/commands/code-analysis-init.md +1 -0
  28. package/setup/.claude/commands/code-analysis-rebuild.md +1 -0
  29. package/setup/.claude/commands/code-analysis-show-memory.md +1 -0
  30. package/setup/.claude/commands/code-analysis-update.md +1 -0
  31. package/setup/.claude/hooks/check-phase-advancement.py +180 -0
  32. package/setup/.claude/hooks/check-phase-test.py +152 -0
  33. package/setup/.claude/hooks/check-phase-write.py +232 -0
  34. package/setup/.claude/hooks/dispatch.py +64 -0
  35. package/setup/.claude/hooks/file-suggestion.py +45 -0
  36. package/setup/.claude/hooks/flowcraft_env.py +99 -0
  37. package/setup/.claude/hooks/log_session.py +91 -0
  38. package/setup/.claude/hooks/permission-request.py +125 -0
  39. package/setup/.claude/hooks/post-tool-failure.py +34 -0
  40. package/setup/.claude/hooks/pre-compact.py +64 -0
  41. package/setup/.claude/hooks/review-runner.py +844 -0
  42. package/setup/.claude/hooks/session-end.py +182 -0
  43. package/setup/.claude/hooks/session-start.py +188 -0
  44. package/setup/.claude/hooks/settings.json +0 -0
  45. package/setup/.claude/hooks/setup-keys.py +280 -0
  46. package/setup/.claude/hooks/statusline.py +61 -0
  47. package/setup/.claude/hooks/stop-check.py +169 -0
  48. package/setup/.claude/hooks/subagent-start.py +48 -0
  49. package/setup/.claude/hooks/subagent-stop.py +61 -0
  50. package/setup/.claude/hooks/task-completed.py +55 -0
  51. package/setup/.claude/hooks/task_manager.py +592 -0
  52. package/setup/.claude/hooks/user-prompt-submit.py +85 -0
  53. package/setup/.claude/project-state.md +109 -0
  54. package/setup/.claude/settings.json +237 -0
  55. package/setup/.claude/settings.local.json +13 -0
  56. package/setup/.claude/skills/P5report/SKILL.md +77 -0
  57. package/setup/.claude/skills/Pstatus/SKILL.md +68 -0
  58. package/setup/.claude/skills/archive/SKILL.md +196 -0
  59. package/setup/.claude/skills/brainstorming/SKILL.md +155 -0
  60. package/setup/.claude/skills/brainstorming/scripts/frame-template.html +213 -0
  61. package/setup/.claude/skills/brainstorming/scripts/helper.js +167 -0
  62. package/setup/.claude/skills/brainstorming/scripts/server.cjs +723 -0
  63. package/setup/.claude/skills/brainstorming/scripts/start-server.py +245 -0
  64. package/setup/.claude/skills/brainstorming/scripts/stop-server.py +178 -0
  65. package/setup/.claude/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
  66. package/setup/.claude/skills/brainstorming/visual-companion.md +298 -0
  67. package/setup/.claude/skills/checkpoint/SKILL.md +42 -0
  68. package/setup/.claude/skills/chrome-integration/SKILL.md +67 -0
  69. package/setup/.claude/skills/code-analysis/SKILL.md +1243 -0
  70. package/setup/.claude/skills/code-review/SKILL.md +265 -0
  71. package/setup/.claude/skills/cross-review/SKILL.md +41 -0
  72. package/setup/.claude/skills/executing-plans/SKILL.md +64 -0
  73. package/setup/.claude/skills/git-prep/SKILL.md +50 -0
  74. package/setup/.claude/skills/gitnexus/gitnexus-cli/SKILL.md +67 -0
  75. package/setup/.claude/skills/gitnexus/gitnexus-debugging/SKILL.md +89 -0
  76. package/setup/.claude/skills/gitnexus/gitnexus-exploring/SKILL.md +78 -0
  77. package/setup/.claude/skills/gitnexus/gitnexus-guide/SKILL.md +66 -0
  78. package/setup/.claude/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +97 -0
  79. package/setup/.claude/skills/gitnexus/gitnexus-refactoring/SKILL.md +121 -0
  80. package/setup/.claude/skills/graphify/SKILL.md +49 -0
  81. package/setup/.claude/skills/openspec-apply-change/SKILL.md +185 -0
  82. package/setup/.claude/skills/openspec-archive-change/SKILL.md +182 -0
  83. package/setup/.claude/skills/openspec-bulk-archive-change/SKILL.md +339 -0
  84. package/setup/.claude/skills/openspec-continue-change/SKILL.md +118 -0
  85. package/setup/.claude/skills/openspec-explore/SKILL.md +311 -0
  86. package/setup/.claude/skills/openspec-ff-change/SKILL.md +113 -0
  87. package/setup/.claude/skills/openspec-new-change/SKILL.md +77 -0
  88. package/setup/.claude/skills/openspec-onboard/SKILL.md +573 -0
  89. package/setup/.claude/skills/openspec-propose/SKILL.md +149 -0
  90. package/setup/.claude/skills/openspec-sync-specs/SKILL.md +262 -0
  91. package/setup/.claude/skills/openspec-update-change/SKILL.md +91 -0
  92. package/setup/.claude/skills/openspec-verify-change/SKILL.md +175 -0
  93. package/setup/.claude/skills/oss-version-check/SKILL.md +71 -0
  94. package/setup/.claude/skills/oss-version-check/check_oss_version.py +205 -0
  95. package/setup/.claude/skills/phase/SKILL.md +63 -0
  96. package/setup/.claude/skills/playwright/SKILL.md +86 -0
  97. package/setup/.claude/skills/playwright-cli/SKILL.md +420 -0
  98. package/setup/.claude/skills/playwright-cli/references/element-attributes.md +23 -0
  99. package/setup/.claude/skills/playwright-cli/references/playwright-tests.md +39 -0
  100. package/setup/.claude/skills/playwright-cli/references/references/element-attributes.md +23 -0
  101. package/setup/.claude/skills/playwright-cli/references/references/playwright-tests.md +39 -0
  102. package/setup/.claude/skills/playwright-cli/references/references/request-mocking.md +87 -0
  103. package/setup/.claude/skills/playwright-cli/references/references/running-code.md +241 -0
  104. package/setup/.claude/skills/playwright-cli/references/references/session-management.md +225 -0
  105. package/setup/.claude/skills/playwright-cli/references/references/storage-state.md +275 -0
  106. package/setup/.claude/skills/playwright-cli/references/references/test-generation.md +433 -0
  107. package/setup/.claude/skills/playwright-cli/references/references/tracing.md +139 -0
  108. package/setup/.claude/skills/playwright-cli/references/references/video-recording.md +143 -0
  109. package/setup/.claude/skills/playwright-cli/references/request-mocking.md +87 -0
  110. package/setup/.claude/skills/playwright-cli/references/running-code.md +241 -0
  111. package/setup/.claude/skills/playwright-cli/references/session-management.md +225 -0
  112. package/setup/.claude/skills/playwright-cli/references/storage-state.md +275 -0
  113. package/setup/.claude/skills/playwright-cli/references/test-generation.md +433 -0
  114. package/setup/.claude/skills/playwright-cli/references/tracing.md +139 -0
  115. package/setup/.claude/skills/playwright-cli/references/video-recording.md +143 -0
  116. package/setup/.claude/skills/review/SKILL.md +98 -0
  117. package/setup/.claude/skills/review-tools/SKILL.md +76 -0
  118. package/setup/.claude/skills/subagent-driven-development/SKILL.md +94 -0
  119. package/setup/.claude/skills/task/SKILL.md +26 -0
  120. package/setup/.claude/skills/test-driven-development/SKILL.md +320 -0
  121. package/setup/.claude/skills/test-driven-development/writing-good-tests.md +198 -0
  122. package/setup/.claude/skills/ui-ux-pro-max/SKILL.md +377 -0
  123. package/setup/.claude/skills/ui-ux-pro-max/data/charts.csv +26 -0
  124. package/setup/.claude/skills/ui-ux-pro-max/data/colors.csv +97 -0
  125. package/setup/.claude/skills/ui-ux-pro-max/data/icons.csv +101 -0
  126. package/setup/.claude/skills/ui-ux-pro-max/data/landing.csv +31 -0
  127. package/setup/.claude/skills/ui-ux-pro-max/data/products.csv +97 -0
  128. package/setup/.claude/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
  129. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/astro.csv +54 -0
  130. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  131. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  132. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  133. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  134. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
  135. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  136. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  137. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
  138. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  139. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  140. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  141. package/setup/.claude/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  142. package/setup/.claude/skills/ui-ux-pro-max/data/styles.csv +68 -0
  143. package/setup/.claude/skills/ui-ux-pro-max/data/typography.csv +58 -0
  144. package/setup/.claude/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
  145. package/setup/.claude/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  146. package/setup/.claude/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
  147. package/setup/.claude/skills/ui-ux-pro-max/scripts/core.py +253 -0
  148. package/setup/.claude/skills/ui-ux-pro-max/scripts/design_system.py +1067 -0
  149. package/setup/.claude/skills/ui-ux-pro-max/scripts/search.py +114 -0
  150. package/setup/.claude/skills/ui-ux-standards/SKILL.md +154 -0
  151. package/setup/.claude/skills/verification-before-completion/SKILL.md +120 -0
  152. package/setup/.claude/skills/wiki-read/SKILL.md +190 -0
  153. package/setup/.claude/skills/writing-plans/SKILL.md +168 -0
  154. package/setup/.claude/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
  155. package/setup/.claude/tasks/.last-session +1 -0
  156. package/setup/.codex/.agents/skills/P5report/SKILL.md +77 -0
  157. package/setup/.codex/.agents/skills/Pstatus/SKILL.md +30 -0
  158. package/setup/.codex/.agents/skills/archive/SKILL.md +196 -0
  159. package/setup/.codex/.agents/skills/brainstorming/SKILL.md +155 -0
  160. package/setup/.codex/.agents/skills/brainstorming/scripts/frame-template.html +213 -0
  161. package/setup/.codex/.agents/skills/brainstorming/scripts/helper.js +167 -0
  162. package/setup/.codex/.agents/skills/brainstorming/scripts/server.cjs +723 -0
  163. package/setup/.codex/.agents/skills/brainstorming/scripts/start-server.py +245 -0
  164. package/setup/.codex/.agents/skills/brainstorming/scripts/stop-server.py +178 -0
  165. package/setup/.codex/.agents/skills/brainstorming/spec-document-reviewer-prompt.md +49 -0
  166. package/setup/.codex/.agents/skills/brainstorming/visual-companion.md +298 -0
  167. package/setup/.codex/.agents/skills/checkpoint/SKILL.md +42 -0
  168. package/setup/.codex/.agents/skills/chrome-integration/SKILL.md +67 -0
  169. package/setup/.codex/.agents/skills/code-analysis/SKILL.md +1243 -0
  170. package/setup/.codex/.agents/skills/code-review/SKILL.md +265 -0
  171. package/setup/.codex/.agents/skills/cross-review/SKILL.md +42 -0
  172. package/setup/.codex/.agents/skills/executing-plans/SKILL.md +64 -0
  173. package/setup/.codex/.agents/skills/git-prep/SKILL.md +50 -0
  174. package/setup/.codex/.agents/skills/gitnexus/gitnexus-cli/SKILL.md +67 -0
  175. package/setup/.codex/.agents/skills/gitnexus/gitnexus-debugging/SKILL.md +89 -0
  176. package/setup/.codex/.agents/skills/gitnexus/gitnexus-exploring/SKILL.md +78 -0
  177. package/setup/.codex/.agents/skills/gitnexus/gitnexus-guide/SKILL.md +66 -0
  178. package/setup/.codex/.agents/skills/gitnexus/gitnexus-impact-analysis/SKILL.md +97 -0
  179. package/setup/.codex/.agents/skills/gitnexus/gitnexus-refactoring/SKILL.md +121 -0
  180. package/setup/.codex/.agents/skills/graphify/SKILL.md +49 -0
  181. package/setup/.codex/.agents/skills/openspec-apply-change/SKILL.md +185 -0
  182. package/setup/.codex/.agents/skills/openspec-archive-change/SKILL.md +182 -0
  183. package/setup/.codex/.agents/skills/openspec-bulk-archive-change/SKILL.md +339 -0
  184. package/setup/.codex/.agents/skills/openspec-continue-change/SKILL.md +118 -0
  185. package/setup/.codex/.agents/skills/openspec-explore/SKILL.md +311 -0
  186. package/setup/.codex/.agents/skills/openspec-ff-change/SKILL.md +113 -0
  187. package/setup/.codex/.agents/skills/openspec-new-change/SKILL.md +77 -0
  188. package/setup/.codex/.agents/skills/openspec-onboard/SKILL.md +573 -0
  189. package/setup/.codex/.agents/skills/openspec-propose/SKILL.md +149 -0
  190. package/setup/.codex/.agents/skills/openspec-sync-specs/SKILL.md +262 -0
  191. package/setup/.codex/.agents/skills/openspec-update-change/SKILL.md +91 -0
  192. package/setup/.codex/.agents/skills/openspec-verify-change/SKILL.md +175 -0
  193. package/setup/.codex/.agents/skills/oss-version-check/SKILL.md +71 -0
  194. package/setup/.codex/.agents/skills/oss-version-check/check_oss_version.py +205 -0
  195. package/setup/.codex/.agents/skills/phase/SKILL.md +63 -0
  196. package/setup/.codex/.agents/skills/playwright/SKILL.md +86 -0
  197. package/setup/.codex/.agents/skills/playwright-cli/SKILL.md +420 -0
  198. package/setup/.codex/.agents/skills/playwright-cli/references/element-attributes.md +23 -0
  199. package/setup/.codex/.agents/skills/playwright-cli/references/playwright-tests.md +39 -0
  200. package/setup/.codex/.agents/skills/playwright-cli/references/references/element-attributes.md +23 -0
  201. package/setup/.codex/.agents/skills/playwright-cli/references/references/playwright-tests.md +39 -0
  202. package/setup/.codex/.agents/skills/playwright-cli/references/references/request-mocking.md +87 -0
  203. package/setup/.codex/.agents/skills/playwright-cli/references/references/running-code.md +241 -0
  204. package/setup/.codex/.agents/skills/playwright-cli/references/references/session-management.md +225 -0
  205. package/setup/.codex/.agents/skills/playwright-cli/references/references/storage-state.md +275 -0
  206. package/setup/.codex/.agents/skills/playwright-cli/references/references/test-generation.md +433 -0
  207. package/setup/.codex/.agents/skills/playwright-cli/references/references/tracing.md +139 -0
  208. package/setup/.codex/.agents/skills/playwright-cli/references/references/video-recording.md +143 -0
  209. package/setup/.codex/.agents/skills/playwright-cli/references/request-mocking.md +87 -0
  210. package/setup/.codex/.agents/skills/playwright-cli/references/running-code.md +241 -0
  211. package/setup/.codex/.agents/skills/playwright-cli/references/session-management.md +225 -0
  212. package/setup/.codex/.agents/skills/playwright-cli/references/storage-state.md +275 -0
  213. package/setup/.codex/.agents/skills/playwright-cli/references/test-generation.md +433 -0
  214. package/setup/.codex/.agents/skills/playwright-cli/references/tracing.md +139 -0
  215. package/setup/.codex/.agents/skills/playwright-cli/references/video-recording.md +143 -0
  216. package/setup/.codex/.agents/skills/review/SKILL.md +98 -0
  217. package/setup/.codex/.agents/skills/review-tools/SKILL.md +76 -0
  218. package/setup/.codex/.agents/skills/subagent-driven-development/SKILL.md +94 -0
  219. package/setup/.codex/.agents/skills/task/SKILL.md +27 -0
  220. package/setup/.codex/.agents/skills/test-driven-development/SKILL.md +320 -0
  221. package/setup/.codex/.agents/skills/test-driven-development/writing-good-tests.md +198 -0
  222. package/setup/.codex/.agents/skills/ui-ux-pro-max/SKILL.md +377 -0
  223. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/charts.csv +26 -0
  224. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/colors.csv +97 -0
  225. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/icons.csv +101 -0
  226. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/landing.csv +31 -0
  227. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/products.csv +97 -0
  228. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/react-performance.csv +45 -0
  229. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/astro.csv +54 -0
  230. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
  231. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/html-tailwind.csv +56 -0
  232. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
  233. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nextjs.csv +53 -0
  234. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nuxt-ui.csv +51 -0
  235. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/nuxtjs.csv +59 -0
  236. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
  237. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/react.csv +54 -0
  238. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/shadcn.csv +61 -0
  239. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/svelte.csv +54 -0
  240. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
  241. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/stacks/vue.csv +50 -0
  242. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/styles.csv +68 -0
  243. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/typography.csv +58 -0
  244. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/ui-reasoning.csv +101 -0
  245. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/ux-guidelines.csv +100 -0
  246. package/setup/.codex/.agents/skills/ui-ux-pro-max/data/web-interface.csv +31 -0
  247. package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/core.py +253 -0
  248. package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/design_system.py +1067 -0
  249. package/setup/.codex/.agents/skills/ui-ux-pro-max/scripts/search.py +114 -0
  250. package/setup/.codex/.agents/skills/ui-ux-standards/SKILL.md +154 -0
  251. package/setup/.codex/.agents/skills/verification-before-completion/SKILL.md +120 -0
  252. package/setup/.codex/.agents/skills/wiki-read/SKILL.md +190 -0
  253. package/setup/.codex/.agents/skills/writing-plans/SKILL.md +168 -0
  254. package/setup/.codex/.agents/skills/writing-plans/plan-document-reviewer-prompt.md +49 -0
  255. package/setup/.codex/config.toml +10 -0
  256. package/setup/.codex/hooks/log_session.py +85 -0
  257. package/setup/.codex/hooks/review-runner.py +844 -0
  258. package/setup/.codex/hooks/sdlc-codex-hook.cjs +1020 -0
  259. package/setup/.codex/hooks/setup-keys.py +280 -0
  260. package/setup/.codex/hooks.json +121 -0
  261. package/setup/.codex/model-catalog.json +154 -0
  262. package/setup/.codex/project-state.md +103 -0
  263. package/setup/AGENTS.md +722 -0
  264. package/setup/CLAUDE.md +717 -0
  265. package/setup/install.sh +24 -0
  266. package/setup/tests/test_decision_timeout.py +241 -0
  267. package/setup/tests/test_hook_guards.py +341 -0
  268. package/setup/tests/test_sdlc_bootstrap.py +167 -0
  269. package/setup/tests/test_task_manager.py +148 -0
package/bin/cli.js ADDED
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * cli.js — FlowCraft CLI 入口(R1 常驻 CLI / R2 子命令与旗标)
4
+ * 子命令: install(默认)| init;旗标: --global --preset --quick --interactive --help --version
5
+ */
6
+ 'use strict';
7
+
8
+ const fs = require('node:fs');
9
+ const os = require('node:os');
10
+ const path = require('node:path');
11
+ const { parseArgs } = require('../lib/args');
12
+ const { ensureRuntimes } = require('../lib/runtime');
13
+ const { copyClaudeFiles, copyCodexFiles, copyFlowcraftConfig } = require('../lib/copy');
14
+ const { fixLineEndings, replacePythonCommands, replaceSessionlogUrl, addPathToHooks } = require('../lib/fix');
15
+ const { writeVersionFile } = require('../lib/version');
16
+ const { installGlobal } = require('../lib/global');
17
+ const { initProject } = require('../lib/init');
18
+ const { interactiveSetup, showStats, showNextSteps } = require('../lib/wizard');
19
+ const { SESSIONLOG_URL, FRAMEWORK_VERSION } = require('../lib/config');
20
+
21
+ const PKG_ROOT = path.resolve(__dirname, '..');
22
+
23
+ /**
24
+ * 解析 --preset 参数为文件路径(名称或文件,对齐 install.sh)
25
+ * @param {string|null} presetArg 预设参数
26
+ * @returns {string|null} 预设文件绝对路径
27
+ */
28
+ function resolvePreset(presetArg) {
29
+ if (!presetArg) return null;
30
+ if (fs.existsSync(presetArg)) return path.resolve(presetArg);
31
+ const byName = path.join(PKG_ROOT, 'presets', `${presetArg.replace(/\.yaml$/, '')}.yaml`);
32
+ if (fs.existsSync(byName)) return byName;
33
+ throw new Error(`预设文件不存在: ${presetArg}`);
34
+ }
35
+
36
+ /**
37
+ * 项目级安装主流程(对齐 install.sh main 的项目分支)
38
+ * @param {object} opts 解析后的参数
39
+ * @returns {Promise<void>}
40
+ */
41
+ async function runInstall(opts) {
42
+ console.log(`脚本目录: ${PKG_ROOT}`);
43
+ console.log(`项目根目录: ${process.cwd()}`);
44
+ console.log('');
45
+ const py = ensureRuntimes();
46
+ let presetFile = resolvePreset(opts.presetArg);
47
+ let presetMode = opts.presetMode || '';
48
+ if (!opts.quick && process.stdin.isTTY) {
49
+ const w = await interactiveSetup(PKG_ROOT, process.cwd(), opts.interactive);
50
+ presetFile = w.presetFile || presetFile;
51
+ presetMode = w.presetMode || presetMode;
52
+ } else if (opts.quick) {
53
+ presetMode = 'default';
54
+ }
55
+ const target = path.join(process.cwd(), '.claude');
56
+ const copyCount = copyClaudeFiles(path.join(PKG_ROOT, 'setup', '.claude'), process.cwd());
57
+ copyCodexFiles(path.join(PKG_ROOT, 'setup', '.codex'), process.cwd());
58
+ copyFlowcraftConfig(PKG_ROOT, process.cwd(), presetFile);
59
+ addPathToHooks(target);
60
+ const fixCount = fixLineEndings(target);
61
+ const replaceCount = replacePythonCommands(target, py);
62
+ const urlCount = replaceSessionlogUrl(SESSIONLOG_URL, target, path.join(PKG_ROOT, 'setup', '.claude'));
63
+ writeVersionFile(target, process.cwd(), presetMode || 'default');
64
+ showStats({ copyCount, fixCount, replaceCount, urlCount }, process.cwd(), SESSIONLOG_URL);
65
+ showNextSteps();
66
+ }
67
+
68
+ /**
69
+ * 主入口:解析参数并分发到对应模式
70
+ * @returns {Promise<void>}
71
+ */
72
+ async function main() {
73
+ const opts = parseArgs(process.argv.slice(2));
74
+ console.log('==========================================');
75
+ console.log(' flowcraft - FlowCraft 框架安装工具');
76
+ console.log('==========================================\n');
77
+ for (const u of opts.unknown) console.log(`未知参数: ${u}`);
78
+ if (opts.mode === 'help') {
79
+ console.log('用法: flowcraft [install|init] [--global] [--preset <name|file>] [--quick] [--interactive]');
80
+ return;
81
+ }
82
+ if (opts.mode === 'version') {
83
+ console.log(FRAMEWORK_VERSION);
84
+ return;
85
+ }
86
+ const home = os.homedir();
87
+ if (opts.mode === 'global') {
88
+ const py = ensureRuntimes();
89
+ installGlobal(PKG_ROOT, home, py);
90
+ return;
91
+ }
92
+ if (opts.mode === 'init') {
93
+ initProject(PKG_ROOT, process.cwd());
94
+ return;
95
+ }
96
+ await runInstall(opts);
97
+ }
98
+
99
+ main().catch((err) => {
100
+ console.error(`错误: ${err.message}`);
101
+ process.exit(1);
102
+ });
package/flowcraft.yaml ADDED
@@ -0,0 +1,30 @@
1
+ # flowcraft.yaml - FlowCraft 项目配置
2
+ # 文档: https://github.com/flowcraft/flowcraft
3
+ version: 1
4
+
5
+ # 阶段开关: true=始终启用, false=跳过, auto=AI自动判断
6
+ stages:
7
+ P1: auto # 需求澄清(小任务可跳过)
8
+ P2: true # 编码(始终启用)
9
+ P3: auto # 测试(简单改动可跳过)
10
+ P4: true # 审查(始终启用)
11
+ P5: auto # 交付(小任务可跳过)
12
+
13
+ # 决策点超时自动默认(决策卡发出后超时未选择 → 自动按推荐项执行;危险操作决策点除外)
14
+ decision_timeout: 120 # 秒;缺失时默认 120
15
+
16
+ # 审查规则
17
+ review:
18
+ standard: alibaba # 审查标准: alibaba / google / custom
19
+ retry_limit: 3 # P4最大重试次数
20
+
21
+ # 技术栈默认值(P1技术选型时预填)
22
+ tech_defaults:
23
+ language: "" # java / typescript / python / go
24
+ framework: "" # spring-boot / nextjs / fastapi / ...
25
+ testing: "" # junit / jest / pytest / ...
26
+
27
+ # 团队预设(可选,指向本地路径或git URL)
28
+ # 示例: team_preset: "presets/backend.yaml"
29
+ # 示例: team_preset: "https://github.com/org/flowcraft-presets/backend.yaml"
30
+ team_preset: ""
package/install.sh ADDED
@@ -0,0 +1,24 @@
1
+ #!/bin/bash
2
+ # install.sh - FlowCraft 框架安装工具(薄包装:Node 为唯一安装逻辑源)
3
+ # 用法: ./install.sh [install|init] [--global] [--preset <name|file>] [--quick] [--interactive]
4
+ # 实现见 bin/cli.js 与 lib/(方案A 单实现收敛:本脚本仅探测 node 并转调)
5
+
6
+ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
7
+
8
+ # 探测 Node(>=20),缺失时尝试安装后重试一次
9
+ if ! node --version >/dev/null 2>&1; then
10
+ echo "[运行时] 未检测到 Node.js,尝试安装 Node.js 24 ..."
11
+ if command -v winget >/dev/null 2>&1; then
12
+ winget install -e --id OpenJS.NodeJS.24 --accept-source-agreements --accept-package-agreements >/dev/null 2>&1
13
+ elif command -v brew >/dev/null 2>&1; then
14
+ brew install node@24 >/dev/null 2>&1
15
+ elif command -v apt-get >/dev/null 2>&1; then
16
+ sudo apt-get install -y nodejs >/dev/null 2>&1
17
+ fi
18
+ if ! node --version >/dev/null 2>&1; then
19
+ echo "错误: 未找到 Node.js(>=20),请先安装 https://nodejs.org/ 后重试"
20
+ exit 1
21
+ fi
22
+ fi
23
+
24
+ exec node "$SCRIPT_DIR/bin/cli.js" "$@"
package/lib/args.js ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * args.js — CLI 参数解析(手写,零依赖;R2 旗标面对齐 install.sh)
3
+ */
4
+ 'use strict';
5
+
6
+ /**
7
+ * 解析命令行参数
8
+ * @param {string[]} argv 进程参数(不含 node 与脚本路径)
9
+ * @returns {{mode:string,presetArg:string|null,presetMode:string,quick:boolean,interactive:boolean,unknown:string[]}} 解析结果
10
+ */
11
+ function parseArgs(argv) {
12
+ const out = { mode: 'install', presetArg: null, presetMode: '', quick: false, interactive: false, unknown: [] };
13
+ const rest = [...argv];
14
+ while (rest.length > 0) {
15
+ const a = rest.shift();
16
+ if (a === 'init' || a === '--init') out.mode = 'init';
17
+ else if (a === 'install') out.mode = 'install';
18
+ else if (a === '--global') out.mode = 'global';
19
+ else if (a === '--quick') {
20
+ out.quick = true;
21
+ out.presetMode = 'default';
22
+ } else if (a === '--interactive') out.interactive = true;
23
+ else if (a === '--preset') {
24
+ out.presetArg = rest.shift() || null;
25
+ if (!out.presetArg) out.unknown.push('--preset(缺参数)');
26
+ else out.presetMode = out.presetArg.replace(/\.yaml$/, '');
27
+ } else if (a === '--help' || a === '-h') out.mode = 'help';
28
+ else if (a === '--version' || a === '-V') out.mode = 'version';
29
+ else out.unknown.push(a);
30
+ }
31
+ return out;
32
+ }
33
+
34
+ module.exports = { parseArgs };
package/lib/config.js ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * config.js — 共享常量(对齐 install.sh 顶部定义)
3
+ */
4
+ 'use strict';
5
+
6
+ /** 默认 sessionlog MCP 地址(与 install.sh SESSIONLOG_URL 一致) */
7
+ const SESSIONLOG_URL = 'http://192.168.2.99:8181/api/logs';
8
+
9
+ /** 框架版本(与 write_version_file / README 一致) */
10
+ const FRAMEWORK_VERSION = '2.0.0';
11
+
12
+ /** 需要 PATH 注入的 hook 文件清单(对齐 install.sh add_path_to_hooks) */
13
+ const HOOK_FILES = [
14
+ 'check-phase-advancement.py', 'check-phase-test.py', 'check-phase-write.py', 'dispatch.py',
15
+ 'file-suggestion.py', 'permission-request.py', 'post-tool-failure.py', 'pre-compact.py',
16
+ 'session-end.py', 'session-start.py', 'statusline.py', 'stop-check.py',
17
+ 'subagent-start.py', 'subagent-stop.py', 'task-completed.py', 'user-prompt-submit.py',
18
+ ];
19
+
20
+ module.exports = { SESSIONLOG_URL, FRAMEWORK_VERSION, HOOK_FILES };
package/lib/copy.js ADDED
@@ -0,0 +1,131 @@
1
+ /**
2
+ * copy.js — 项目级安装复制(R4:项目数据永不覆盖/备份策略,对齐 copy_claude_files/copy_codex_files)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const { timestamp, backupFile, copyDir } = require('./fsutil');
9
+
10
+ /**
11
+ * 复制 setup/.claude 到项目 .claude(项目数据永不覆盖/不删除/不备份)
12
+ * @param {string} setupClaude 源目录(setup/.claude,其父目录含 CLAUDE.md)
13
+ * @param {string} projectRoot 项目根
14
+ * @returns {number} 目标 .claude 内文件总数
15
+ */
16
+ function copyClaudeFiles(setupClaude, projectRoot) {
17
+ const setupRoot = path.dirname(setupClaude);
18
+ console.log('[1/3] 复制 setup/.claude 到项目根目录(项目数据永不覆盖)...');
19
+ if (!fs.existsSync(setupClaude)) throw new Error(`setup/.claude 目录不存在: ${setupClaude}`);
20
+ const target = path.join(projectRoot, '.claude');
21
+ fs.mkdirSync(target, { recursive: true });
22
+ const ts = timestamp();
23
+ for (const d of ['reports', 'reviews', 'archive']) fs.mkdirSync(path.join(target, d), { recursive: true });
24
+ for (const f of ['project-state.md', 'settings.local.json']) {
25
+ const dst = path.join(target, f);
26
+ if (fs.existsSync(dst)) {
27
+ if (f === 'project-state.md') backupFile(dst, ts);
28
+ console.log(` 保留 ${f}(不覆盖)`);
29
+ } else if (fs.existsSync(path.join(setupClaude, f))) {
30
+ fs.copyFileSync(path.join(setupClaude, f), dst);
31
+ }
32
+ }
33
+ for (const sub of ['hooks', 'commands', 'skills', 'sdlc']) fs.mkdirSync(path.join(target, sub), { recursive: true });
34
+ const settingsSrc = path.join(setupClaude, 'settings.json');
35
+ if (fs.existsSync(settingsSrc)) {
36
+ backupFile(path.join(target, 'settings.json'), ts);
37
+ fs.copyFileSync(settingsSrc, path.join(target, 'settings.json'));
38
+ }
39
+ const claudeMd = path.join(setupRoot, 'CLAUDE.md');
40
+ if (fs.existsSync(claudeMd)) {
41
+ backupFile(path.join(projectRoot, 'CLAUDE.md'), ts);
42
+ fs.copyFileSync(claudeMd, path.join(projectRoot, 'CLAUDE.md'));
43
+ console.log(' ✓ CLAUDE.md');
44
+ }
45
+ const count = countFiles(target);
46
+ console.log(`✓ 复制完成: ${count} 个文件(项目数据已保护)`);
47
+ return count;
48
+ }
49
+
50
+ /**
51
+ * 统计目录内文件数
52
+ * @param {string} dir 目录
53
+ * @returns {number} 文件数
54
+ */
55
+ function countFiles(dir) {
56
+ let n = 0;
57
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
58
+ n += e.isDirectory() ? countFiles(path.join(dir, e.name)) : 1;
59
+ }
60
+ return n;
61
+ }
62
+
63
+ /**
64
+ * 复制 setup/.codex 与 AGENTS.md 到项目根(Codex 支持,项目数据永不覆盖)
65
+ * @param {string} setupCodex 源目录(setup/.codex,其父目录含 AGENTS.md)
66
+ * @param {string} projectRoot 项目根
67
+ * @returns {void}
68
+ */
69
+ function copyCodexFiles(setupCodex, projectRoot) {
70
+ const setupRoot = path.dirname(setupCodex);
71
+ console.log('[Codex] 复制 setup/.codex 与 AGENTS.md 到项目根目录(项目数据永不覆盖)...');
72
+ if (!fs.existsSync(setupCodex)) {
73
+ console.log(' 跳过: setup/.codex 目录不存在');
74
+ return;
75
+ }
76
+ const target = path.join(projectRoot, '.codex');
77
+ fs.mkdirSync(target, { recursive: true });
78
+ const ts = timestamp();
79
+ const stateDst = path.join(target, 'project-state.md');
80
+ if (fs.existsSync(stateDst)) {
81
+ backupFile(stateDst, ts);
82
+ console.log(' 保留 .codex/project-state.md(不覆盖)');
83
+ } else if (fs.existsSync(path.join(setupCodex, 'project-state.md'))) {
84
+ fs.copyFileSync(path.join(setupCodex, 'project-state.md'), stateDst);
85
+ }
86
+ for (const f of ['config.toml', 'model-catalog.json']) {
87
+ const dst = path.join(target, f);
88
+ if (fs.existsSync(dst)) console.log(` 保留 .codex/${f}(不覆盖)`);
89
+ else if (fs.existsSync(path.join(setupCodex, f))) fs.copyFileSync(path.join(setupCodex, f), dst);
90
+ }
91
+ fs.mkdirSync(path.join(target, 'hooks'), { recursive: true });
92
+ console.log(' ✓ .codex(hooks 框架文件已更新,项目数据已保护)');
93
+ const agentsSrc = path.join(setupRoot, 'AGENTS.md');
94
+ if (fs.existsSync(agentsSrc)) {
95
+ const agentsDst = path.join(projectRoot, 'AGENTS.md');
96
+ if (fs.existsSync(agentsDst)) {
97
+ fs.copyFileSync(agentsDst, `${agentsDst}.bak`);
98
+ console.log(' ✓ AGENTS.md.bak(旧文件备份)');
99
+ }
100
+ fs.copyFileSync(agentsSrc, agentsDst);
101
+ console.log(' ✓ AGENTS.md(已强制覆盖)');
102
+ }
103
+ fs.mkdirSync(path.join(projectRoot, '.agents', 'skills'), { recursive: true });
104
+ console.log(' ✓ .agents/skills(空目录,用户级兜底)');
105
+ }
106
+
107
+ /**
108
+ * 复制 flowcraft.yaml(不覆盖已有配置;指定预设时应用预设内容)
109
+ * @param {string} pkgRoot 包根(含 flowcraft.yaml 与 presets/)
110
+ * @param {string} projectRoot 项目根
111
+ * @param {string|null} presetFile 已解析的预设文件路径
112
+ * @returns {void}
113
+ */
114
+ function copyFlowcraftConfig(pkgRoot, projectRoot, presetFile) {
115
+ const src = path.join(pkgRoot, 'flowcraft.yaml');
116
+ const dst = path.join(projectRoot, 'flowcraft.yaml');
117
+ if (!fs.existsSync(src)) return;
118
+ if (fs.existsSync(dst)) {
119
+ console.log('! flowcraft.yaml 已存在,保留现有配置');
120
+ return;
121
+ }
122
+ if (presetFile && fs.existsSync(presetFile)) {
123
+ fs.copyFileSync(presetFile, dst);
124
+ console.log(`✓ 已应用预设: ${presetFile}`);
125
+ } else {
126
+ fs.copyFileSync(src, dst);
127
+ console.log('✓ 已生成 flowcraft.yaml(默认配置)');
128
+ }
129
+ }
130
+
131
+ module.exports = { copyClaudeFiles, copyCodexFiles, copyFlowcraftConfig };
package/lib/fix.js ADDED
@@ -0,0 +1,110 @@
1
+ /**
2
+ * fix.js — 修复类函数(R4:行尾符/python 命令/SESSIONLOG_URL/PATH 注入)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+ const { hasCrlf, isTextFile, listFiles } = require('./fsutil');
9
+ const { HOOK_FILES } = require('./config');
10
+
11
+ /**
12
+ * 修复行尾符(CRLF → LF,.py 与 .ps1,对齐 install.sh fix_line_endings)
13
+ * @param {string} targetDir 目标目录(.claude 或全局 hooks 仓)
14
+ * @returns {number} 修复文件数
15
+ */
16
+ function fixLineEndings(targetDir) {
17
+ console.log('[2/3] 修复行尾符(CRLF -> LF)...');
18
+ let n = 0;
19
+ for (const f of listFiles(targetDir)) {
20
+ if (!/\.(py|ps1)$/i.test(f)) continue;
21
+ const buf = fs.readFileSync(f);
22
+ if (!hasCrlf(buf)) continue;
23
+ fs.writeFileSync(f, Buffer.from(buf.toString('binary').replace(/\r/g, ''), 'binary'));
24
+ console.log(` ✓ ${f}`);
25
+ n += 1;
26
+ }
27
+ console.log(`✓ 行尾符修复完成(修复 ${n} 个文件)`);
28
+ return n;
29
+ }
30
+
31
+ /**
32
+ * 替换 Python 命令(python3/python 均替换为探测命令,对齐 sed 保护式替换)
33
+ * 非 UTF-8 文本跳过并告警,避免解码损坏(.claude 内文件约定 UTF-8)
34
+ * @param {string} targetDir 目标目录
35
+ * @param {string} pythonCmd 探测到的 python 命令
36
+ * @returns {number} 修改文件数
37
+ */
38
+ function replacePythonCommands(targetDir, pythonCmd) {
39
+ console.log(`[3/3] 替换 Python 命令为: ${pythonCmd}`);
40
+ let n = 0;
41
+ for (const f of listFiles(targetDir)) {
42
+ if (!isTextFile(f)) continue;
43
+ const buf = fs.readFileSync(f);
44
+ let text;
45
+ try {
46
+ text = new TextDecoder('utf-8', { fatal: true }).decode(buf);
47
+ } catch {
48
+ console.log(` 跳过(非UTF-8): ${f}`);
49
+ continue;
50
+ }
51
+ if (!/python3|python/.test(text)) continue;
52
+ fs.writeFileSync(f, text.replace(/python3|python/g, pythonCmd));
53
+ n += 1;
54
+ console.log(` ✓ ${f}`);
55
+ }
56
+ console.log(`✓ 替换完成(修改 ${n} 个文件)`);
57
+ return n;
58
+ }
59
+
60
+ /**
61
+ * 分发 sessionlog URL:.py 中 MCP_URL= 的值替换为新 URL(目标与 setup 源同步)
62
+ * @param {string} url 新的 MCP 地址
63
+ * @param {...string} dirs 要处理的目录列表
64
+ * @returns {number} 更新文件数
65
+ */
66
+ function replaceSessionlogUrl(url, ...dirs) {
67
+ console.log(`[4/4] 分发 sessionlog URL: ${url}`);
68
+ let n = 0;
69
+ for (const dir of dirs) {
70
+ for (const f of listFiles(dir)) {
71
+ if (!/\.py$/i.test(f)) continue;
72
+ const text = fs.readFileSync(f, 'utf8');
73
+ if (!/MCP_URL\s*=/.test(text)) continue;
74
+ const out = text.replace(/MCP_URL\s*=\s*".*/, `MCP_URL="${url}"`);
75
+ if (out !== text) {
76
+ fs.writeFileSync(f, out);
77
+ n += 1;
78
+ console.log(` ✓ ${f}`);
79
+ }
80
+ }
81
+ }
82
+ console.log(`✓ URL 分发完成(更新 ${n} 个文件)`);
83
+ return n;
84
+ }
85
+
86
+ /**
87
+ * 对 bash 型 hook 注入 PATH 设置(Python 脚本跳过,避免破坏语法)
88
+ * @param {string} targetDir .claude 目录
89
+ * @returns {number} 注入文件数
90
+ */
91
+ function addPathToHooks(targetDir) {
92
+ console.log('[2/3] 添加 PATH 设置到 hook 文件...');
93
+ let n = 0;
94
+ for (const name of HOOK_FILES) {
95
+ const f = path.join(targetDir, 'hooks', name);
96
+ if (!fs.existsSync(f)) continue;
97
+ const lines = fs.readFileSync(f, 'utf8').split('\n');
98
+ const shebang = lines[0] || '';
99
+ if (!/^#!.*(bash|sh)/.test(shebang)) continue;
100
+ if (lines.some((l) => l.includes('export PATH='))) continue;
101
+ lines.splice(1, 0, 'export PATH="/usr/local/bin:/usr/bin:/bin:$PATH"');
102
+ fs.writeFileSync(f, lines.join('\n'));
103
+ console.log(` ✓ ${name}`);
104
+ n += 1;
105
+ }
106
+ console.log('✓ PATH 设置添加完成');
107
+ return n;
108
+ }
109
+
110
+ module.exports = { fixLineEndings, replacePythonCommands, replaceSessionlogUrl, addPathToHooks };
package/lib/fsutil.js ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * fsutil.js — 文件系统工具(备份/行尾检测/遍历/复制)
3
+ */
4
+ 'use strict';
5
+
6
+ const fs = require('node:fs');
7
+ const path = require('node:path');
8
+
9
+ /**
10
+ * 生成备份时间戳(对齐 date +%Y%m%d-%H%M%S)
11
+ * @returns {string} 形如 20261009-110530
12
+ */
13
+ function timestamp() {
14
+ const d = new Date();
15
+ const p = (n) => String(n).padStart(2, '0');
16
+ return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}`;
17
+ }
18
+
19
+ /**
20
+ * 生成日期时间字符串(对齐 date '+%Y-%m-%d %H:%M:%S')
21
+ * @returns {string} 形如 2026-10-09 11:05:30
22
+ */
23
+ function nowStr() {
24
+ const d = new Date();
25
+ const p = (n) => String(n).padStart(2, '0');
26
+ return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
27
+ }
28
+
29
+ /**
30
+ * 时间戳备份:已存在则复制为 <file>.bak-<ts>(不覆盖旧备份)
31
+ * @param {string} file 目标文件
32
+ * @param {string} ts 时间戳
33
+ * @returns {boolean} 是否完成备份
34
+ */
35
+ function backupFile(file, ts) {
36
+ if (!fs.existsSync(file)) return false;
37
+ fs.copyFileSync(file, `${file}.bak-${ts}`);
38
+ console.log(` 备份: ${file} -> ${path.basename(file)}.bak-${ts}`);
39
+ return true;
40
+ }
41
+
42
+ /**
43
+ * 检测缓冲区是否含 CR(CRLF)
44
+ * @param {Buffer} buf 文件内容
45
+ * @returns {boolean} 是否含 \r
46
+ */
47
+ function hasCrlf(buf) {
48
+ return buf.includes(0x0d);
49
+ }
50
+
51
+ /**
52
+ * 判断文件是否文本(前 8KB 无 NUL,对齐 file|grep text)
53
+ * @param {string} file 文件路径
54
+ * @returns {boolean} 是否文本
55
+ */
56
+ function isTextFile(file) {
57
+ const fd = fs.openSync(file, 'r');
58
+ try {
59
+ const buf = Buffer.alloc(8192);
60
+ const n = fs.readSync(fd, buf, 0, 8192, 0);
61
+ return !buf.slice(0, n).includes(0x00);
62
+ } finally {
63
+ fs.closeSync(fd);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 递归列出目录内全部文件
69
+ * @param {string} dir 目录
70
+ * @returns {string[]} 文件绝对路径列表
71
+ */
72
+ function listFiles(dir) {
73
+ const out = [];
74
+ if (!fs.existsSync(dir)) return out;
75
+ for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
76
+ const abs = path.join(dir, e.name);
77
+ if (e.isDirectory()) out.push(...listFiles(abs));
78
+ else out.push(abs);
79
+ }
80
+ return out;
81
+ }
82
+
83
+ /**
84
+ * 递归复制目录(覆盖同名文件)
85
+ * @param {string} src 源目录
86
+ * @param {string} dst 目标目录
87
+ * @returns {number} 复制的文件数
88
+ */
89
+ function copyDir(src, dst) {
90
+ let n = 0;
91
+ fs.mkdirSync(dst, { recursive: true });
92
+ for (const e of fs.readdirSync(src, { withFileTypes: true })) {
93
+ const s = path.join(src, e.name);
94
+ const d = path.join(dst, e.name);
95
+ if (e.isDirectory()) n += copyDir(s, d);
96
+ else {
97
+ fs.copyFileSync(s, d);
98
+ n += 1;
99
+ }
100
+ }
101
+ return n;
102
+ }
103
+
104
+ module.exports = { timestamp, nowStr, backupFile, hasCrlf, isTextFile, listFiles, copyDir };