@claude-flow/cli 3.0.0-alpha.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.
Files changed (229) hide show
  1. package/.agentic-flow/intelligence.json +16 -0
  2. package/.claude-flow/metrics/agent-metrics.json +1 -0
  3. package/.claude-flow/metrics/performance.json +87 -0
  4. package/.claude-flow/metrics/task-metrics.json +10 -0
  5. package/README.md +1186 -0
  6. package/__tests__/README.md +140 -0
  7. package/__tests__/TEST_SUMMARY.md +144 -0
  8. package/__tests__/cli.test.ts +558 -0
  9. package/__tests__/commands.test.ts +726 -0
  10. package/__tests__/config-adapter.test.ts +362 -0
  11. package/__tests__/config-loading.test.ts +106 -0
  12. package/__tests__/coverage/.tmp/coverage-0.json +1 -0
  13. package/__tests__/coverage/.tmp/coverage-1.json +1 -0
  14. package/__tests__/coverage/.tmp/coverage-2.json +1 -0
  15. package/__tests__/coverage/.tmp/coverage-3.json +1 -0
  16. package/__tests__/coverage/.tmp/coverage-4.json +1 -0
  17. package/__tests__/coverage/.tmp/coverage-5.json +1 -0
  18. package/__tests__/mcp-client.test.ts +480 -0
  19. package/__tests__/p1-commands.test.ts +1064 -0
  20. package/bin/cli.js +14 -0
  21. package/dist/src/commands/agent.d.ts +8 -0
  22. package/dist/src/commands/agent.d.ts.map +1 -0
  23. package/dist/src/commands/agent.js +803 -0
  24. package/dist/src/commands/agent.js.map +1 -0
  25. package/dist/src/commands/config.d.ts +8 -0
  26. package/dist/src/commands/config.d.ts.map +1 -0
  27. package/dist/src/commands/config.js +406 -0
  28. package/dist/src/commands/config.js.map +1 -0
  29. package/dist/src/commands/hive-mind.d.ts +8 -0
  30. package/dist/src/commands/hive-mind.d.ts.map +1 -0
  31. package/dist/src/commands/hive-mind.js +627 -0
  32. package/dist/src/commands/hive-mind.js.map +1 -0
  33. package/dist/src/commands/hooks.d.ts +8 -0
  34. package/dist/src/commands/hooks.d.ts.map +1 -0
  35. package/dist/src/commands/hooks.js +2098 -0
  36. package/dist/src/commands/hooks.js.map +1 -0
  37. package/dist/src/commands/index.d.ts +51 -0
  38. package/dist/src/commands/index.d.ts.map +1 -0
  39. package/dist/src/commands/index.js +105 -0
  40. package/dist/src/commands/index.js.map +1 -0
  41. package/dist/src/commands/init.d.ts +8 -0
  42. package/dist/src/commands/init.d.ts.map +1 -0
  43. package/dist/src/commands/init.js +532 -0
  44. package/dist/src/commands/init.js.map +1 -0
  45. package/dist/src/commands/mcp.d.ts +11 -0
  46. package/dist/src/commands/mcp.d.ts.map +1 -0
  47. package/dist/src/commands/mcp.js +662 -0
  48. package/dist/src/commands/mcp.js.map +1 -0
  49. package/dist/src/commands/memory.d.ts +8 -0
  50. package/dist/src/commands/memory.d.ts.map +1 -0
  51. package/dist/src/commands/memory.js +911 -0
  52. package/dist/src/commands/memory.js.map +1 -0
  53. package/dist/src/commands/migrate.d.ts +8 -0
  54. package/dist/src/commands/migrate.d.ts.map +1 -0
  55. package/dist/src/commands/migrate.js +398 -0
  56. package/dist/src/commands/migrate.js.map +1 -0
  57. package/dist/src/commands/process.d.ts +10 -0
  58. package/dist/src/commands/process.d.ts.map +1 -0
  59. package/dist/src/commands/process.js +566 -0
  60. package/dist/src/commands/process.js.map +1 -0
  61. package/dist/src/commands/session.d.ts +8 -0
  62. package/dist/src/commands/session.d.ts.map +1 -0
  63. package/dist/src/commands/session.js +750 -0
  64. package/dist/src/commands/session.js.map +1 -0
  65. package/dist/src/commands/start.d.ts +8 -0
  66. package/dist/src/commands/start.d.ts.map +1 -0
  67. package/dist/src/commands/start.js +398 -0
  68. package/dist/src/commands/start.js.map +1 -0
  69. package/dist/src/commands/status.d.ts +8 -0
  70. package/dist/src/commands/status.d.ts.map +1 -0
  71. package/dist/src/commands/status.js +560 -0
  72. package/dist/src/commands/status.js.map +1 -0
  73. package/dist/src/commands/swarm.d.ts +8 -0
  74. package/dist/src/commands/swarm.d.ts.map +1 -0
  75. package/dist/src/commands/swarm.js +573 -0
  76. package/dist/src/commands/swarm.js.map +1 -0
  77. package/dist/src/commands/task.d.ts +8 -0
  78. package/dist/src/commands/task.d.ts.map +1 -0
  79. package/dist/src/commands/task.js +671 -0
  80. package/dist/src/commands/task.js.map +1 -0
  81. package/dist/src/commands/workflow.d.ts +8 -0
  82. package/dist/src/commands/workflow.d.ts.map +1 -0
  83. package/dist/src/commands/workflow.js +617 -0
  84. package/dist/src/commands/workflow.js.map +1 -0
  85. package/dist/src/config-adapter.d.ts +15 -0
  86. package/dist/src/config-adapter.d.ts.map +1 -0
  87. package/dist/src/config-adapter.js +185 -0
  88. package/dist/src/config-adapter.js.map +1 -0
  89. package/dist/src/index.d.ts +55 -0
  90. package/dist/src/index.d.ts.map +1 -0
  91. package/dist/src/index.js +312 -0
  92. package/dist/src/index.js.map +1 -0
  93. package/dist/src/infrastructure/in-memory-repositories.d.ts +68 -0
  94. package/dist/src/infrastructure/in-memory-repositories.d.ts.map +1 -0
  95. package/dist/src/infrastructure/in-memory-repositories.js +264 -0
  96. package/dist/src/infrastructure/in-memory-repositories.js.map +1 -0
  97. package/dist/src/init/claudemd-generator.d.ts +15 -0
  98. package/dist/src/init/claudemd-generator.d.ts.map +1 -0
  99. package/dist/src/init/claudemd-generator.js +626 -0
  100. package/dist/src/init/claudemd-generator.js.map +1 -0
  101. package/dist/src/init/executor.d.ts +11 -0
  102. package/dist/src/init/executor.d.ts.map +1 -0
  103. package/dist/src/init/executor.js +647 -0
  104. package/dist/src/init/executor.js.map +1 -0
  105. package/dist/src/init/helpers-generator.d.ts +42 -0
  106. package/dist/src/init/helpers-generator.d.ts.map +1 -0
  107. package/dist/src/init/helpers-generator.js +613 -0
  108. package/dist/src/init/helpers-generator.js.map +1 -0
  109. package/dist/src/init/index.d.ts +12 -0
  110. package/dist/src/init/index.d.ts.map +1 -0
  111. package/dist/src/init/index.js +15 -0
  112. package/dist/src/init/index.js.map +1 -0
  113. package/dist/src/init/mcp-generator.d.ts +18 -0
  114. package/dist/src/init/mcp-generator.d.ts.map +1 -0
  115. package/dist/src/init/mcp-generator.js +71 -0
  116. package/dist/src/init/mcp-generator.js.map +1 -0
  117. package/dist/src/init/settings-generator.d.ts +14 -0
  118. package/dist/src/init/settings-generator.d.ts.map +1 -0
  119. package/dist/src/init/settings-generator.js +257 -0
  120. package/dist/src/init/settings-generator.js.map +1 -0
  121. package/dist/src/init/statusline-generator.d.ts +14 -0
  122. package/dist/src/init/statusline-generator.d.ts.map +1 -0
  123. package/dist/src/init/statusline-generator.js +206 -0
  124. package/dist/src/init/statusline-generator.js.map +1 -0
  125. package/dist/src/init/types.d.ts +240 -0
  126. package/dist/src/init/types.d.ts.map +1 -0
  127. package/dist/src/init/types.js +210 -0
  128. package/dist/src/init/types.js.map +1 -0
  129. package/dist/src/mcp-client.d.ts +92 -0
  130. package/dist/src/mcp-client.d.ts.map +1 -0
  131. package/dist/src/mcp-client.js +189 -0
  132. package/dist/src/mcp-client.js.map +1 -0
  133. package/dist/src/mcp-server.d.ts +153 -0
  134. package/dist/src/mcp-server.d.ts.map +1 -0
  135. package/dist/src/mcp-server.js +448 -0
  136. package/dist/src/mcp-server.js.map +1 -0
  137. package/dist/src/mcp-tools/agent-tools.d.ts +8 -0
  138. package/dist/src/mcp-tools/agent-tools.d.ts.map +1 -0
  139. package/dist/src/mcp-tools/agent-tools.js +90 -0
  140. package/dist/src/mcp-tools/agent-tools.js.map +1 -0
  141. package/dist/src/mcp-tools/config-tools.d.ts +8 -0
  142. package/dist/src/mcp-tools/config-tools.d.ts.map +1 -0
  143. package/dist/src/mcp-tools/config-tools.js +86 -0
  144. package/dist/src/mcp-tools/config-tools.js.map +1 -0
  145. package/dist/src/mcp-tools/hooks-tools.d.ts +41 -0
  146. package/dist/src/mcp-tools/hooks-tools.d.ts.map +1 -0
  147. package/dist/src/mcp-tools/hooks-tools.js +1646 -0
  148. package/dist/src/mcp-tools/hooks-tools.js.map +1 -0
  149. package/dist/src/mcp-tools/index.d.ts +12 -0
  150. package/dist/src/mcp-tools/index.d.ts.map +1 -0
  151. package/dist/src/mcp-tools/index.js +11 -0
  152. package/dist/src/mcp-tools/index.js.map +1 -0
  153. package/dist/src/mcp-tools/memory-tools.d.ts +8 -0
  154. package/dist/src/mcp-tools/memory-tools.d.ts.map +1 -0
  155. package/dist/src/mcp-tools/memory-tools.js +87 -0
  156. package/dist/src/mcp-tools/memory-tools.js.map +1 -0
  157. package/dist/src/mcp-tools/swarm-tools.d.ts +8 -0
  158. package/dist/src/mcp-tools/swarm-tools.d.ts.map +1 -0
  159. package/dist/src/mcp-tools/swarm-tools.js +67 -0
  160. package/dist/src/mcp-tools/swarm-tools.js.map +1 -0
  161. package/dist/src/mcp-tools/types.d.ts +31 -0
  162. package/dist/src/mcp-tools/types.d.ts.map +1 -0
  163. package/dist/src/mcp-tools/types.js +7 -0
  164. package/dist/src/mcp-tools/types.js.map +1 -0
  165. package/dist/src/output.d.ts +117 -0
  166. package/dist/src/output.d.ts.map +1 -0
  167. package/dist/src/output.js +471 -0
  168. package/dist/src/output.js.map +1 -0
  169. package/dist/src/parser.d.ts +41 -0
  170. package/dist/src/parser.d.ts.map +1 -0
  171. package/dist/src/parser.js +353 -0
  172. package/dist/src/parser.js.map +1 -0
  173. package/dist/src/prompt.d.ts +44 -0
  174. package/dist/src/prompt.d.ts.map +1 -0
  175. package/dist/src/prompt.js +501 -0
  176. package/dist/src/prompt.js.map +1 -0
  177. package/dist/src/types.d.ts +198 -0
  178. package/dist/src/types.d.ts.map +1 -0
  179. package/dist/src/types.js +38 -0
  180. package/dist/src/types.js.map +1 -0
  181. package/dist/tsconfig.tsbuildinfo +1 -0
  182. package/docs/CONFIG_LOADING.md +236 -0
  183. package/docs/IMPLEMENTATION_COMPLETE.md +421 -0
  184. package/docs/MCP_CLIENT_GUIDE.md +620 -0
  185. package/docs/REFACTORING_SUMMARY.md +247 -0
  186. package/package.json +29 -0
  187. package/src/commands/agent.ts +941 -0
  188. package/src/commands/config.ts +452 -0
  189. package/src/commands/hive-mind.ts +762 -0
  190. package/src/commands/hooks.ts +2603 -0
  191. package/src/commands/index.ts +115 -0
  192. package/src/commands/init.ts +597 -0
  193. package/src/commands/mcp.ts +753 -0
  194. package/src/commands/memory.ts +1063 -0
  195. package/src/commands/migrate.ts +447 -0
  196. package/src/commands/process.ts +617 -0
  197. package/src/commands/session.ts +891 -0
  198. package/src/commands/start.ts +457 -0
  199. package/src/commands/status.ts +705 -0
  200. package/src/commands/swarm.ts +648 -0
  201. package/src/commands/task.ts +792 -0
  202. package/src/commands/workflow.ts +742 -0
  203. package/src/config-adapter.ts +210 -0
  204. package/src/index.ts +383 -0
  205. package/src/infrastructure/in-memory-repositories.ts +310 -0
  206. package/src/init/claudemd-generator.ts +631 -0
  207. package/src/init/executor.ts +756 -0
  208. package/src/init/helpers-generator.ts +628 -0
  209. package/src/init/index.ts +60 -0
  210. package/src/init/mcp-generator.ts +83 -0
  211. package/src/init/settings-generator.ts +274 -0
  212. package/src/init/statusline-generator.ts +211 -0
  213. package/src/init/types.ts +447 -0
  214. package/src/mcp-client.ts +227 -0
  215. package/src/mcp-server.ts +571 -0
  216. package/src/mcp-tools/agent-tools.ts +92 -0
  217. package/src/mcp-tools/config-tools.ts +88 -0
  218. package/src/mcp-tools/hooks-tools.ts +1819 -0
  219. package/src/mcp-tools/index.ts +12 -0
  220. package/src/mcp-tools/memory-tools.ts +89 -0
  221. package/src/mcp-tools/swarm-tools.ts +69 -0
  222. package/src/mcp-tools/types.ts +33 -0
  223. package/src/output.ts +593 -0
  224. package/src/parser.ts +417 -0
  225. package/src/prompt.ts +619 -0
  226. package/src/types.ts +287 -0
  227. package/tsconfig.json +16 -0
  228. package/tsconfig.tsbuildinfo +1 -0
  229. package/vitest.config.ts +13 -0
