@nerviq/cli 1.29.0 → 1.30.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 (93) hide show
  1. package/CHANGELOG.md +1764 -1493
  2. package/README.md +568 -538
  3. package/SECURITY.md +78 -82
  4. package/bin/cli.js +2838 -2558
  5. package/docs/api-reference.md +356 -356
  6. package/docs/audit-fix.md +109 -0
  7. package/docs/autofix.md +3 -62
  8. package/docs/getting-started.md +1 -1
  9. package/docs/index.html +592 -592
  10. package/docs/integration-contracts.md +287 -287
  11. package/docs/maintenance.md +128 -128
  12. package/docs/new-platform-guide.md +202 -202
  13. package/docs/release-process.md +63 -0
  14. package/docs/shallow-risk.md +244 -244
  15. package/docs/why-nerviq.md +82 -82
  16. package/package.json +75 -67
  17. package/sdk/README.md +12 -3
  18. package/sdk/examples/langchain-integration.md +128 -0
  19. package/sdk/examples/self-governing-agent.js +135 -0
  20. package/sdk/index.d.ts +115 -0
  21. package/sdk/index.js +94 -0
  22. package/sdk/package.json +11 -0
  23. package/src/activity.js +13 -0
  24. package/src/aider/activity.js +226 -226
  25. package/src/aider/context.js +162 -162
  26. package/src/aider/freshness.js +123 -123
  27. package/src/aider/techniques.js +3465 -3465
  28. package/src/audit/layers.js +180 -180
  29. package/src/audit.js +1133 -1032
  30. package/src/auto-suggest.js +9 -2
  31. package/src/behavioral-drift.js +37 -2
  32. package/src/benchmark.js +299 -299
  33. package/src/codex/activity.js +324 -324
  34. package/src/codex/freshness.js +149 -142
  35. package/src/codex/techniques.js +4895 -4895
  36. package/src/context.js +326 -326
  37. package/src/continuous-ops.js +11 -1
  38. package/src/convert.js +340 -340
  39. package/src/copilot/config-parser.js +280 -280
  40. package/src/copilot/context.js +218 -218
  41. package/src/copilot/freshness.js +184 -177
  42. package/src/copilot/patch.js +238 -238
  43. package/src/copilot/techniques.js +3578 -3578
  44. package/src/cursor/freshness.js +194 -194
  45. package/src/cursor/patch.js +243 -243
  46. package/src/cursor/techniques.js +3735 -3735
  47. package/src/doctor.js +201 -201
  48. package/src/fix-engine.js +511 -8
  49. package/src/formatters/csv.js +86 -86
  50. package/src/formatters/junit.js +123 -123
  51. package/src/formatters/markdown.js +164 -164
  52. package/src/formatters/otel.js +151 -151
  53. package/src/freshness.js +163 -156
  54. package/src/gemini/activity.js +402 -402
  55. package/src/gemini/context.js +290 -290
  56. package/src/gemini/freshness.js +188 -188
  57. package/src/gemini/patch.js +229 -229
  58. package/src/gemini/techniques.js +3811 -3811
  59. package/src/governance.js +533 -533
  60. package/src/harmony/audit.js +306 -306
  61. package/src/i18n.js +63 -63
  62. package/src/insights.js +119 -119
  63. package/src/integrations.js +134 -134
  64. package/src/locales/en.json +33 -33
  65. package/src/locales/es.json +33 -33
  66. package/src/migrate.js +354 -354
  67. package/src/opencode/activity.js +286 -286
  68. package/src/opencode/freshness.js +137 -137
  69. package/src/opencode/techniques.js +3450 -3450
  70. package/src/safe-glyph.js +97 -0
  71. package/src/setup/analysis.js +12 -12
  72. package/src/setup.js +13 -6
  73. package/src/shallow-risk/index.js +113 -56
  74. package/src/shallow-risk/patterns/agent-config-cross-platform-drift.js +51 -50
  75. package/src/shallow-risk/patterns/agent-config-dangerous-autoapprove.js +47 -46
  76. package/src/shallow-risk/patterns/agent-config-deprecated-keys.js +47 -46
  77. package/src/shallow-risk/patterns/agent-config-framework-version-mismatch.js +138 -0
  78. package/src/shallow-risk/patterns/agent-config-missing-file.js +318 -317
  79. package/src/shallow-risk/patterns/agent-config-script-not-in-package-json.js +108 -0
  80. package/src/shallow-risk/patterns/agent-config-secret-literal.js +52 -49
  81. package/src/shallow-risk/patterns/agent-config-stack-contradiction.js +35 -34
  82. package/src/shallow-risk/patterns/hook-script-missing.js +71 -70
  83. package/src/shallow-risk/patterns/mcp-server-no-allowlist.js +53 -52
  84. package/src/shallow-risk/shared.js +653 -648
  85. package/src/source-urls.js +295 -295
  86. package/src/state-paths.js +85 -85
  87. package/src/supplemental-checks.js +805 -805
  88. package/src/telemetry.js +160 -160
  89. package/src/watch.js +46 -0
  90. package/src/windsurf/context.js +359 -359
  91. package/src/windsurf/freshness.js +194 -194
  92. package/src/windsurf/patch.js +231 -231
  93. package/src/windsurf/techniques.js +3779 -3779
