@crewx/cli 0.8.0-rc.66

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 (219) hide show
  1. package/README.md +663 -0
  2. package/dist/ai-provider.service.d.ts +36 -0
  3. package/dist/ai-provider.service.js +315 -0
  4. package/dist/ai-provider.service.js.map +1 -0
  5. package/dist/ai.service.d.ts +17 -0
  6. package/dist/ai.service.js +51 -0
  7. package/dist/ai.service.js.map +1 -0
  8. package/dist/app.module.d.ts +5 -0
  9. package/dist/app.module.js +165 -0
  10. package/dist/app.module.js.map +1 -0
  11. package/dist/cli/agent.handler.d.ts +2 -0
  12. package/dist/cli/agent.handler.js +186 -0
  13. package/dist/cli/agent.handler.js.map +1 -0
  14. package/dist/cli/builtin.handler.d.ts +3 -0
  15. package/dist/cli/builtin.handler.js +110 -0
  16. package/dist/cli/builtin.handler.js.map +1 -0
  17. package/dist/cli/chat.handler.d.ts +20 -0
  18. package/dist/cli/chat.handler.js +446 -0
  19. package/dist/cli/chat.handler.js.map +1 -0
  20. package/dist/cli/cli.handler.d.ts +4 -0
  21. package/dist/cli/cli.handler.js +119 -0
  22. package/dist/cli/cli.handler.js.map +1 -0
  23. package/dist/cli/doctor.handler.d.ts +38 -0
  24. package/dist/cli/doctor.handler.js +495 -0
  25. package/dist/cli/doctor.handler.js.map +1 -0
  26. package/dist/cli/execute.handler.d.ts +2 -0
  27. package/dist/cli/execute.handler.js +376 -0
  28. package/dist/cli/execute.handler.js.map +1 -0
  29. package/dist/cli/help.handler.d.ts +2 -0
  30. package/dist/cli/help.handler.js +10 -0
  31. package/dist/cli/help.handler.js.map +1 -0
  32. package/dist/cli/init.handler.d.ts +26 -0
  33. package/dist/cli/init.handler.js +450 -0
  34. package/dist/cli/init.handler.js.map +1 -0
  35. package/dist/cli/log.handler.d.ts +2 -0
  36. package/dist/cli/log.handler.js +69 -0
  37. package/dist/cli/log.handler.js.map +1 -0
  38. package/dist/cli/mcp.handler.d.ts +3 -0
  39. package/dist/cli/mcp.handler.js +121 -0
  40. package/dist/cli/mcp.handler.js.map +1 -0
  41. package/dist/cli/query.handler.d.ts +2 -0
  42. package/dist/cli/query.handler.js +392 -0
  43. package/dist/cli/query.handler.js.map +1 -0
  44. package/dist/cli/skill.handler.d.ts +2 -0
  45. package/dist/cli/skill.handler.js +252 -0
  46. package/dist/cli/skill.handler.js.map +1 -0
  47. package/dist/cli/slack-files.handler.d.ts +2 -0
  48. package/dist/cli/slack-files.handler.js +291 -0
  49. package/dist/cli/slack-files.handler.js.map +1 -0
  50. package/dist/cli/template.handler.d.ts +2 -0
  51. package/dist/cli/template.handler.js +188 -0
  52. package/dist/cli/template.handler.js.map +1 -0
  53. package/dist/cli/templates.handler.d.ts +2 -0
  54. package/dist/cli/templates.handler.js +100 -0
  55. package/dist/cli/templates.handler.js.map +1 -0
  56. package/dist/cli-options.d.ts +40 -0
  57. package/dist/cli-options.js +371 -0
  58. package/dist/cli-options.js.map +1 -0
  59. package/dist/config/timeout.config.d.ts +14 -0
  60. package/dist/config/timeout.config.js +34 -0
  61. package/dist/config/timeout.config.js.map +1 -0
  62. package/dist/conversation/base-conversation-history.provider.d.ts +12 -0
  63. package/dist/conversation/base-conversation-history.provider.js +45 -0
  64. package/dist/conversation/base-conversation-history.provider.js.map +1 -0
  65. package/dist/conversation/cli-box-reader.adapter.d.ts +6 -0
  66. package/dist/conversation/cli-box-reader.adapter.js +10 -0
  67. package/dist/conversation/cli-box-reader.adapter.js.map +1 -0
  68. package/dist/conversation/cli-conversation-history.provider.d.ts +16 -0
  69. package/dist/conversation/cli-conversation-history.provider.js +112 -0
  70. package/dist/conversation/cli-conversation-history.provider.js.map +1 -0
  71. package/dist/conversation/cli-task-reader.adapter.d.ts +6 -0
  72. package/dist/conversation/cli-task-reader.adapter.js +25 -0
  73. package/dist/conversation/cli-task-reader.adapter.js.map +1 -0
  74. package/dist/conversation/conversation-provider.factory.d.ts +10 -0
  75. package/dist/conversation/conversation-provider.factory.js +50 -0
  76. package/dist/conversation/conversation-provider.factory.js.map +1 -0
  77. package/dist/conversation/index.d.ts +8 -0
  78. package/dist/conversation/index.js +29 -0
  79. package/dist/conversation/index.js.map +1 -0
  80. package/dist/conversation/slack-conversation-history.provider.d.ts +29 -0
  81. package/dist/conversation/slack-conversation-history.provider.js +302 -0
  82. package/dist/conversation/slack-conversation-history.provider.js.map +1 -0
  83. package/dist/crewx.tool.d.ts +360 -0
  84. package/dist/crewx.tool.js +2531 -0
  85. package/dist/crewx.tool.js.map +1 -0
  86. package/dist/crewx.tool.spec.d.ts +1 -0
  87. package/dist/crewx.tool.spec.js +222 -0
  88. package/dist/crewx.tool.spec.js.map +1 -0
  89. package/dist/guards/bearer-auth.guard.d.ts +7 -0
  90. package/dist/guards/bearer-auth.guard.js +44 -0
  91. package/dist/guards/bearer-auth.guard.js.map +1 -0
  92. package/dist/health.controller.d.ts +6 -0
  93. package/dist/health.controller.js +32 -0
  94. package/dist/health.controller.js.map +1 -0
  95. package/dist/main.d.ts +1 -0
  96. package/dist/main.js +311 -0
  97. package/dist/main.js.map +1 -0
  98. package/dist/mcp.controller.d.ts +8 -0
  99. package/dist/mcp.controller.js +62 -0
  100. package/dist/mcp.controller.js.map +1 -0
  101. package/dist/package.json +3 -0
  102. package/dist/providers/dynamic-provider.factory.d.ts +17 -0
  103. package/dist/providers/dynamic-provider.factory.js +138 -0
  104. package/dist/providers/dynamic-provider.factory.js.map +1 -0
  105. package/dist/providers/logger.adapter.d.ts +7 -0
  106. package/dist/providers/logger.adapter.js +107 -0
  107. package/dist/providers/logger.adapter.js.map +1 -0
  108. package/dist/services/agent-loader.service.d.ts +35 -0
  109. package/dist/services/agent-loader.service.js +623 -0
  110. package/dist/services/agent-loader.service.js.map +1 -0
  111. package/dist/services/auth.service.d.ts +9 -0
  112. package/dist/services/auth.service.js +47 -0
  113. package/dist/services/auth.service.js.map +1 -0
  114. package/dist/services/config-validator.service.d.ts +29 -0
  115. package/dist/services/config-validator.service.js +483 -0
  116. package/dist/services/config-validator.service.js.map +1 -0
  117. package/dist/services/config.service.d.ts +45 -0
  118. package/dist/services/config.service.js +352 -0
  119. package/dist/services/config.service.js.map +1 -0
  120. package/dist/services/document-loader.service.d.ts +26 -0
  121. package/dist/services/document-loader.service.js +186 -0
  122. package/dist/services/document-loader.service.js.map +1 -0
  123. package/dist/services/help.service.d.ts +5 -0
  124. package/dist/services/help.service.js +139 -0
  125. package/dist/services/help.service.js.map +1 -0
  126. package/dist/services/intelligent-compression.service.d.ts +20 -0
  127. package/dist/services/intelligent-compression.service.js +179 -0
  128. package/dist/services/intelligent-compression.service.js.map +1 -0
  129. package/dist/services/mcp-client.service.d.ts +26 -0
  130. package/dist/services/mcp-client.service.js +81 -0
  131. package/dist/services/mcp-client.service.js.map +1 -0
  132. package/dist/services/parallel-processing.service.d.ts +108 -0
  133. package/dist/services/parallel-processing.service.js +333 -0
  134. package/dist/services/parallel-processing.service.js.map +1 -0
  135. package/dist/services/provider-bridge.service.d.ts +35 -0
  136. package/dist/services/provider-bridge.service.js +224 -0
  137. package/dist/services/provider-bridge.service.js.map +1 -0
  138. package/dist/services/remote-agent.service.d.ts +50 -0
  139. package/dist/services/remote-agent.service.js +171 -0
  140. package/dist/services/remote-agent.service.js.map +1 -0
  141. package/dist/services/result-formatter.service.d.ts +27 -0
  142. package/dist/services/result-formatter.service.js +126 -0
  143. package/dist/services/result-formatter.service.js.map +1 -0
  144. package/dist/services/skill-loader.service.d.ts +15 -0
  145. package/dist/services/skill-loader.service.js +278 -0
  146. package/dist/services/skill-loader.service.js.map +1 -0
  147. package/dist/services/skill.service.d.ts +69 -0
  148. package/dist/services/skill.service.js +779 -0
  149. package/dist/services/skill.service.js.map +1 -0
  150. package/dist/services/skill.service.spec.d.ts +1 -0
  151. package/dist/services/skill.service.spec.js +168 -0
  152. package/dist/services/skill.service.spec.js.map +1 -0
  153. package/dist/services/task-management.service.d.ts +71 -0
  154. package/dist/services/task-management.service.js +324 -0
  155. package/dist/services/task-management.service.js.map +1 -0
  156. package/dist/services/template.service.d.ts +61 -0
  157. package/dist/services/template.service.js +416 -0
  158. package/dist/services/template.service.js.map +1 -0
  159. package/dist/services/tool-call.service.d.ts +16 -0
  160. package/dist/services/tool-call.service.js +302 -0
  161. package/dist/services/tool-call.service.js.map +1 -0
  162. package/dist/services/tracing.service.d.ts +197 -0
  163. package/dist/services/tracing.service.js +1267 -0
  164. package/dist/services/tracing.service.js.map +1 -0
  165. package/dist/slack/formatters/message.formatter.d.ts +43 -0
  166. package/dist/slack/formatters/message.formatter.js +505 -0
  167. package/dist/slack/formatters/message.formatter.js.map +1 -0
  168. package/dist/slack/services/slack-file-download.service.d.ts +58 -0
  169. package/dist/slack/services/slack-file-download.service.js +558 -0
  170. package/dist/slack/services/slack-file-download.service.js.map +1 -0
  171. package/dist/slack/slack-bot.d.ts +33 -0
  172. package/dist/slack/slack-bot.js +568 -0
  173. package/dist/slack/slack-bot.js.map +1 -0
  174. package/dist/stderr.logger.d.ts +8 -0
  175. package/dist/stderr.logger.js +26 -0
  176. package/dist/stderr.logger.js.map +1 -0
  177. package/dist/types/usage.types.d.ts +107 -0
  178. package/dist/types/usage.types.js +3 -0
  179. package/dist/types/usage.types.js.map +1 -0
  180. package/dist/utils/config-utils.d.ts +15 -0
  181. package/dist/utils/config-utils.js +69 -0
  182. package/dist/utils/config-utils.js.map +1 -0
  183. package/dist/utils/extract-text.d.ts +1 -0
  184. package/dist/utils/extract-text.js +15 -0
  185. package/dist/utils/extract-text.js.map +1 -0
  186. package/dist/utils/mcp-installer.d.ts +20 -0
  187. package/dist/utils/mcp-installer.js +199 -0
  188. package/dist/utils/mcp-installer.js.map +1 -0
  189. package/dist/utils/project-hash.d.ts +6 -0
  190. package/dist/utils/project-hash.js +70 -0
  191. package/dist/utils/project-hash.js.map +1 -0
  192. package/dist/utils/simple-security.d.ts +3 -0
  193. package/dist/utils/simple-security.js +20 -0
  194. package/dist/utils/simple-security.js.map +1 -0
  195. package/dist/utils/stdin-utils.d.ts +6 -0
  196. package/dist/utils/stdin-utils.js +109 -0
  197. package/dist/utils/stdin-utils.js.map +1 -0
  198. package/dist/utils/template-processor.d.ts +27 -0
  199. package/dist/utils/template-processor.js +395 -0
  200. package/dist/utils/template-processor.js.map +1 -0
  201. package/dist/utils/terminal-message-formatter.d.ts +23 -0
  202. package/dist/utils/terminal-message-formatter.js +136 -0
  203. package/dist/utils/terminal-message-formatter.js.map +1 -0
  204. package/dist/version.d.ts +1 -0
  205. package/dist/version.js +17 -0
  206. package/dist/version.js.map +1 -0
  207. package/dist/workspace.service.d.ts +44 -0
  208. package/dist/workspace.service.js +299 -0
  209. package/dist/workspace.service.js.map +1 -0
  210. package/package.json +135 -0
  211. package/scripts/backfill-tokens.js +218 -0
  212. package/scripts/postbuild-cli.mjs +88 -0
  213. package/scripts/postinstall-cli.mjs +30 -0
  214. package/templates/agents/default.yaml +490 -0
  215. package/templates/agents/minimal.yaml +16 -0
  216. package/templates/documents/conversation-history-default.hbs +17 -0
  217. package/templates/documents/crewx-manual.md +2278 -0
  218. package/templates/documents/crewx-quick-guide.md +147 -0
  219. package/templates/versions.json +19 -0