@@ -0,0 +1,756 @@
1
+ /**
2
+ * Init Executor
3
+ * Main execution logic for V3 initialization
4
+ */
5
+
6
+ import * as fs from 'fs';
7
+ import * as path from 'path';
8
+ import type { InitOptions, InitResult, PlatformInfo } from './types.js';
9
+ import { detectPlatform } from './types.js';
10
+ import { generateSettingsJson } from './settings-generator.js';
11
+ import { generateMCPJson } from './mcp-generator.js';
12
+ import { generateStatuslineScript, generateStatuslineHook } from './statusline-generator.js';
13
+ import {
14
+ generatePreCommitHook,
15
+ generatePostCommitHook,
16
+ generateSessionManager,
17
+ generateAgentRouter,
18
+ generateMemoryHelper,
19
+ } from './helpers-generator.js';
20
+ import { generateClaudeMd, generateMinimalClaudeMd } from './claudemd-generator.js';
21
+
22
+ /**
23
+ * Skills to copy based on configuration
24
+ */
25
+ const SKILLS_MAP: Record<string, string[]> = {
26
+ core: [
27
+ 'swarm-orchestration',
28
+ 'swarm-advanced',
29
+ 'sparc-methodology',
30
+ 'hooks-automation',
31
+ 'pair-programming',
32
+ 'verification-quality',
33
+ 'stream-chain',
34
+ 'skill-builder',
35
+ ],
36
+ agentdb: [
37
+ 'agentdb-advanced',
38
+ 'agentdb-learning',
39
+ 'agentdb-memory-patterns',
40
+ 'agentdb-optimization',
41
+ 'agentdb-vector-search',
42
+ 'reasoningbank-agentdb',
43
+ 'reasoningbank-intelligence',
44
+ ],
45
+ github: [
46
+ 'github-code-review',
47
+ 'github-multi-repo',
48
+ 'github-project-management',
49
+ 'github-release-management',
50
+ 'github-workflow-automation',
51
+ ],
52
+ flowNexus: [
53
+ 'flow-nexus-neural',
54
+ 'flow-nexus-platform',
55
+ 'flow-nexus-swarm',
56
+ ],
57
+ v3: [
58
+ 'v3-cli-modernization',
59
+ 'v3-core-implementation',
60
+ 'v3-ddd-architecture',
61
+ 'v3-integration-deep',
62
+ 'v3-mcp-optimization',
63
+ 'v3-memory-unification',
64
+ 'v3-performance-optimization',
65
+ 'v3-security-overhaul',
66
+ 'v3-swarm-coordination',
67
+ ],
68
+ };
69
+
70
+ /**
71
+ * Commands to copy based on configuration
72
+ */
73
+ const COMMANDS_MAP: Record<string, string[]> = {
74
+ core: ['claude-flow-help.md', 'claude-flow-swarm.md', 'claude-flow-memory.md'],
75
+ analysis: ['analysis'],
76
+ automation: ['automation'],
77
+ github: ['github'],
78
+ hooks: ['hooks'],
79
+ monitoring: ['monitoring'],
80
+ optimization: ['optimization'],
81
+ sparc: ['sparc'],
82
+ };
83
+
84
+ /**
85
+ * Agents to copy based on configuration
86
+ */
87
+ const AGENTS_MAP: Record<string, string[]> = {
88
+ core: ['core'],
89
+ consensus: ['consensus'],
90
+ github: ['github'],
91
+ hiveMind: ['hive-mind'],
92
+ sparc: ['sparc'],
93
+ swarm: ['swarm'],
94
+ };
95
+
96
+ /**
97
+ * Directory structure to create
98
+ */
99
+ const DIRECTORIES = {
100
+ claude: [
101
+ '.claude',
102
+ '.claude/skills',
103
+ '.claude/commands',
104
+ '.claude/agents',
105
+ '.claude/helpers',
106
+ ],
107
+ runtime: [
108
+ '.claude-flow',
109
+ '.claude-flow/data',
110
+ '.claude-flow/logs',
111
+ '.claude-flow/sessions',
112
+ '.claude-flow/hooks',
113
+ '.claude-flow/agents',
114
+ '.claude-flow/workflows',
115
+ ],
116
+ };
117
+
118
+ /**
119
+ * Execute initialization
120
+ */
121
+ export async function executeInit(options: InitOptions): Promise<InitResult> {
122
+ // Detect platform
123
+ const platform = detectPlatform();
124
+
125
+ const result: InitResult = {
126
+ success: true,
127
+ platform,
128
+ created: {
129
+ directories: [],
130
+ files: [],
131
+ },
132
+ skipped: [],
133
+ errors: [],
134
+ summary: {
135
+ skillsCount: 0,
136
+ commandsCount: 0,
137
+ agentsCount: 0,
138
+ hooksEnabled: 0,
139
+ },
140
+ };
141
+
142
+ const targetDir = options.targetDir;
143
+
144
+ try {
145
+ // Create directory structure
146
+ await createDirectories(targetDir, options, result);
147
+
148
+ // Generate and write settings.json
149
+ if (options.components.settings) {
150
+ await writeSettings(targetDir, options, result);
151
+ }
152
+
153
+ // Generate and write .mcp.json
154
+ if (options.components.mcp) {
155
+ await writeMCPConfig(targetDir, options, result);
156
+ }
157
+
158
+ // Copy skills
159
+ if (options.components.skills) {
160
+ await copySkills(targetDir, options, result);
161
+ }
162
+
163
+ // Copy commands
164
+ if (options.components.commands) {
165
+ await copyCommands(targetDir, options, result);
166
+ }
167
+
168
+ // Copy agents
169
+ if (options.components.agents) {
170
+ await copyAgents(targetDir, options, result);
171
+ }
172
+
173
+ // Generate helpers
174
+ if (options.components.helpers) {
175
+ await writeHelpers(targetDir, options, result);
176
+ }
177
+
178
+ // Generate statusline
179
+ if (options.components.statusline) {
180
+ await writeStatusline(targetDir, options, result);
181
+ }
182
+
183
+ // Generate runtime config
184
+ if (options.components.runtime) {
185
+ await writeRuntimeConfig(targetDir, options, result);
186
+ }
187
+
188
+ // Generate CLAUDE.md
189
+ if (options.components.claudeMd) {
190
+ await writeClaudeMd(targetDir, options, result);
191
+ }
192
+
193
+ // Count enabled hooks
194
+ result.summary.hooksEnabled = countEnabledHooks(options);
195
+
196
+ } catch (error) {
197
+ result.success = false;
198
+ result.errors.push(error instanceof Error ? error.message : String(error));
199
+ }
200
+
201
+ return result;
202
+ }
203
+
204
+ /**
205
+ * Create directory structure
206
+ */
207
+ async function createDirectories(
208
+ targetDir: string,
209
+ options: InitOptions,
210
+ result: InitResult
211
+ ): Promise<void> {
212
+ const dirs = [
213
+ ...DIRECTORIES.claude,
214
+ ...(options.components.runtime ? DIRECTORIES.runtime : []),
215
+ ];
216
+
217
+ for (const dir of dirs) {
218
+ const fullPath = path.join(targetDir, dir);
219
+ if (!fs.existsSync(fullPath)) {
220
+ fs.mkdirSync(fullPath, { recursive: true });
221
+ result.created.directories.push(dir);
222
+ }
223
+ }
224
+ }
225
+
226
+ /**
227
+ * Write settings.json
228
+ */
229
+ async function writeSettings(
230
+ targetDir: string,
231
+ options: InitOptions,
232
+ result: InitResult
233
+ ): Promise<void> {
234
+ const settingsPath = path.join(targetDir, '.claude', 'settings.json');
235
+
236
+ if (fs.existsSync(settingsPath) && !options.force) {
237
+ result.skipped.push('.claude/settings.json');
238
+ return;
239
+ }
240
+
241
+ const content = generateSettingsJson(options);
242
+ fs.writeFileSync(settingsPath, content, 'utf-8');
243
+ result.created.files.push('.claude/settings.json');
244
+ }
245
+
246
+ /**
247
+ * Write .mcp.json
248
+ */
249
+ async function writeMCPConfig(
250
+ targetDir: string,
251
+ options: InitOptions,
252
+ result: InitResult
253
+ ): Promise<void> {
254
+ const mcpPath = path.join(targetDir, '.mcp.json');
255
+
256
+ if (fs.existsSync(mcpPath) && !options.force) {
257
+ result.skipped.push('.mcp.json');
258
+ return;
259
+ }
260
+
261
+ const content = generateMCPJson(options);
262
+ fs.writeFileSync(mcpPath, content, 'utf-8');
263
+ result.created.files.push('.mcp.json');
264
+ }
265
+
266
+ /**
267
+ * Copy skills from source
268
+ */
269
+ async function copySkills(
270
+ targetDir: string,
271
+ options: InitOptions,
272
+ result: InitResult
273
+ ): Promise<void> {
274
+ const skillsConfig = options.skills;
275
+ const targetSkillsDir = path.join(targetDir, '.claude', 'skills');
276
+
277
+ // Determine which skills to copy
278
+ const skillsToCopy: string[] = [];
279
+
280
+ if (skillsConfig.all) {
281
+ // Copy all available skills
282
+ Object.values(SKILLS_MAP).forEach(skills => skillsToCopy.push(...skills));
283
+ } else {
284
+ if (skillsConfig.core) skillsToCopy.push(...SKILLS_MAP.core);
285
+ if (skillsConfig.agentdb) skillsToCopy.push(...SKILLS_MAP.agentdb);
286
+ if (skillsConfig.github) skillsToCopy.push(...SKILLS_MAP.github);
287
+ if (skillsConfig.flowNexus) skillsToCopy.push(...SKILLS_MAP.flowNexus);
288
+ if (skillsConfig.v3) skillsToCopy.push(...SKILLS_MAP.v3);
289
+ }
290
+
291
+ // Find source skills directory
292
+ const sourceSkillsDir = findSourceDir('skills', options.sourceBaseDir);
293
+ if (!sourceSkillsDir) {
294
+ result.errors.push('Could not find source skills directory');
295
+ return;
296
+ }
297
+
298
+ // Copy each skill
299
+ for (const skillName of [...new Set(skillsToCopy)]) {
300
+ const sourcePath = path.join(sourceSkillsDir, skillName);
301
+ const targetPath = path.join(targetSkillsDir, skillName);
302
+
303
+ if (fs.existsSync(sourcePath)) {
304
+ if (!fs.existsSync(targetPath) || options.force) {
305
+ copyDirRecursive(sourcePath, targetPath);
306
+ result.created.files.push(`.claude/skills/${skillName}`);
307
+ result.summary.skillsCount++;
308
+ } else {
309
+ result.skipped.push(`.claude/skills/${skillName}`);
310
+ }
311
+ }
312
+ }
313
+ }
314
+
315
+ /**
316
+ * Copy commands from source
317
+ */
318
+ async function copyCommands(
319
+ targetDir: string,
320
+ options: InitOptions,
321
+ result: InitResult
322
+ ): Promise<void> {
323
+ const commandsConfig = options.commands;
324
+ const targetCommandsDir = path.join(targetDir, '.claude', 'commands');
325
+
326
+ // Determine which commands to copy
327
+ const commandsToCopy: string[] = [];
328
+
329
+ if (commandsConfig.all) {
330
+ Object.values(COMMANDS_MAP).forEach(cmds => commandsToCopy.push(...cmds));
331
+ } else {
332
+ if (commandsConfig.core) commandsToCopy.push(...COMMANDS_MAP.core);
333
+ if (commandsConfig.analysis) commandsToCopy.push(...COMMANDS_MAP.analysis);
334
+ if (commandsConfig.automation) commandsToCopy.push(...COMMANDS_MAP.automation);
335
+ if (commandsConfig.github) commandsToCopy.push(...COMMANDS_MAP.github);
336
+ if (commandsConfig.hooks) commandsToCopy.push(...COMMANDS_MAP.hooks);
337
+ if (commandsConfig.monitoring) commandsToCopy.push(...COMMANDS_MAP.monitoring);
338
+ if (commandsConfig.optimization) commandsToCopy.push(...COMMANDS_MAP.optimization);
339
+ if (commandsConfig.sparc) commandsToCopy.push(...COMMANDS_MAP.sparc);
340
+ }
341
+
342
+ // Find source commands directory
343
+ const sourceCommandsDir = findSourceDir('commands', options.sourceBaseDir);
344
+ if (!sourceCommandsDir) {
345
+ result.errors.push('Could not find source commands directory');
346
+ return;
347
+ }
348
+
349
+ // Copy each command/directory
350
+ for (const cmdName of [...new Set(commandsToCopy)]) {
351
+ const sourcePath = path.join(sourceCommandsDir, cmdName);
352
+ const targetPath = path.join(targetCommandsDir, cmdName);
353
+
354
+ if (fs.existsSync(sourcePath)) {
355
+ if (!fs.existsSync(targetPath) || options.force) {
356
+ if (fs.statSync(sourcePath).isDirectory()) {
357
+ copyDirRecursive(sourcePath, targetPath);
358
+ } else {
359
+ fs.copyFileSync(sourcePath, targetPath);
360
+ }
361
+ result.created.files.push(`.claude/commands/${cmdName}`);
362
+ result.summary.commandsCount++;
363
+ } else {
364
+ result.skipped.push(`.claude/commands/${cmdName}`);
365
+ }
366
+ }
367
+ }
368
+ }
369
+
370
+ /**
371
+ * Copy agents from source
372
+ */
373
+ async function copyAgents(
374
+ targetDir: string,
375
+ options: InitOptions,
376
+ result: InitResult
377
+ ): Promise<void> {
378
+ const agentsConfig = options.agents;
379
+ const targetAgentsDir = path.join(targetDir, '.claude', 'agents');
380
+
381
+ // Determine which agents to copy
382
+ const agentsToCopy: string[] = [];
383
+
384
+ if (agentsConfig.all) {
385
+ Object.values(AGENTS_MAP).forEach(agents => agentsToCopy.push(...agents));
386
+ } else {
387
+ if (agentsConfig.core) agentsToCopy.push(...AGENTS_MAP.core);
388
+ if (agentsConfig.consensus) agentsToCopy.push(...AGENTS_MAP.consensus);
389
+ if (agentsConfig.github) agentsToCopy.push(...AGENTS_MAP.github);
390
+ if (agentsConfig.hiveMind) agentsToCopy.push(...AGENTS_MAP.hiveMind);
391
+ if (agentsConfig.sparc) agentsToCopy.push(...AGENTS_MAP.sparc);
392
+ if (agentsConfig.swarm) agentsToCopy.push(...AGENTS_MAP.swarm);
393
+ }
394
+
395
+ // Find source agents directory
396
+ const sourceAgentsDir = findSourceDir('agents', options.sourceBaseDir);
397
+ if (!sourceAgentsDir) {
398
+ result.errors.push('Could not find source agents directory');
399
+ return;
400
+ }
401
+
402
+ // Copy each agent category
403
+ for (const agentCategory of [...new Set(agentsToCopy)]) {
404
+ const sourcePath = path.join(sourceAgentsDir, agentCategory);
405
+ const targetPath = path.join(targetAgentsDir, agentCategory);
406
+
407
+ if (fs.existsSync(sourcePath)) {
408
+ if (!fs.existsSync(targetPath) || options.force) {
409
+ copyDirRecursive(sourcePath, targetPath);
410
+ // Count agent files
411
+ const agentFiles = countFiles(sourcePath, '.md');
412
+ result.summary.agentsCount += agentFiles;
413
+ result.created.files.push(`.claude/agents/${agentCategory}`);
414
+ } else {
415
+ result.skipped.push(`.claude/agents/${agentCategory}`);
416
+ }
417
+ }
418
+ }
419
+ }
420
+
421
+ /**
422
+ * Write helper scripts
423
+ */
424
+ async function writeHelpers(
425
+ targetDir: string,
426
+ options: InitOptions,
427
+ result: InitResult
428
+ ): Promise<void> {
429
+ const helpersDir = path.join(targetDir, '.claude', 'helpers');
430
+ const sourceBaseDir = options.sourceBaseDir;
431
+
432
+ // Try to copy existing helpers from source first
433
+ if (sourceBaseDir) {
434
+ const sourceHelpersDir = path.join(sourceBaseDir, '.claude', 'helpers');
435
+ if (fs.existsSync(sourceHelpersDir)) {
436
+ const helperFiles = fs.readdirSync(sourceHelpersDir);
437
+ for (const file of helperFiles) {
438
+ const sourcePath = path.join(sourceHelpersDir, file);
439
+ const destPath = path.join(helpersDir, file);
440
+
441
+ // Skip directories and only copy files
442
+ if (!fs.statSync(sourcePath).isFile()) continue;
443
+
444
+ if (!fs.existsSync(destPath) || options.force) {
445
+ fs.copyFileSync(sourcePath, destPath);
446
+
447
+ // Make shell scripts executable
448
+ if (file.endsWith('.sh')) {
449
+ fs.chmodSync(destPath, '755');
450
+ }
451
+
452
+ result.created.files.push(`.claude/helpers/${file}`);
453
+ } else {
454
+ result.skipped.push(`.claude/helpers/${file}`);
455
+ }
456
+ }
457
+ return; // Skip generating if we copied from source
458
+ }
459
+ }
460
+
461
+ // Fall back to generating helpers if source not available
462
+ const helpers: Record<string, string> = {
463
+ 'pre-commit': generatePreCommitHook(),
464
+ 'post-commit': generatePostCommitHook(),
465
+ 'session.js': generateSessionManager(),
466
+ 'router.js': generateAgentRouter(),
467
+ 'memory.js': generateMemoryHelper(),
468
+ };
469
+
470
+ for (const [name, content] of Object.entries(helpers)) {
471
+ const filePath = path.join(helpersDir, name);
472
+
473
+ if (!fs.existsSync(filePath) || options.force) {
474
+ fs.writeFileSync(filePath, content, 'utf-8');
475
+
476
+ // Make shell scripts executable
477
+ if (!name.endsWith('.js')) {
478
+ fs.chmodSync(filePath, '755');
479
+ }
480
+
481
+ result.created.files.push(`.claude/helpers/${name}`);
482
+ } else {
483
+ result.skipped.push(`.claude/helpers/${name}`);
484
+ }
485
+ }
486
+ }
487
+
488
+ /**
489
+ * Write statusline configuration
490
+ */
491
+ async function writeStatusline(
492
+ targetDir: string,
493
+ options: InitOptions,
494
+ result: InitResult
495
+ ): Promise<void> {
496
+ const claudeDir = path.join(targetDir, '.claude');
497
+ const helpersDir = path.join(targetDir, '.claude', 'helpers');
498
+
499
+ // Try to copy existing advanced statusline files from source
500
+ const sourceBaseDir = options.sourceBaseDir;
501
+ const advancedStatuslineFiles = [
502
+ { src: 'statusline.sh', dest: 'statusline.sh', dir: claudeDir },
503
+ { src: 'statusline.mjs', dest: 'statusline.mjs', dir: claudeDir },
504
+ ];
505
+
506
+ let copiedAdvanced = false;
507
+ if (sourceBaseDir) {
508
+ for (const file of advancedStatuslineFiles) {
509
+ const sourcePath = path.join(sourceBaseDir, '.claude', file.src);
510
+ const destPath = path.join(file.dir, file.dest);
511
+
512
+ if (fs.existsSync(sourcePath)) {
513
+ if (!fs.existsSync(destPath) || options.force) {
514
+ fs.copyFileSync(sourcePath, destPath);
515
+ // Make shell scripts executable
516
+ if (file.src.endsWith('.sh')) {
517
+ fs.chmodSync(destPath, '755');
518
+ }
519
+ result.created.files.push(`.claude/${file.dest}`);
520
+ copiedAdvanced = true;
521
+ } else {
522
+ result.skipped.push(`.claude/${file.dest}`);
523
+ }
524
+ }
525
+ }
526
+ }
527
+
528
+ // Fall back to generating simple statusline if advanced files not available
529
+ if (!copiedAdvanced) {
530
+ const statuslineScript = generateStatuslineScript(options);
531
+ const statuslineHook = generateStatuslineHook(options);
532
+
533
+ const files: Record<string, string> = {
534
+ 'statusline.js': statuslineScript,
535
+ 'statusline-hook.sh': statuslineHook,
536
+ };
537
+
538
+ for (const [name, content] of Object.entries(files)) {
539
+ const filePath = path.join(helpersDir, name);
540
+
541
+ if (!fs.existsSync(filePath) || options.force) {
542
+ fs.writeFileSync(filePath, content, 'utf-8');
543
+ result.created.files.push(`.claude/helpers/${name}`);
544
+ } else {
545
+ result.skipped.push(`.claude/helpers/${name}`);
546
+ }
547
+ }
548
+ }
549
+ }
550
+
551
+ /**
552
+ * Write runtime configuration (.claude-flow/)
553
+ */
554
+ async function writeRuntimeConfig(
555
+ targetDir: string,
556
+ options: InitOptions,
557
+ result: InitResult
558
+ ): Promise<void> {
559
+ const configPath = path.join(targetDir, '.claude-flow', 'config.yaml');
560
+
561
+ if (fs.existsSync(configPath) && !options.force) {
562
+ result.skipped.push('.claude-flow/config.yaml');
563
+ return;
564
+ }
565
+
566
+ const config = `# Claude Flow V3 Runtime Configuration
567
+ # Generated: ${new Date().toISOString()}
568
+
569
+ version: "3.0.0"
570
+
571
+ swarm:
572
+ topology: ${options.runtime.topology}
573
+ maxAgents: ${options.runtime.maxAgents}
574
+ autoScale: true
575
+ coordinationStrategy: consensus
576
+
577
+ memory:
578
+ backend: ${options.runtime.memoryBackend}
579
+ enableHNSW: ${options.runtime.enableHNSW}
580
+ persistPath: .claude-flow/data
581
+ cacheSize: 100
582
+
583
+ neural:
584
+ enabled: ${options.runtime.enableNeural}
585
+ modelPath: .claude-flow/neural
586
+
587
+ hooks:
588
+ enabled: true
589
+ autoExecute: true
590
+
591
+ mcp:
592
+ autoStart: ${options.mcp.autoStart}
593
+ port: ${options.mcp.port}
594
+ `;
595
+
596
+ fs.writeFileSync(configPath, config, 'utf-8');
597
+ result.created.files.push('.claude-flow/config.yaml');
598
+
599
+ // Write .gitignore
600
+ const gitignorePath = path.join(targetDir, '.claude-flow', '.gitignore');
601
+ const gitignore = `# Claude Flow runtime files
602
+ data/
603
+ logs/
604
+ sessions/
605
+ neural/
606
+ *.log
607
+ *.tmp
608
+ `;
609
+
610
+ if (!fs.existsSync(gitignorePath) || options.force) {
611
+ fs.writeFileSync(gitignorePath, gitignore, 'utf-8');
612
+ result.created.files.push('.claude-flow/.gitignore');
613
+ }
614
+ }
615
+
616
+ /**
617
+ * Write CLAUDE.md with swarm guidance
618
+ */
619
+ async function writeClaudeMd(
620
+ targetDir: string,
621
+ options: InitOptions,
622
+ result: InitResult
623
+ ): Promise<void> {
624
+ const claudeMdPath = path.join(targetDir, 'CLAUDE.md');
625
+
626
+ if (fs.existsSync(claudeMdPath) && !options.force) {
627
+ result.skipped.push('CLAUDE.md');
628
+ return;
629
+ }
630
+
631
+ // Use minimal version for minimal init, full version otherwise
632
+ const isMinimal = !options.components.commands && !options.components.agents;
633
+ const content = isMinimal ? generateMinimalClaudeMd(options) : generateClaudeMd(options);
634
+
635
+ fs.writeFileSync(claudeMdPath, content, 'utf-8');
636
+ result.created.files.push('CLAUDE.md');
637
+ }
638
+
639
+ /**
640
+ * Find source directory for skills/commands/agents
641
+ */
642
+ function findSourceDir(type: 'skills' | 'commands' | 'agents', sourceBaseDir?: string): string | null {
643
+ // Build list of possible paths to check
644
+ const possiblePaths: string[] = [];
645
+
646
+ // If explicit source base directory is provided, use it first
647
+ if (sourceBaseDir) {
648
+ possiblePaths.push(path.join(sourceBaseDir, '.claude', type));
649
+ }
650
+
651
+ // From dist/src/init -> go up to project root
652
+ // __dirname is typically /path/to/v3/@claude-flow/cli/dist/src/init
653
+ const distPath = __dirname;
654
+
655
+ // Try to find the project root by looking for .claude directory
656
+ let currentDir = distPath;
657
+ for (let i = 0; i < 10; i++) {
658
+ const parentDir = path.dirname(currentDir);
659
+ const dotClaudePath = path.join(parentDir, '.claude', type);
660
+ if (fs.existsSync(dotClaudePath)) {
661
+ possiblePaths.push(dotClaudePath);
662
+ }
663
+ currentDir = parentDir;
664
+ }
665
+
666
+ // Also check relative to process.cwd() for development
667
+ const cwdBased = [
668
+ path.join(process.cwd(), '.claude', type),
669
+ path.join(process.cwd(), '..', '.claude', type),
670
+ path.join(process.cwd(), '..', '..', '.claude', type),
671
+ ];
672
+ possiblePaths.push(...cwdBased);
673
+
674
+ // Check v2 directory for agents
675
+ if (type === 'agents') {
676
+ possiblePaths.push(
677
+ path.join(process.cwd(), 'v2', '.claude', type),
678
+ path.join(process.cwd(), '..', 'v2', '.claude', type),
679
+ );
680
+ }
681
+
682
+ // Plugin directory
683
+ possiblePaths.push(
684
+ path.join(process.cwd(), 'plugin', type),
685
+ path.join(process.cwd(), '..', 'plugin', type),
686
+ );
687
+
688
+ for (const p of possiblePaths) {
689
+ if (fs.existsSync(p)) {
690
+ return p;
691
+ }
692
+ }
693
+
694
+ return null;
695
+ }
696
+
697
+ /**
698
+ * Copy directory recursively
699
+ */
700
+ function copyDirRecursive(src: string, dest: string): void {
701
+ fs.mkdirSync(dest, { recursive: true });
702
+
703
+ const entries = fs.readdirSync(src, { withFileTypes: true });
704
+
705
+ for (const entry of entries) {
706
+ const srcPath = path.join(src, entry.name);
707
+ const destPath = path.join(dest, entry.name);
708
+
709
+ if (entry.isDirectory()) {
710
+ copyDirRecursive(srcPath, destPath);
711
+ } else {
712
+ fs.copyFileSync(srcPath, destPath);
713
+ }
714
+ }
715
+ }
716
+
717
+ /**
718
+ * Count files with extension in directory
719
+ */
720
+ function countFiles(dir: string, ext: string): number {
721
+ let count = 0;
722
+
723
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
724
+
725
+ for (const entry of entries) {
726
+ const fullPath = path.join(dir, entry.name);
727
+
728
+ if (entry.isDirectory()) {
729
+ count += countFiles(fullPath, ext);
730
+ } else if (entry.name.endsWith(ext)) {
731
+ count++;
732
+ }
733
+ }
734
+
735
+ return count;
736
+ }
737
+
738
+ /**
739
+ * Count enabled hooks
740
+ */
741
+ function countEnabledHooks(options: InitOptions): number {
742
+ const hooks = options.hooks;
743
+ let count = 0;
744
+
745
+ if (hooks.preToolUse) count++;
746
+ if (hooks.postToolUse) count++;
747
+ if (hooks.userPromptSubmit) count++;
748
+ if (hooks.sessionStart) count++;
749
+ if (hooks.stop) count++;
750
+ if (hooks.notification) count++;
751
+ if (hooks.permissionRequest) count++;
752
+
753
+ return count;
754
+ }
755
+
756
+ export default executeInit;