package/src/convert.js CHANGED
@@ -1,340 +1,340 @@
1
- /**
2
- * Nerviq Convert
3
- *
4
- * Converts configuration files between AI coding platforms.
5
- * Reads the source platform's config and emits equivalent config
6
- * for the target platform, preserving intent where possible.
7
- *
8
- * Supported conversions:
9
- * claude → codex, cursor, copilot, gemini, windsurf, aider
10
- * codex → claude, cursor, copilot, gemini, windsurf, aider
11
- * cursor → claude, codex, copilot, gemini, windsurf, aider
12
- * (any) → (any) using canonical model as intermediary
13
- */
14
-
15
- 'use strict';
16
-
17
- const fs = require('fs');
18
- const path = require('path');
19
-
20
- const COLORS = {
21
- reset: '\x1b[0m',
22
- bold: '\x1b[1m',
23
- dim: '\x1b[2m',
24
- red: '\x1b[31m',
25
- green: '\x1b[32m',
26
- yellow: '\x1b[33m',
27
- blue: '\x1b[36m',
28
- };
29
-
30
- function c(text, color) {
31
- return `${COLORS[color] || ''}${text}${COLORS.reset}`;
32
- }
33
-
34
- // ─── Platform config readers ─────────────────────────────────────────────────
35
-
36
- /**
37
- * Read the canonical "intent" from a source platform.
38
- * Returns a normalized object with: name, description, rules[], mcpServers{}, hooks[]
39
- */
40
- function readSourceConfig(dir, from) {
41
- const canonical = {
42
- platform: from,
43
- name: path.basename(dir),
44
- description: null,
45
- rules: [], // Array of { name, content, alwaysOn, glob, description }
46
- mcpServers: {}, // { serverName: { command, args, env, url, type } }
47
- hooks: [], // Array of { event, command, matcher }
48
- techStack: [], // Detected languages/frameworks
49
- lintCmd: null,
50
- testCmd: null,
51
- buildCmd: null,
52
- };
53
-
54
- if (from === 'claude') {
55
- const claudeMd = fs.existsSync(path.join(dir, 'CLAUDE.md'))
56
- ? fs.readFileSync(path.join(dir, 'CLAUDE.md'), 'utf8')
57
- : null;
58
- if (claudeMd) {
59
- canonical.description = claudeMd.slice(0, 500);
60
- canonical.rules.push({ name: 'CLAUDE.md', content: claudeMd, alwaysOn: true });
61
- }
62
- // Read .claude/settings.json for MCP
63
- const settingsPath = path.join(dir, '.claude', 'settings.json');
64
- if (fs.existsSync(settingsPath)) {
65
- try {
66
- const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
67
- if (settings.mcpServers) canonical.mcpServers = settings.mcpServers;
68
- } catch {}
69
- }
70
- }
71
-
72
- if (from === 'codex') {
73
- const agentsMd = fs.existsSync(path.join(dir, 'AGENTS.md'))
74
- ? fs.readFileSync(path.join(dir, 'AGENTS.md'), 'utf8')
75
- : null;
76
- if (agentsMd) {
77
- canonical.description = agentsMd.slice(0, 500);
78
- canonical.rules.push({ name: 'AGENTS.md', content: agentsMd, alwaysOn: true });
79
- }
80
- }
81
-
82
- if (from === 'cursor') {
83
- const rulesDir = path.join(dir, '.cursor', 'rules');
84
- if (fs.existsSync(rulesDir)) {
85
- const files = fs.readdirSync(rulesDir).filter(f => f.endsWith('.mdc'));
86
- for (const file of files) {
87
- const content = fs.readFileSync(path.join(rulesDir, file), 'utf8');
88
- // Parse frontmatter
89
- const fmMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
90
- let alwaysOn = false;
91
- let glob = null;
92
- let desc = null;
93
- if (fmMatch) {
94
- alwaysOn = /alwaysApply\s*:\s*true/i.test(fmMatch[1]);
95
- const globMatch = fmMatch[1].match(/globs?\s*:\s*(.+)/i);
96
- if (globMatch) glob = globMatch[1].trim();
97
- const descMatch = fmMatch[1].match(/description\s*:\s*"?([^"\n]+)"?/i);
98
- if (descMatch) desc = descMatch[1].trim();
99
- }
100
- canonical.rules.push({
101
- name: file.replace(/\.(mdc|md|txt)$/i, ''),
102
- content,
103
- alwaysOn,
104
- glob,
105
- description: desc,
106
- });
107
- }
108
- }
109
- // Cursor MCP
110
- const mcpPath = path.join(dir, '.cursor', 'mcp.json');
111
- if (fs.existsSync(mcpPath)) {
112
- try {
113
- const mcp = JSON.parse(fs.readFileSync(mcpPath, 'utf8'));
114
- if (mcp.mcpServers) canonical.mcpServers = mcp.mcpServers;
115
- } catch {}
116
- }
117
- }
118
-
119
- if (from === 'gemini') {
120
- const geminiMd = fs.existsSync(path.join(dir, 'GEMINI.md'))
121
- ? fs.readFileSync(path.join(dir, 'GEMINI.md'), 'utf8')
122
- : null;
123
- if (geminiMd) {
124
- canonical.description = geminiMd.slice(0, 500);
125
- canonical.rules.push({ name: 'GEMINI.md', content: geminiMd, alwaysOn: true });
126
- }
127
- }
128
-
129
- if (from === 'windsurf') {
130
- const windsurfRulesDir = path.join(dir, '.windsurf', 'rules');
131
- if (fs.existsSync(windsurfRulesDir)) {
132
- const files = fs.readdirSync(windsurfRulesDir).filter(f => f.endsWith('.md'));
133
- for (const file of files) {
134
- const content = fs.readFileSync(path.join(windsurfRulesDir, file), 'utf8');
135
- canonical.rules.push({ name: file.replace('.md', ''), content, alwaysOn: true });
136
- }
137
- }
138
- }
139
-
140
- if (from === 'aider') {
141
- const aiderConf = fs.existsSync(path.join(dir, '.aider.conf.yml'))
142
- ? fs.readFileSync(path.join(dir, '.aider.conf.yml'), 'utf8')
143
- : null;
144
- if (aiderConf) {
145
- canonical.rules.push({ name: '.aider.conf.yml', content: aiderConf, alwaysOn: false });
146
- const lintMatch = aiderConf.match(/lint-cmd\s*:\s*(.+)/);
147
- if (lintMatch) canonical.lintCmd = lintMatch[1].trim().replace(/^['"]|['"]$/g, '');
148
- const testMatch = aiderConf.match(/test-cmd\s*:\s*(.+)/);
149
- if (testMatch) canonical.testCmd = testMatch[1].trim().replace(/^['"]|['"]$/g, '');
150
- }
151
- }
152
-
153
- if (from === 'copilot') {
154
- const copilotPath = path.join(dir, '.github', 'copilot-instructions.md');
155
- if (fs.existsSync(copilotPath)) {
156
- const content = fs.readFileSync(copilotPath, 'utf8');
157
- canonical.rules.push({ name: 'copilot-instructions', content, alwaysOn: true });
158
- }
159
- }
160
-
161
- return canonical;
162
- }
163
-
164
- // ─── Platform config writers ─────────────────────────────────────────────────
165
-
166
- function buildTargetOutput(canonical, to, { dryRun = false } = {}) {
167
- const outputs = []; // Array of { path, content }
168
- // Strip MDC frontmatter from rule content for non-cursor targets to prevent leaking
169
- const stripFrontmatter = (text) => text.replace(/^---[\s\S]*?---\n/m, '').trim();
170
- const combinedContent = to === 'cursor'
171
- ? canonical.rules.map(r => r.content).join('\n\n')
172
- : canonical.rules.map(r => stripFrontmatter(r.content)).join('\n\n');
173
-
174
- if (to === 'claude') {
175
- // Extract or create CLAUDE.md from combined rules
176
- const content = `# ${canonical.name}\n\n${combinedContent}\n`;
177
- outputs.push({ file: 'CLAUDE.md', content });
178
-
179
- if (Object.keys(canonical.mcpServers).length > 0) {
180
- const settings = { mcpServers: canonical.mcpServers };
181
- outputs.push({ file: '.claude/settings.json', content: JSON.stringify(settings, null, 2) + '\n' });
182
- }
183
- }
184
-
185
- if (to === 'codex') {
186
- const content = `# ${canonical.name}\n\n${combinedContent}\n`;
187
- outputs.push({ file: 'AGENTS.md', content });
188
- }
189
-
190
- if (to === 'cursor') {
191
- // Write each rule as an .mdc file
192
- if (canonical.rules.length === 0) {
193
- const content = `---\nalwaysApply: true\n---\n\n# ${canonical.name}\n\n${combinedContent}\n`;
194
- outputs.push({ file: '.cursor/rules/core.mdc', content });
195
- } else {
196
- for (const rule of canonical.rules) {
197
- const fm = rule.alwaysOn
198
- ? `---\nalwaysApply: true\n---\n`
199
- : rule.glob
200
- ? `---\nglobs: ${rule.glob}\nalwaysApply: false\n---\n`
201
- : `---\nalwaysApply: false\n---\n`;
202
- outputs.push({ file: `.cursor/rules/${rule.name}.mdc`, content: `${fm}\n${rule.content}\n` });
203
- }
204
- }
205
- if (Object.keys(canonical.mcpServers).length > 0) {
206
- const mcp = { mcpServers: canonical.mcpServers };
207
- outputs.push({ file: '.cursor/mcp.json', content: JSON.stringify(mcp, null, 2) + '\n' });
208
- }
209
- }
210
-
211
- if (to === 'gemini') {
212
- const content = `# ${canonical.name}\n\n${combinedContent}\n`;
213
- outputs.push({ file: 'GEMINI.md', content });
214
- if (Object.keys(canonical.mcpServers).length > 0) {
215
- const settings = { mcpServers: canonical.mcpServers };
216
- outputs.push({ file: '.gemini/settings.json', content: JSON.stringify(settings, null, 2) + '\n' });
217
- }
218
- }
219
-
220
- if (to === 'windsurf') {
221
- if (canonical.rules.length === 0) {
222
- outputs.push({ file: '.windsurf/rules/core.md', content: `---\ntrigger: always_on\n---\n\n${combinedContent}\n` });
223
- } else {
224
- for (const rule of canonical.rules) {
225
- const fm = `---\ntrigger: always_on\n---\n`;
226
- const safeContent = rule.content.replace(/^---[\s\S]*?---\n/m, '').trim();
227
- outputs.push({ file: `.windsurf/rules/${rule.name}.md`, content: `${fm}\n${safeContent}\n` });
228
- }
229
- }
230
- }
231
-
232
- if (to === 'aider') {
233
- const confLines = ['# Generated by nerviq convert'];
234
- if (canonical.lintCmd) confLines.push(`lint-cmd: '${canonical.lintCmd}'`);
235
- if (canonical.testCmd) confLines.push(`test-cmd: '${canonical.testCmd}'`);
236
- confLines.push('auto-commits: true');
237
- confLines.push('auto-lint: true');
238
- outputs.push({ file: '.aider.conf.yml', content: confLines.join('\n') + '\n' });
239
- if (combinedContent.trim()) {
240
- outputs.push({ file: 'CONVENTIONS.md', content: `# ${canonical.name} Conventions\n\n${combinedContent}\n` });
241
- }
242
- }
243
-
244
- if (to === 'copilot') {
245
- const content = `# ${canonical.name}\n\n${combinedContent}\n`;
246
- outputs.push({ file: '.github/copilot-instructions.md', content });
247
- }
248
-
249
- return outputs;
250
- }
251
-
252
- // ─── Main convert function ────────────────────────────────────────────────────
253
-
254
- async function runConvert({ dir = process.cwd(), from, to, dryRun = false, json = false } = {}) {
255
- if (!from || !to) {
256
- throw new Error('Both --from and --to are required. Example: nerviq convert --from claude --to codex');
257
- }
258
-
259
- const SUPPORTED = ['claude', 'codex', 'cursor', 'copilot', 'gemini', 'windsurf', 'aider', 'opencode'];
260
- if (!SUPPORTED.includes(from)) throw new Error(`Unsupported source platform '${from}'. Use: ${SUPPORTED.join(', ')}`);
261
- if (!SUPPORTED.includes(to)) throw new Error(`Unsupported target platform '${to}'. Use: ${SUPPORTED.join(', ')}`);
262
- if (from === to) throw new Error(`Source and target platform are the same: '${from}'`);
263
-
264
- const canonical = readSourceConfig(dir, from);
265
- const outputs = buildTargetOutput(canonical, to, { dryRun });
266
-
267
- const written = [];
268
- const skipped = [];
269
-
270
- if (!dryRun) {
271
- for (const out of outputs) {
272
- const outPath = path.join(dir, out.file);
273
- const outDir = path.dirname(outPath);
274
- if (!fs.existsSync(outPath)) {
275
- fs.mkdirSync(outDir, { recursive: true });
276
- fs.writeFileSync(outPath, out.content, 'utf8');
277
- written.push(out.file);
278
- } else {
279
- skipped.push(out.file);
280
- }
281
- }
282
- }
283
-
284
- const result = {
285
- from,
286
- to,
287
- dir,
288
- dryRun,
289
- sourceRulesFound: canonical.rules.length,
290
- mcpServersFound: Object.keys(canonical.mcpServers).length,
291
- outputFiles: outputs.map(o => o.file),
292
- written: dryRun ? [] : written,
293
- skipped: dryRun ? [] : skipped,
294
- wouldWrite: dryRun ? outputs.map(o => o.file) : [],
295
- };
296
-
297
- if (json) return JSON.stringify(result, null, 2);
298
-
299
- const lines = [''];
300
- lines.push(c(` nerviq convert ${from} → ${to}`, 'bold'));
301
- lines.push(c(' ═══════════════════════════════════════', 'dim'));
302
- lines.push('');
303
- lines.push(` Source platform: ${c(from, 'blue')} (${canonical.rules.length} rule(s) found)`);
304
- lines.push(` Target platform: ${c(to, 'blue')}`);
305
- lines.push(` Directory: ${dir}`);
306
- lines.push(` MCP servers: ${Object.keys(canonical.mcpServers).length}`);
307
- lines.push('');
308
-
309
- if (dryRun) {
310
- lines.push(c(' Dry run — no files written', 'yellow'));
311
- lines.push('');
312
- lines.push(' Would generate:');
313
- for (const f of outputs) {
314
- lines.push(` ${c('→', 'dim')} ${f.file}`);
315
- }
316
- } else if (written.length > 0 || skipped.length > 0) {
317
- if (written.length > 0) {
318
- lines.push(' Written:');
319
- for (const f of written) lines.push(` ${c('✓', 'green')} ${f}`);
320
- }
321
- if (skipped.length > 0) {
322
- lines.push(' Skipped (already exists):');
323
- for (const f of skipped) lines.push(` ${c('-', 'dim')} ${f}`);
324
- }
325
- }
326
-
327
- lines.push('');
328
- if (!dryRun && written.length > 0) {
329
- lines.push(c(` ✓ Conversion complete. Run \`nerviq audit --platform ${to}\` to verify.`, 'green'));
330
- } else if (dryRun) {
331
- lines.push(c(` Run without --dry-run to write files.`, 'dim'));
332
- } else {
333
- lines.push(c(` No new files written (all already exist).`, 'dim'));
334
- }
335
- lines.push('');
336
-
337
- return lines.join('\n');
338
- }
339
-
340
- module.exports = { runConvert };
1
+ /**
2
+ * Nerviq Convert
3
+ *
4
+ * Converts configuration files between AI coding platforms.
5
+ * Reads the source platform's config and emits equivalent config
6
+ * for the target platform, preserving intent where possible.
7
+ *
8
+ * Supported conversions:
9
+ * claude → codex, cursor, copilot, gemini, windsurf, aider
10
+ * codex → claude, cursor, copilot, gemini, windsurf, aider
11
+ * cursor → claude, codex, copilot, gemini, windsurf, aider
12
+ * (any) → (any) using canonical model as intermediary
13
+ */
14
+
15
+ 'use strict';
16
+
17
+ const fs = require('fs');
18
+ const path = require('path');
19
+
20
+ const COLORS = {
21
+ reset: '\x1b[0m',
22
+ bold: '\x1b[1m',
23
+ dim: '\x1b[2m',
24
+ red: '\x1b[31m',
25
+ green: '\x1b[32m',
26
+ yellow: '\x1b[33m',
27
+ blue: '\x1b[36m',
28
+ };
29
+
30
+ function c(text, color) {
31
+ return `${COLORS[color] || ''}${text}${COLORS.reset}`;
32
+ }
33
+
34
+ // ─── Platform config readers ─────────────────────────────────────────────────
35
+
36
+ /**
37
+ * Read the canonical "intent" from a source platform.
38
+ * Returns a normalized object with: name, description, rules[], mcpServers{}, hooks[]
39
+ */
40
+ function readSourceConfig(dir, from) {
41
+ const canonical = {
42
+ platform: from,
43
+ name: path.basename(dir),
44
+ description: null,
45
+ rules: [], // Array of { name, content, alwaysOn, glob, description }
46
+ mcpServers: {}, // { serverName: { command, args, env, url, type } }
47
+ hooks: [], // Array of { event, command, matcher }
48
+ techStack: [], // Detected languages/frameworks
49
+ lintCmd: null,
50
+ testCmd: null,
51
+ buildCmd: null,
52
+ };
53
+
54
+ if (from === 'claude') {
55
+ const claudeMd = fs.existsSync(path.join(dir, 'CLAUDE.md'))
56
+ ? fs.readFileSync(path.join(dir, 'CLAUDE.md'), 'utf8')
57
+ : null;
58
+ if (claudeMd) {
59
+ canonical.description = claudeMd.slice(0, 500);
60
+ canonical.rules.push({ name: 'CLAUDE.md', content: claudeMd, alwaysOn: true });
61
+ }
62
+ // Read .claude/settings.json for MCP
63
+ const settingsPath = path.join(dir, '.claude', 'settings.json');
64
+ if (fs.existsSync(settingsPath)) {
65
+ try {
66
+ const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
67
+ if (settings.mcpServers) canonical.mcpServers = settings.mcpServers;
68
+ } catch {}
69
+ }
70
+ }
71
+
72
+ if (from === 'codex') {
73
+ const agentsMd = fs.existsSync(path.join(dir, 'AGENTS.md'))
74
+ ? fs.readFileSync(path.join(dir, 'AGENTS.md'), 'utf8')
75
+ : null;
76
+ if (agentsMd) {
77
+ canonical.description = agentsMd.slice(0, 500);
78
+ canonical.rules.push({ name: 'AGENTS.md', content: agentsMd, alwaysOn: true });
79
+ }
80
+ }
81
+
82
+ if (from === 'cursor') {
83
+ const rulesDir = path.join(dir, '.cursor', 'rules');
84
+ if (fs.existsSync(rulesDir)) {
85
+ const files = fs.readdirSync(rulesDir).filter(f => f.endsWith('.mdc'));
86
+ for (const file of files) {
87
+ const content = fs.readFileSync(path.join(rulesDir, file), 'utf8');
88
+ // Parse frontmatter
89
+ const fmMatch = content.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/);
90
+ let alwaysOn = false;
91
+ let glob = null;
92
+ let desc = null;
93
+ if (fmMatch) {
94
+ alwaysOn = /alwaysApply\s*:\s*true/i.test(fmMatch[1]);
95
+ const globMatch = fmMatch[1].match(/globs?\s*:\s*(.+)/i);
96
+ if (globMatch) glob = globMatch[1].trim();
97
+ const descMatch = fmMatch[1].match(/description\s*:\s*"?([^"\n]+)"?/i);
98
+ if (descMatch) desc = descMatch[1].trim();
99
+ }
100
+ canonical.rules.push({
101
+ name: file.replace(/\.(mdc|md|txt)$/i, ''),
102
+ content,
103
+ alwaysOn,
104
+ glob,
105
+ description: desc,
106
+ });
107
+ }
108
+ }
109
+ // Cursor MCP
110
+ const mcpPath = path.join(dir, '.cursor', 'mcp.json');
111
+ if (fs.existsSync(mcpPath)) {
112
+ try {
113
+ const mcp = JSON.parse(fs.readFileSync(mcpPath, 'utf8'));
114
+ if (mcp.mcpServers) canonical.mcpServers = mcp.mcpServers;
115
+ } catch {}
116
+ }
117
+ }
118
+
119
+ if (from === 'gemini') {
120
+ const geminiMd = fs.existsSync(path.join(dir, 'GEMINI.md'))
121
+ ? fs.readFileSync(path.join(dir, 'GEMINI.md'), 'utf8')
122
+ : null;
123
+ if (geminiMd) {
124
+ canonical.description = geminiMd.slice(0, 500);
125
+ canonical.rules.push({ name: 'GEMINI.md', content: geminiMd, alwaysOn: true });
126
+ }
127
+ }
128
+
129
+ if (from === 'windsurf') {
130
+ const windsurfRulesDir = path.join(dir, '.windsurf', 'rules');
131
+ if (fs.existsSync(windsurfRulesDir)) {
132
+ const files = fs.readdirSync(windsurfRulesDir).filter(f => f.endsWith('.md'));
133
+ for (const file of files) {
134
+ const content = fs.readFileSync(path.join(windsurfRulesDir, file), 'utf8');
135
+ canonical.rules.push({ name: file.replace('.md', ''), content, alwaysOn: true });
136
+ }
137
+ }
138
+ }
139
+
140
+ if (from === 'aider') {
141
+ const aiderConf = fs.existsSync(path.join(dir, '.aider.conf.yml'))
142
+ ? fs.readFileSync(path.join(dir, '.aider.conf.yml'), 'utf8')
143
+ : null;
144
+ if (aiderConf) {
145
+ canonical.rules.push({ name: '.aider.conf.yml', content: aiderConf, alwaysOn: false });
146
+ const lintMatch = aiderConf.match(/lint-cmd\s*:\s*(.+)/);
147
+ if (lintMatch) canonical.lintCmd = lintMatch[1].trim().replace(/^['"]|['"]$/g, '');
148
+ const testMatch = aiderConf.match(/test-cmd\s*:\s*(.+)/);
149
+ if (testMatch) canonical.testCmd = testMatch[1].trim().replace(/^['"]|['"]$/g, '');
150
+ }
151
+ }
152
+
153
+ if (from === 'copilot') {
154
+ const copilotPath = path.join(dir, '.github', 'copilot-instructions.md');
155
+ if (fs.existsSync(copilotPath)) {
156
+ const content = fs.readFileSync(copilotPath, 'utf8');
157
+ canonical.rules.push({ name: 'copilot-instructions', content, alwaysOn: true });
158
+ }
159
+ }
160
+
161
+ return canonical;
162
+ }
163
+
164
+ // ─── Platform config writers ─────────────────────────────────────────────────
165
+
166
+ function buildTargetOutput(canonical, to, { dryRun = false } = {}) {
167
+ const outputs = []; // Array of { path, content }
168
+ // Strip MDC frontmatter from rule content for non-cursor targets to prevent leaking
169
+ const stripFrontmatter = (text) => text.replace(/^---[\s\S]*?---\n/m, '').trim();
170
+ const combinedContent = to === 'cursor'
171
+ ? canonical.rules.map(r => r.content).join('\n\n')
172
+ : canonical.rules.map(r => stripFrontmatter(r.content)).join('\n\n');
173
+
174
+ if (to === 'claude') {
175
+ // Extract or create CLAUDE.md from combined rules
176
+ const content = `# ${canonical.name}\n\n${combinedContent}\n`;
177
+ outputs.push({ file: 'CLAUDE.md', content });
178
+
179
+ if (Object.keys(canonical.mcpServers).length > 0) {
180
+ const settings = { mcpServers: canonical.mcpServers };
181
+ outputs.push({ file: '.claude/settings.json', content: JSON.stringify(settings, null, 2) + '\n' });
182
+ }
183
+ }
184
+
185
+ if (to === 'codex') {
186
+ const content = `# ${canonical.name}\n\n${combinedContent}\n`;
187
+ outputs.push({ file: 'AGENTS.md', content });
188
+ }
189
+
190
+ if (to === 'cursor') {
191
+ // Write each rule as an .mdc file
192
+ if (canonical.rules.length === 0) {
193
+ const content = `---\nalwaysApply: true\n---\n\n# ${canonical.name}\n\n${combinedContent}\n`;
194
+ outputs.push({ file: '.cursor/rules/core.mdc', content });
195
+ } else {
196
+ for (const rule of canonical.rules) {
197
+ const fm = rule.alwaysOn
198
+ ? `---\nalwaysApply: true\n---\n`
199
+ : rule.glob
200
+ ? `---\nglobs: ${rule.glob}\nalwaysApply: false\n---\n`
201
+ : `---\nalwaysApply: false\n---\n`;
202
+ outputs.push({ file: `.cursor/rules/${rule.name}.mdc`, content: `${fm}\n${rule.content}\n` });
203
+ }
204
+ }
205
+ if (Object.keys(canonical.mcpServers).length > 0) {
206
+ const mcp = { mcpServers: canonical.mcpServers };
207
+ outputs.push({ file: '.cursor/mcp.json', content: JSON.stringify(mcp, null, 2) + '\n' });
208
+ }
209
+ }
210
+
211
+ if (to === 'gemini') {
212
+ const content = `# ${canonical.name}\n\n${combinedContent}\n`;
213
+ outputs.push({ file: 'GEMINI.md', content });
214
+ if (Object.keys(canonical.mcpServers).length > 0) {
215
+ const settings = { mcpServers: canonical.mcpServers };
216
+ outputs.push({ file: '.gemini/settings.json', content: JSON.stringify(settings, null, 2) + '\n' });
217
+ }
218
+ }
219
+
220
+ if (to === 'windsurf') {
221
+ if (canonical.rules.length === 0) {
222
+ outputs.push({ file: '.windsurf/rules/core.md', content: `---\ntrigger: always_on\n---\n\n${combinedContent}\n` });
223
+ } else {
224
+ for (const rule of canonical.rules) {
225
+ const fm = `---\ntrigger: always_on\n---\n`;
226
+ const safeContent = rule.content.replace(/^---[\s\S]*?---\n/m, '').trim();
227
+ outputs.push({ file: `.windsurf/rules/${rule.name}.md`, content: `${fm}\n${safeContent}\n` });
228
+ }
229
+ }
230
+ }
231
+
232
+ if (to === 'aider') {
233
+ const confLines = ['# Generated by nerviq convert'];
234
+ if (canonical.lintCmd) confLines.push(`lint-cmd: '${canonical.lintCmd}'`);
235
+ if (canonical.testCmd) confLines.push(`test-cmd: '${canonical.testCmd}'`);
236
+ confLines.push('auto-commits: true');
237
+ confLines.push('auto-lint: true');
238
+ outputs.push({ file: '.aider.conf.yml', content: confLines.join('\n') + '\n' });
239
+ if (combinedContent.trim()) {
240
+ outputs.push({ file: 'CONVENTIONS.md', content: `# ${canonical.name} Conventions\n\n${combinedContent}\n` });
241
+ }
242
+ }
243
+
244
+ if (to === 'copilot') {
245
+ const content = `# ${canonical.name}\n\n${combinedContent}\n`;
246
+ outputs.push({ file: '.github/copilot-instructions.md', content });
247
+ }
248
+
249
+ return outputs;
250
+ }
251
+
252
+ // ─── Main convert function ────────────────────────────────────────────────────
253
+
254
+ async function runConvert({ dir = process.cwd(), from, to, dryRun = false, json = false } = {}) {
255
+ if (!from || !to) {
256
+ throw new Error('Both --from and --to are required. Example: nerviq convert --from claude --to codex');
257
+ }
258
+
259
+ const SUPPORTED = ['claude', 'codex', 'cursor', 'copilot', 'gemini', 'windsurf', 'aider', 'opencode'];
260
+ if (!SUPPORTED.includes(from)) throw new Error(`Unsupported source platform '${from}'. Use: ${SUPPORTED.join(', ')}`);
261
+ if (!SUPPORTED.includes(to)) throw new Error(`Unsupported target platform '${to}'. Use: ${SUPPORTED.join(', ')}`);
262
+ if (from === to) throw new Error(`Source and target platform are the same: '${from}'`);
263
+
264
+ const canonical = readSourceConfig(dir, from);
265
+ const outputs = buildTargetOutput(canonical, to, { dryRun });
266
+
267
+ const written = [];
268
+ const skipped = [];
269
+
270
+ if (!dryRun) {
271
+ for (const out of outputs) {
272
+ const outPath = path.join(dir, out.file);
273
+ const outDir = path.dirname(outPath);
274
+ if (!fs.existsSync(outPath)) {
275
+ fs.mkdirSync(outDir, { recursive: true });
276
+ fs.writeFileSync(outPath, out.content, 'utf8');
277
+ written.push(out.file);
278
+ } else {
279
+ skipped.push(out.file);
280
+ }
281
+ }
282
+ }
283
+
284
+ const result = {
285
+ from,
286
+ to,
287
+ dir,
288
+ dryRun,
289
+ sourceRulesFound: canonical.rules.length,
290
+ mcpServersFound: Object.keys(canonical.mcpServers).length,
291
+ outputFiles: outputs.map(o => o.file),
292
+ written: dryRun ? [] : written,
293
+ skipped: dryRun ? [] : skipped,
294
+ wouldWrite: dryRun ? outputs.map(o => o.file) : [],
295
+ };
296
+
297
+ if (json) return JSON.stringify(result, null, 2);
298
+
299
+ const lines = [''];
300
+ lines.push(c(` nerviq convert ${from} → ${to}`, 'bold'));
301
+ lines.push(c(' ═══════════════════════════════════════', 'dim'));
302
+ lines.push('');
303
+ lines.push(` Source platform: ${c(from, 'blue')} (${canonical.rules.length} rule(s) found)`);
304
+ lines.push(` Target platform: ${c(to, 'blue')}`);
305
+ lines.push(` Directory: ${dir}`);
306
+ lines.push(` MCP servers: ${Object.keys(canonical.mcpServers).length}`);
307
+ lines.push('');
308
+
309
+ if (dryRun) {
310
+ lines.push(c(' Dry run — no files written', 'yellow'));
311
+ lines.push('');
312
+ lines.push(' Would generate:');
313
+ for (const f of outputs) {
314
+ lines.push(` ${c('→', 'dim')} ${f.file}`);
315
+ }
316
+ } else if (written.length > 0 || skipped.length > 0) {
317
+ if (written.length > 0) {
318
+ lines.push(' Written:');
319
+ for (const f of written) lines.push(` ${c('✓', 'green')} ${f}`);
320
+ }
321
+ if (skipped.length > 0) {
322
+ lines.push(' Skipped (already exists):');
323
+ for (const f of skipped) lines.push(` ${c('-', 'dim')} ${f}`);
324
+ }
325
+ }
326
+
327
+ lines.push('');
328
+ if (!dryRun && written.length > 0) {
329
+ lines.push(c(` ✓ Conversion complete. Run \`nerviq audit --platform ${to}\` to verify.`, 'green'));
330
+ } else if (dryRun) {
331
+ lines.push(c(` Run without --dry-run to write files.`, 'dim'));
332
+ } else {
333
+ lines.push(c(` No new files written (all already exist).`, 'dim'));
334
+ }
335
+ lines.push('');
336
+
337
+ return lines.join('\n');
338
+ }
339
+
340
+ module.exports = { runConvert };