@@ -0,0 +1,218 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * backfill-tokens.js
4
+ *
5
+ * One-time migration script to backfill input_tokens, output_tokens, cost_usd
6
+ * for existing tasks whose logs already contain usage data but were recorded
7
+ * before extractUsageFromLogs() was introduced.
8
+ *
9
+ * Usage:
10
+ * node packages/cli/scripts/backfill-tokens.js [--dry-run] [db-path]
11
+ *
12
+ * DB resolution order:
13
+ * 1. Positional argument
14
+ * 2. CREWX_DB env var (CREWX_TRACES_DB kept for backward compat)
15
+ * 3. .crewx/crewx.db (local, if exists)
16
+ * 4. ~/.crewx/crewx.db (global)
17
+ */
18
+
19
+ const { Database } = require('node-sqlite3-wasm');
20
+ const fs = require('fs');
21
+ const os = require('os');
22
+ const path = require('path');
23
+
24
+ // ─── Pricing table (mirrors packages/sdk/src/config/pricing.ts) ─────────────
25
+
26
+ const MODEL_PRICING = {
27
+ 'claude-opus': { input: 15, output: 75 },
28
+ 'opus': { input: 15, output: 75 },
29
+ 'claude-sonnet': { input: 3, output: 15 },
30
+ 'sonnet': { input: 3, output: 15 },
31
+ 'claude-haiku': { input: 0.25, output: 1.25 },
32
+ 'haiku': { input: 0.25, output: 1.25 },
33
+ 'gemini-3-pro-preview': { input: 1.25, output: 5 },
34
+ 'gemini-2-flash': { input: 0.075, output: 0.30 },
35
+ 'gemini-flash': { input: 0.075, output: 0.30 },
36
+ 'default': { input: 1, output: 1 },
37
+ };
38
+
39
+ function getPricing(model) {
40
+ const fallback = MODEL_PRICING['default'];
41
+ if (!model) return fallback;
42
+
43
+ const normalized = model.toLowerCase();
44
+
45
+ if (MODEL_PRICING[normalized]) return MODEL_PRICING[normalized];
46
+
47
+ for (const key of Object.keys(MODEL_PRICING)) {
48
+ if (normalized.includes(key)) return MODEL_PRICING[key];
49
+ }
50
+
51
+ return fallback;
52
+ }
53
+
54
+ function calculateCost(inputTokens, outputTokens, model) {
55
+ const pricing = getPricing(model);
56
+ return (inputTokens * pricing.input + outputTokens * pricing.output) / 1_000_000;
57
+ }
58
+
59
+ // ─── Usage extraction (mirrors tracing.service.ts extractUsageFromLogs) ──────
60
+
61
+ function extractUsageFromLogs(logs) {
62
+ if (!logs || logs.length === 0) return null;
63
+
64
+ let found = null;
65
+
66
+ for (const entry of logs) {
67
+ if (entry.level !== 'stdout') continue;
68
+
69
+ let parsed;
70
+ try {
71
+ parsed = JSON.parse(entry.message);
72
+ } catch {
73
+ continue;
74
+ }
75
+
76
+ // Claude stream-json: {"type":"result","subtype":"success","usage":{...}}
77
+ if (
78
+ parsed.type === 'result' &&
79
+ parsed.subtype === 'success' &&
80
+ parsed.usage &&
81
+ typeof parsed.usage === 'object'
82
+ ) {
83
+ const u = parsed.usage;
84
+ if (typeof u.input_tokens === 'number' && typeof u.output_tokens === 'number') {
85
+ found = { inputTokens: u.input_tokens, outputTokens: u.output_tokens };
86
+ }
87
+ }
88
+
89
+ // Codex experimental-json: {"type":"turn.completed","usage":{...}}
90
+ if (
91
+ parsed.type === 'turn.completed' &&
92
+ parsed.usage &&
93
+ typeof parsed.usage === 'object'
94
+ ) {
95
+ const u = parsed.usage;
96
+ if (typeof u.input_tokens === 'number' && typeof u.output_tokens === 'number') {
97
+ found = { inputTokens: u.input_tokens, outputTokens: u.output_tokens };
98
+ }
99
+ }
100
+ }
101
+
102
+ return found;
103
+ }
104
+
105
+ // ─── DB path resolution ───────────────────────────────────────────────────────
106
+
107
+ function resolveDbPaths(argPath) {
108
+ const paths = [];
109
+
110
+ if (argPath) {
111
+ paths.push(path.resolve(argPath));
112
+ return paths; // explicit path: only use that one
113
+ }
114
+
115
+ if (process.env.CREWX_TRACES_DB || process.env.CREWX_DB) {
116
+ paths.push(path.resolve(process.env.CREWX_TRACES_DB || process.env.CREWX_DB));
117
+ }
118
+
119
+ const localDb = path.join(process.cwd(), '.crewx', 'crewx.db');
120
+ if (fs.existsSync(localDb)) {
121
+ paths.push(localDb);
122
+ }
123
+
124
+ const globalDb = path.join(os.homedir(), '.crewx', 'crewx.db');
125
+ if (fs.existsSync(globalDb) && globalDb !== localDb) {
126
+ paths.push(globalDb);
127
+ }
128
+
129
+ return [...new Set(paths)]; // deduplicate
130
+ }
131
+
132
+ // ─── Backfill logic ───────────────────────────────────────────────────────────
133
+
134
+ function backfillDb(dbPath, dryRun) {
135
+ if (!fs.existsSync(dbPath)) {
136
+ console.log(` [SKIP] DB not found: ${dbPath}`);
137
+ return { processed: 0, updated: 0, skipped: 0 };
138
+ }
139
+
140
+ console.log(`\nProcessing: ${dbPath}${dryRun ? ' (dry-run)' : ''}`);
141
+
142
+ const db = new Database(dbPath, { readOnly: dryRun });
143
+
144
+ // Only target tasks where tokens are still 0 and logs exist
145
+ const tasks = db.all(`
146
+ SELECT id, model, logs
147
+ FROM tasks
148
+ WHERE (input_tokens = 0 OR input_tokens IS NULL)
149
+ AND logs IS NOT NULL
150
+ AND logs != '[]'
151
+ AND logs != ''
152
+ `);
153
+
154
+ console.log(` Found ${tasks.length} tasks with tokens=0 and non-empty logs`);
155
+
156
+ let updated = 0;
157
+ let skipped = 0;
158
+
159
+ for (const task of tasks) {
160
+ let logs;
161
+ try {
162
+ logs = JSON.parse(task.logs);
163
+ } catch {
164
+ skipped++;
165
+ continue;
166
+ }
167
+
168
+ const usage = extractUsageFromLogs(logs);
169
+ if (!usage) {
170
+ skipped++;
171
+ continue;
172
+ }
173
+
174
+ const costUsd = calculateCost(usage.inputTokens, usage.outputTokens, task.model);
175
+
176
+ if (dryRun) {
177
+ console.log(` [DRY] id=${task.id} model=${task.model ?? 'unknown'} input=${usage.inputTokens} output=${usage.outputTokens} cost=$${costUsd.toFixed(6)}`);
178
+ } else {
179
+ db.run('UPDATE tasks SET input_tokens=?, output_tokens=?, cost_usd=? WHERE id=?', [usage.inputTokens, usage.outputTokens, costUsd, task.id]);
180
+ }
181
+
182
+ updated++;
183
+ }
184
+
185
+ db.close();
186
+
187
+ console.log(` Updated: ${updated}, Skipped (no usage found): ${skipped}`);
188
+ return { processed: tasks.length, updated, skipped };
189
+ }
190
+
191
+ // ─── Main ─────────────────────────────────────────────────────────────────────
192
+
193
+ const args = process.argv.slice(2);
194
+ const dryRun = args.includes('--dry-run');
195
+ const dbArg = args.find(a => !a.startsWith('--'));
196
+
197
+ const dbPaths = resolveDbPaths(dbArg);
198
+
199
+ if (dbPaths.length === 0) {
200
+ console.error('No crewx.db found. Pass a path explicitly or set CREWX_DB.');
201
+ process.exit(1);
202
+ }
203
+
204
+ console.log(`backfill-tokens ${dryRun ? '[DRY RUN] ' : ''}— ${dbPaths.length} DB(s) to process`);
205
+
206
+ let totalUpdated = 0;
207
+ let totalSkipped = 0;
208
+
209
+ for (const dbPath of dbPaths) {
210
+ const { updated, skipped } = backfillDb(dbPath, dryRun);
211
+ totalUpdated += updated;
212
+ totalSkipped += skipped;
213
+ }
214
+
215
+ console.log(`\nDone. Total updated: ${totalUpdated}, skipped: ${totalSkipped}`);
216
+ if (dryRun) {
217
+ console.log('(Dry-run mode — no changes were written)');
218
+ }
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Ensure the CLI build artifact has an executable shebang after compilation.
4
+ * Works both inside the monorepo and when the package is installed from npm.
5
+ */
6
+ import { chmodSync, existsSync, readFileSync, writeFileSync, cpSync, rmSync, readdirSync } from 'fs';
7
+ import { dirname, join } from 'path';
8
+ import { fileURLToPath } from 'url';
9
+
10
+ const scriptDir = dirname(fileURLToPath(import.meta.url));
11
+ const packageRoot = dirname(scriptDir);
12
+ const candidateTemplateSources = [
13
+ join(dirname(packageRoot), 'templates'),
14
+ join(dirname(dirname(packageRoot)), 'templates'),
15
+ join(packageRoot, 'templates'),
16
+ ];
17
+ const sourceTemplatesDir = candidateTemplateSources.find(candidate => existsSync(candidate));
18
+ const targetTemplatesDir = join(packageRoot, 'templates');
19
+
20
+ try {
21
+ if (sourceTemplatesDir && sourceTemplatesDir !== targetTemplatesDir) {
22
+ rmSync(targetTemplatesDir, { recursive: true, force: true });
23
+ cpSync(sourceTemplatesDir, targetTemplatesDir, { recursive: true });
24
+ console.log(`✅ Synced templates to ${targetTemplatesDir}`);
25
+ } else if (!sourceTemplatesDir && !existsSync(targetTemplatesDir)) {
26
+ console.warn('⚠️ Templates directory not found in monorepo; packaged CLI may rely on CDN fallback.');
27
+ }
28
+ } catch (err) {
29
+ console.warn(`⚠️ Failed to sync templates directory: ${err instanceof Error ? err.message : err}`);
30
+ }
31
+
32
+ const candidatePaths = [
33
+ join(packageRoot, 'dist', 'main.js'),
34
+ join(packageRoot, '..', '..', 'dist', 'main.js')
35
+ ];
36
+
37
+ const targetPath = candidatePaths.find(p => existsSync(p));
38
+
39
+ if (!targetPath) {
40
+ console.warn('⚠️ postbuild skipped: dist/main.js not found (expected during migration)');
41
+ process.exit(0);
42
+ }
43
+
44
+ let content = readFileSync(targetPath, 'utf8');
45
+ const lines = content.split('\n');
46
+ if (lines[0] === '#!/usr/bin/env node' && lines[1] === '#!/usr/bin/env node') {
47
+ lines.shift();
48
+ content = lines.join('\n');
49
+ writeFileSync(targetPath, content);
50
+ console.log('✅ Removed duplicate shebang');
51
+ } else if (!content.startsWith('#!/usr/bin/env node')) {
52
+ writeFileSync(targetPath, '#!/usr/bin/env node\n' + content);
53
+ console.log('✅ Added shebang to dist/main.js');
54
+ } else {
55
+ console.log('✅ Shebang already present');
56
+ }
57
+
58
+ chmodSync(targetPath, 0o755);
59
+ console.log(`✅ Execute permission set (${targetPath})`);
60
+
61
+ // Ensure dist/ is treated as CJS even when root package.json has "type": "module".
62
+ // Without this, global installs fail because Node resolves module type from the
63
+ // nearest parent package.json — which is the root one after npm strips the
64
+ // packages/cli/package.json during pack.
65
+ const distDir = join(packageRoot, 'dist');
66
+ const distPkgPath = join(distDir, 'package.json');
67
+ writeFileSync(distPkgPath, JSON.stringify({ type: 'commonjs' }, null, 2) + '\n');
68
+ console.log('✅ Created dist/package.json with "type": "commonjs"');
69
+
70
+ // Rewrite workspace-local SDK paths to npm package names.
71
+ // nest build resolves `file:../sdk` as relative paths (e.g., ../../sdk/dist/api),
72
+ // which break when installed globally from npm. Replace with package names.
73
+ const distJsFiles = readdirSync(distDir).filter(f => f.endsWith('.js'));
74
+ for (const file of distJsFiles) {
75
+ const filePath = join(distDir, file);
76
+ let src = readFileSync(filePath, 'utf8');
77
+ const before = src;
78
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist\/api"\)/g, 'require("@crewx/sdk/api")');
79
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist\/tools"\)/g, 'require("@crewx/sdk/tools")');
80
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist\/internal"\)/g, 'require("@crewx/sdk/internal")');
81
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist\/core\/env-defaults"\)/g, 'require("@crewx/sdk/core/env-defaults")');
82
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist"\)/g, 'require("@crewx/sdk")');
83
+ src = src.replace(/require\("\.\.\/\.\.\/sdk\/dist\/([^"]+)"\)/g, 'require("@crewx/sdk/dist/$1")');
84
+ if (src !== before) {
85
+ writeFileSync(filePath, src);
86
+ console.log(`✅ Rewrote SDK paths in ${file}`);
87
+ }
88
+ }
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * postinstall hook used after npm install to ensure the CrewX CLI binary is executable.
4
+ * Works in both workspace and published package contexts.
5
+ */
6
+ import { chmodSync, existsSync } from 'fs';
7
+ import { dirname, join } from 'path';
8
+ import { fileURLToPath } from 'url';
9
+
10
+ const scriptDir = dirname(fileURLToPath(import.meta.url));
11
+ const packageRoot = dirname(scriptDir);
12
+
13
+ const candidatePaths = [
14
+ join(packageRoot, 'dist', 'main.js'),
15
+ join(packageRoot, '..', '..', 'dist', 'main.js')
16
+ ];
17
+
18
+ const targetPath = candidatePaths.find(p => existsSync(p));
19
+
20
+ if (!targetPath) {
21
+ console.warn('⚠️ postinstall skipped: dist/main.js not found (expected during migration)');
22
+ process.exit(0);
23
+ }
24
+
25
+ try {
26
+ chmodSync(targetPath, 0o755);
27
+ console.log(`✅ Execute permission set for crewx binary (${targetPath})`);
28
+ } catch (err) {
29
+ console.warn('⚠️ postinstall could not set execute permission:', err.message);
30
+ }