baseguard 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/.baseguardrc.example.json +63 -63
  2. package/.eslintrc.json +24 -24
  3. package/.prettierrc +7 -7
  4. package/CHANGELOG.md +195 -195
  5. package/DEPLOYMENT.md +624 -624
  6. package/DEPLOYMENT_CHECKLIST.md +239 -239
  7. package/DEPLOYMENT_SUMMARY_v1.0.2.md +202 -202
  8. package/QUICK_START.md +134 -134
  9. package/README.md +488 -488
  10. package/RELEASE_NOTES_v1.0.2.md +434 -434
  11. package/bin/base.js +627 -627
  12. package/dist/ai/fix-manager.d.ts.map +1 -1
  13. package/dist/ai/fix-manager.js +1 -1
  14. package/dist/ai/fix-manager.js.map +1 -1
  15. package/dist/ai/gemini-analyzer.d.ts.map +1 -1
  16. package/dist/ai/gemini-analyzer.js +29 -35
  17. package/dist/ai/gemini-analyzer.js.map +1 -1
  18. package/dist/ai/gemini-code-fixer.d.ts.map +1 -1
  19. package/dist/ai/gemini-code-fixer.js +58 -58
  20. package/dist/ai/gemini-code-fixer.js.map +1 -1
  21. package/dist/ai/jules-implementer.d.ts +3 -0
  22. package/dist/ai/jules-implementer.d.ts.map +1 -1
  23. package/dist/ai/jules-implementer.js +63 -32
  24. package/dist/ai/jules-implementer.js.map +1 -1
  25. package/dist/ai/unified-code-fixer.js.map +1 -1
  26. package/dist/commands/check.d.ts.map +1 -1
  27. package/dist/commands/check.js +1 -1
  28. package/dist/commands/check.js.map +1 -1
  29. package/dist/commands/config.js +2 -1
  30. package/dist/commands/config.js.map +1 -1
  31. package/dist/commands/fix.d.ts.map +1 -1
  32. package/dist/commands/fix.js +48 -15
  33. package/dist/commands/fix.js.map +1 -1
  34. package/dist/core/api-key-manager.js +2 -2
  35. package/dist/core/api-key-manager.js.map +1 -1
  36. package/dist/core/baseguard.d.ts +1 -0
  37. package/dist/core/baseguard.d.ts.map +1 -1
  38. package/dist/core/baseguard.js +13 -10
  39. package/dist/core/baseguard.js.map +1 -1
  40. package/dist/core/baseline-checker.d.ts.map +1 -1
  41. package/dist/core/baseline-checker.js +8 -5
  42. package/dist/core/baseline-checker.js.map +1 -1
  43. package/dist/core/configuration-recovery.d.ts.map +1 -1
  44. package/dist/core/configuration-recovery.js +1 -1
  45. package/dist/core/configuration-recovery.js.map +1 -1
  46. package/dist/core/debug-logger.d.ts.map +1 -1
  47. package/dist/core/debug-logger.js +1 -1
  48. package/dist/core/debug-logger.js.map +1 -1
  49. package/dist/core/error-handler.d.ts.map +1 -1
  50. package/dist/core/error-handler.js +2 -1
  51. package/dist/core/error-handler.js.map +1 -1
  52. package/dist/core/gitignore-manager.js +5 -5
  53. package/dist/core/graceful-degradation-manager.d.ts.map +1 -1
  54. package/dist/core/graceful-degradation-manager.js +16 -16
  55. package/dist/core/graceful-degradation-manager.js.map +1 -1
  56. package/dist/core/lazy-loader.d.ts.map +1 -1
  57. package/dist/core/lazy-loader.js +9 -2
  58. package/dist/core/lazy-loader.js.map +1 -1
  59. package/dist/core/memory-manager.d.ts +0 -3
  60. package/dist/core/memory-manager.d.ts.map +1 -1
  61. package/dist/core/memory-manager.js.map +1 -1
  62. package/dist/core/parser-worker.d.ts +2 -0
  63. package/dist/core/parser-worker.d.ts.map +1 -0
  64. package/dist/core/parser-worker.js +19 -0
  65. package/dist/core/parser-worker.js.map +1 -0
  66. package/dist/core/startup-optimizer.d.ts.map +1 -1
  67. package/dist/core/startup-optimizer.js +4 -8
  68. package/dist/core/startup-optimizer.js.map +1 -1
  69. package/dist/core/system-error-handler.d.ts.map +1 -1
  70. package/dist/core/system-error-handler.js.map +1 -1
  71. package/dist/git/automation-engine.d.ts.map +1 -1
  72. package/dist/git/automation-engine.js +5 -4
  73. package/dist/git/automation-engine.js.map +1 -1
  74. package/dist/git/github-manager.d.ts.map +1 -1
  75. package/dist/git/github-manager.js.map +1 -1
  76. package/dist/git/hook-manager.js +5 -5
  77. package/dist/git/hook-manager.js.map +1 -1
  78. package/dist/parsers/parser-manager.d.ts.map +1 -1
  79. package/dist/parsers/parser-manager.js +1 -1
  80. package/dist/parsers/parser-manager.js.map +1 -1
  81. package/dist/parsers/svelte-parser.js +1 -1
  82. package/dist/parsers/svelte-parser.js.map +1 -1
  83. package/dist/parsers/vanilla-parser.d.ts.map +1 -1
  84. package/dist/parsers/vanilla-parser.js.map +1 -1
  85. package/dist/parsers/vue-parser.d.ts.map +1 -1
  86. package/dist/parsers/vue-parser.js.map +1 -1
  87. package/dist/ui/components.d.ts +1 -1
  88. package/dist/ui/components.d.ts.map +1 -1
  89. package/dist/ui/components.js +11 -11
  90. package/dist/ui/components.js.map +1 -1
  91. package/dist/ui/terminal-header.js +14 -14
  92. package/package.json +105 -105
  93. package/src/ai/__tests__/gemini-analyzer.test.ts +180 -180
  94. package/src/ai/agentkit-orchestrator.ts +533 -533
  95. package/src/ai/fix-manager.ts +362 -362
  96. package/src/ai/gemini-analyzer.ts +665 -671
  97. package/src/ai/gemini-code-fixer.ts +539 -540
  98. package/src/ai/index.ts +3 -3
  99. package/src/ai/jules-implementer.ts +504 -460
  100. package/src/ai/unified-code-fixer.ts +347 -347
  101. package/src/commands/automation.ts +343 -343
  102. package/src/commands/check.ts +298 -299
  103. package/src/commands/config.ts +584 -583
  104. package/src/commands/fix.ts +269 -238
  105. package/src/commands/index.ts +6 -6
  106. package/src/commands/init.ts +155 -155
  107. package/src/commands/status.ts +306 -306
  108. package/src/core/api-key-manager.ts +298 -298
  109. package/src/core/baseguard.ts +757 -756
  110. package/src/core/baseline-checker.ts +566 -563
  111. package/src/core/cache-manager.ts +271 -271
  112. package/src/core/configuration-recovery.ts +672 -673
  113. package/src/core/configuration.ts +595 -595
  114. package/src/core/debug-logger.ts +590 -590
  115. package/src/core/directory-filter.ts +420 -420
  116. package/src/core/error-handler.ts +518 -517
  117. package/src/core/file-processor.ts +337 -337
  118. package/src/core/gitignore-manager.ts +168 -168
  119. package/src/core/graceful-degradation-manager.ts +596 -596
  120. package/src/core/index.ts +16 -16
  121. package/src/core/lazy-loader.ts +317 -307
  122. package/src/core/memory-manager.ts +290 -295
  123. package/src/core/parser-worker.ts +33 -0
  124. package/src/core/startup-optimizer.ts +246 -255
  125. package/src/core/system-error-handler.ts +755 -756
  126. package/src/git/automation-engine.ts +361 -361
  127. package/src/git/github-manager.ts +190 -192
  128. package/src/git/hook-manager.ts +210 -210
  129. package/src/git/index.ts +3 -3
  130. package/src/index.ts +7 -7
  131. package/src/parsers/feature-validator.ts +558 -558
  132. package/src/parsers/index.ts +7 -7
  133. package/src/parsers/parser-manager.ts +418 -419
  134. package/src/parsers/parser.ts +25 -25
  135. package/src/parsers/react-parser-optimized.ts +160 -160
  136. package/src/parsers/react-parser.ts +358 -358
  137. package/src/parsers/svelte-parser.ts +510 -510
  138. package/src/parsers/vanilla-parser.ts +685 -686
  139. package/src/parsers/vue-parser.ts +476 -478
  140. package/src/types/index.ts +95 -95
  141. package/src/ui/components.ts +567 -567
  142. package/src/ui/help.ts +192 -192
  143. package/src/ui/index.ts +3 -3
  144. package/src/ui/prompts.ts +680 -680
  145. package/src/ui/terminal-header.ts +58 -58
  146. package/test-build.js +40 -40
  147. package/test-config-commands.js +55 -55
  148. package/test-header-simple.js +32 -32
  149. package/test-terminal-header.js +11 -11
  150. package/test-ui.js +28 -28
  151. package/tests/e2e/baseguard.e2e.test.ts +515 -515
  152. package/tests/e2e/cross-platform.e2e.test.ts +419 -419
  153. package/tests/e2e/git-integration.e2e.test.ts +486 -486
  154. package/tests/fixtures/react-project/package.json +13 -13
  155. package/tests/fixtures/react-project/src/App.css +75 -75
  156. package/tests/fixtures/react-project/src/App.tsx +76 -76
  157. package/tests/fixtures/svelte-project/package.json +10 -10
  158. package/tests/fixtures/svelte-project/src/App.svelte +368 -368
  159. package/tests/fixtures/vanilla-project/index.html +75 -75
  160. package/tests/fixtures/vanilla-project/script.js +330 -330
  161. package/tests/fixtures/vanilla-project/styles.css +358 -358
  162. package/tests/fixtures/vue-project/package.json +11 -11
  163. package/tests/fixtures/vue-project/src/App.vue +215 -215
  164. package/tmp-smoke/.baseguard/backups/config-2026-02-19T12-04-11-067Z-auto.json +30 -0
  165. package/tmp-smoke/src/bad.css +3 -0
  166. package/tsconfig.json +34 -34
  167. package/vitest.config.ts +11 -11
  168. package/dist/terminal-header.d.ts +0 -12
  169. package/dist/terminal-header.js +0 -45
@@ -1,362 +1,362 @@
1
- import { readFile, writeFile, copyFile } from 'fs/promises';
2
- import { existsSync } from 'fs';
3
- import { join, dirname } from 'path';
4
- import { mkdir } from 'fs/promises';
5
- import chalk from 'chalk';
6
- import type { Fix, Violation } from '../types/index.js';
7
-
8
- /**
9
- * Fix manager for previewing and applying code fixes
10
- */
11
- export class FixManager {
12
- private appliedFixes: Map<string, { original: string; backup: string }> = new Map();
13
-
14
- /**
15
- * Generate unified diff preview of proposed changes
16
- */
17
- async generatePreview(fix: Fix): Promise<string> {
18
- try {
19
- const originalContent = await readFile(fix.filePath, 'utf8');
20
- const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
21
-
22
- return this.createUnifiedDiff(fix.filePath, originalContent, modifiedContent);
23
- } catch (error) {
24
- throw new Error(`Failed to generate preview: ${error instanceof Error ? error.message : 'Unknown error'}`);
25
- }
26
- }
27
-
28
- /**
29
- * Show interactive fix approval with clear change descriptions
30
- */
31
- async showFixPreview(fix: Fix): Promise<boolean> {
32
- console.log(chalk.cyan(`\nšŸ”§ Fix Preview for ${fix.violation.feature} in ${fix.filePath}\n`));
33
-
34
- // Show fix explanation
35
- console.log(chalk.white('šŸ“‹ Fix Description:'));
36
- console.log(chalk.dim(fix.explanation));
37
- console.log();
38
-
39
- // Show confidence score
40
- const confidenceColor = fix.confidence >= 0.8 ? 'green' : fix.confidence >= 0.6 ? 'yellow' : 'red';
41
- console.log(chalk.white('šŸŽÆ Confidence: ') + chalk[confidenceColor](`${Math.round(fix.confidence * 100)}%`));
42
- console.log();
43
-
44
- // Show unified diff
45
- console.log(chalk.white('šŸ“ Changes:'));
46
- const preview = await this.generatePreview(fix);
47
- console.log(this.colorizeUnifiedDiff(preview));
48
- console.log();
49
-
50
- // Show human-readable preview
51
- if (fix.preview) {
52
- console.log(chalk.white('šŸ‘€ Summary:'));
53
- console.log(chalk.dim(fix.preview));
54
- console.log();
55
- }
56
-
57
- // Interactive approval
58
- const { default: inquirer } = await import('inquirer');
59
-
60
- const { action } = await inquirer.prompt([
61
- {
62
- type: 'list',
63
- name: 'action',
64
- message: 'What would you like to do with this fix?',
65
- choices: [
66
- { name: 'āœ… Apply this fix', value: 'apply' },
67
- { name: 'šŸ‘€ Show detailed diff', value: 'detail' },
68
- { name: 'āŒ Skip this fix', value: 'skip' },
69
- { name: '🚫 Cancel all fixes', value: 'cancel' }
70
- ]
71
- }
72
- ]);
73
-
74
- switch (action) {
75
- case 'apply':
76
- return true;
77
- case 'detail':
78
- await this.showDetailedDiff(fix);
79
- return await this.showFixPreview(fix); // Show preview again
80
- case 'skip':
81
- return false;
82
- case 'cancel':
83
- throw new Error('Fix application cancelled by user');
84
- default:
85
- return false;
86
- }
87
- }
88
-
89
- /**
90
- * Apply fixes to original files while preserving formatting and structure
91
- */
92
- async applyFix(fix: Fix): Promise<void> {
93
- try {
94
- // Create backup before applying fix
95
- await this.createBackup(fix.filePath);
96
-
97
- // Read original content
98
- const originalContent = await readFile(fix.filePath, 'utf8');
99
-
100
- // Apply patch to content
101
- const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
102
-
103
- // Write modified content back to file
104
- await writeFile(fix.filePath, modifiedContent, 'utf8');
105
-
106
- // Store fix information for potential rollback
107
- this.appliedFixes.set(fix.filePath, {
108
- original: originalContent,
109
- backup: this.getBackupPath(fix.filePath)
110
- });
111
-
112
- console.log(chalk.green(`āœ… Applied fix to ${fix.filePath}`));
113
- } catch (error) {
114
- throw new Error(`Failed to apply fix: ${error instanceof Error ? error.message : 'Unknown error'}`);
115
- }
116
- }
117
-
118
- /**
119
- * Apply multiple fixes with batch processing
120
- */
121
- async applyFixes(fixes: Fix[]): Promise<{ applied: Fix[]; skipped: Fix[]; failed: { fix: Fix; error: string }[] }> {
122
- const results = {
123
- applied: [] as Fix[],
124
- skipped: [] as Fix[],
125
- failed: [] as { fix: Fix; error: string }[]
126
- };
127
-
128
- console.log(chalk.cyan(`\nšŸ”§ Applying ${fixes.length} fixes...\n`));
129
-
130
- for (const fix of fixes) {
131
- try {
132
- const shouldApply = await this.showFixPreview(fix);
133
-
134
- if (shouldApply) {
135
- await this.applyFix(fix);
136
- results.applied.push(fix);
137
- } else {
138
- results.skipped.push(fix);
139
- console.log(chalk.yellow(`ā­ļø Skipped fix for ${fix.filePath}`));
140
- }
141
- } catch (error) {
142
- const errorMessage = error instanceof Error ? error.message : 'Unknown error';
143
- results.failed.push({ fix, error: errorMessage });
144
- console.log(chalk.red(`āŒ Failed to apply fix for ${fix.filePath}: ${errorMessage}`));
145
- }
146
- }
147
-
148
- return results;
149
- }
150
-
151
- /**
152
- * Rollback applied fixes
153
- */
154
- async rollbackFix(filePath: string): Promise<void> {
155
- const fixInfo = this.appliedFixes.get(filePath);
156
-
157
- if (!fixInfo) {
158
- throw new Error(`No applied fix found for ${filePath}`);
159
- }
160
-
161
- try {
162
- // Restore original content
163
- await writeFile(filePath, fixInfo.original, 'utf8');
164
-
165
- // Remove from applied fixes
166
- this.appliedFixes.delete(filePath);
167
-
168
- console.log(chalk.green(`āœ… Rolled back fix for ${filePath}`));
169
- } catch (error) {
170
- throw new Error(`Failed to rollback fix: ${error instanceof Error ? error.message : 'Unknown error'}`);
171
- }
172
- }
173
-
174
- /**
175
- * Rollback all applied fixes
176
- */
177
- async rollbackAllFixes(): Promise<void> {
178
- const filePaths = Array.from(this.appliedFixes.keys());
179
-
180
- console.log(chalk.cyan(`\nšŸ”„ Rolling back ${filePaths.length} fixes...\n`));
181
-
182
- for (const filePath of filePaths) {
183
- try {
184
- await this.rollbackFix(filePath);
185
- } catch (error) {
186
- console.log(chalk.red(`āŒ Failed to rollback ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`));
187
- }
188
- }
189
- }
190
-
191
- /**
192
- * Get list of applied fixes
193
- */
194
- getAppliedFixes(): string[] {
195
- return Array.from(this.appliedFixes.keys());
196
- }
197
-
198
- /**
199
- * Create backup of original file
200
- */
201
- private async createBackup(filePath: string): Promise<void> {
202
- const backupPath = this.getBackupPath(filePath);
203
- const backupDir = dirname(backupPath);
204
-
205
- // Ensure backup directory exists
206
- if (!existsSync(backupDir)) {
207
- await mkdir(backupDir, { recursive: true });
208
- }
209
-
210
- // Copy original file to backup location
211
- await copyFile(filePath, backupPath);
212
- }
213
-
214
- /**
215
- * Get backup file path
216
- */
217
- private getBackupPath(filePath: string): string {
218
- const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
219
- return join('.baseguard', 'backups', `${filePath.replace(/[/\\]/g, '_')}.${timestamp}.backup`);
220
- }
221
-
222
- /**
223
- * Apply patch to file content
224
- */
225
- private applyPatchToContent(originalContent: string, patch: string): string {
226
- const lines = originalContent.split('\n');
227
- const patchLines = patch.split('\n');
228
-
229
- let result = [...lines];
230
- let currentLine = 0;
231
-
232
- for (const patchLine of patchLines) {
233
- if (patchLine.startsWith('@@')) {
234
- // Parse hunk header to get line numbers
235
- const match = patchLine.match(/@@ -(\d+),?\d* \+(\d+),?\d* @@/);
236
- if (match && match[2]) {
237
- currentLine = parseInt(match[2]) - 1;
238
- }
239
- } else if (patchLine.startsWith('-')) {
240
- // Remove line
241
- const lineToRemove = patchLine.substring(1);
242
- const index = result.findIndex((line, i) => i >= currentLine && line.trim() === lineToRemove.trim());
243
- if (index !== -1) {
244
- result.splice(index, 1);
245
- }
246
- } else if (patchLine.startsWith('+')) {
247
- // Add line
248
- const lineToAdd = patchLine.substring(1);
249
- result.splice(currentLine, 0, lineToAdd);
250
- currentLine++;
251
- } else if (patchLine.startsWith(' ')) {
252
- // Context line - advance current line
253
- currentLine++;
254
- }
255
- }
256
-
257
- return result.join('\n');
258
- }
259
-
260
- /**
261
- * Create unified diff between original and modified content
262
- */
263
- private createUnifiedDiff(filePath: string, original: string, modified: string): string {
264
- const originalLines = original.split('\n');
265
- const modifiedLines = modified.split('\n');
266
-
267
- let diff = `--- a/${filePath}\n+++ b/${filePath}\n`;
268
-
269
- // Simple diff algorithm (in production, use a proper diff library)
270
- let i = 0, j = 0;
271
- while (i < originalLines.length || j < modifiedLines.length) {
272
- const originalLine = originalLines[i] || '';
273
- const modifiedLine = modifiedLines[j] || '';
274
-
275
- if (originalLine === modifiedLine) {
276
- diff += ` ${originalLine}\n`;
277
- i++;
278
- j++;
279
- } else {
280
- // Find next matching line
281
- let nextMatch = -1;
282
- for (let k = j + 1; k < modifiedLines.length; k++) {
283
- if (modifiedLines[k] === originalLine) {
284
- nextMatch = k;
285
- break;
286
- }
287
- }
288
-
289
- if (nextMatch !== -1) {
290
- // Lines were added
291
- for (let k = j; k < nextMatch; k++) {
292
- diff += `+${modifiedLines[k]}\n`;
293
- }
294
- j = nextMatch;
295
- } else {
296
- // Line was removed or changed
297
- if (i < originalLines.length) {
298
- diff += `-${originalLine}\n`;
299
- i++;
300
- }
301
- if (j < modifiedLines.length) {
302
- diff += `+${modifiedLine}\n`;
303
- j++;
304
- }
305
- }
306
- }
307
- }
308
-
309
- return diff;
310
- }
311
-
312
- /**
313
- * Colorize unified diff for better readability
314
- */
315
- private colorizeUnifiedDiff(diff: string): string {
316
- return diff
317
- .split('\n')
318
- .map(line => {
319
- if (line.startsWith('+++') || line.startsWith('---')) {
320
- return chalk.bold(line);
321
- } else if (line.startsWith('@@')) {
322
- return chalk.cyan(line);
323
- } else if (line.startsWith('+')) {
324
- return chalk.green(line);
325
- } else if (line.startsWith('-')) {
326
- return chalk.red(line);
327
- } else {
328
- return chalk.dim(line);
329
- }
330
- })
331
- .join('\n');
332
- }
333
-
334
- /**
335
- * Show detailed diff with more context
336
- */
337
- private async showDetailedDiff(fix: Fix): Promise<void> {
338
- console.log(chalk.cyan(`\nšŸ“‹ Detailed Diff for ${fix.filePath}\n`));
339
-
340
- try {
341
- const originalContent = await readFile(fix.filePath, 'utf8');
342
- const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
343
-
344
- const originalLines = originalContent.split('\n');
345
- const modifiedLines = modifiedContent.split('\n');
346
-
347
- console.log(chalk.white('Original:'));
348
- originalLines.forEach((line, i) => {
349
- console.log(chalk.dim(`${(i + 1).toString().padStart(3)} | `) + line);
350
- });
351
-
352
- console.log(chalk.white('\nModified:'));
353
- modifiedLines.forEach((line, i) => {
354
- console.log(chalk.dim(`${(i + 1).toString().padStart(3)} | `) + line);
355
- });
356
-
357
- console.log();
358
- } catch (error) {
359
- console.log(chalk.red(`Failed to show detailed diff: ${error instanceof Error ? error.message : 'Unknown error'}`));
360
- }
361
- }
362
- }
1
+ import { readFile, writeFile, copyFile } from 'fs/promises';
2
+ import { existsSync } from 'fs';
3
+ import { join, dirname } from 'path';
4
+ import { mkdir } from 'fs/promises';
5
+ import chalk from 'chalk';
6
+ import type { Fix } from '../types/index.js';
7
+
8
+ /**
9
+ * Fix manager for previewing and applying code fixes
10
+ */
11
+ export class FixManager {
12
+ private appliedFixes: Map<string, { original: string; backup: string }> = new Map();
13
+
14
+ /**
15
+ * Generate unified diff preview of proposed changes
16
+ */
17
+ async generatePreview(fix: Fix): Promise<string> {
18
+ try {
19
+ const originalContent = await readFile(fix.filePath, 'utf8');
20
+ const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
21
+
22
+ return this.createUnifiedDiff(fix.filePath, originalContent, modifiedContent);
23
+ } catch (error) {
24
+ throw new Error(`Failed to generate preview: ${error instanceof Error ? error.message : 'Unknown error'}`);
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Show interactive fix approval with clear change descriptions
30
+ */
31
+ async showFixPreview(fix: Fix): Promise<boolean> {
32
+ console.log(chalk.cyan(`\nšŸ”§ Fix Preview for ${fix.violation.feature} in ${fix.filePath}\n`));
33
+
34
+ // Show fix explanation
35
+ console.log(chalk.white('šŸ“‹ Fix Description:'));
36
+ console.log(chalk.dim(fix.explanation));
37
+ console.log();
38
+
39
+ // Show confidence score
40
+ const confidenceColor = fix.confidence >= 0.8 ? 'green' : fix.confidence >= 0.6 ? 'yellow' : 'red';
41
+ console.log(chalk.white('šŸŽÆ Confidence: ') + chalk[confidenceColor](`${Math.round(fix.confidence * 100)}%`));
42
+ console.log();
43
+
44
+ // Show unified diff
45
+ console.log(chalk.white('šŸ“ Changes:'));
46
+ const preview = await this.generatePreview(fix);
47
+ console.log(this.colorizeUnifiedDiff(preview));
48
+ console.log();
49
+
50
+ // Show human-readable preview
51
+ if (fix.preview) {
52
+ console.log(chalk.white('šŸ‘€ Summary:'));
53
+ console.log(chalk.dim(fix.preview));
54
+ console.log();
55
+ }
56
+
57
+ // Interactive approval
58
+ const { default: inquirer } = await import('inquirer');
59
+
60
+ const { action } = await inquirer.prompt([
61
+ {
62
+ type: 'list',
63
+ name: 'action',
64
+ message: 'What would you like to do with this fix?',
65
+ choices: [
66
+ { name: 'āœ… Apply this fix', value: 'apply' },
67
+ { name: 'šŸ‘€ Show detailed diff', value: 'detail' },
68
+ { name: 'āŒ Skip this fix', value: 'skip' },
69
+ { name: '🚫 Cancel all fixes', value: 'cancel' }
70
+ ]
71
+ }
72
+ ]);
73
+
74
+ switch (action) {
75
+ case 'apply':
76
+ return true;
77
+ case 'detail':
78
+ await this.showDetailedDiff(fix);
79
+ return await this.showFixPreview(fix); // Show preview again
80
+ case 'skip':
81
+ return false;
82
+ case 'cancel':
83
+ throw new Error('Fix application cancelled by user');
84
+ default:
85
+ return false;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Apply fixes to original files while preserving formatting and structure
91
+ */
92
+ async applyFix(fix: Fix): Promise<void> {
93
+ try {
94
+ // Create backup before applying fix
95
+ await this.createBackup(fix.filePath);
96
+
97
+ // Read original content
98
+ const originalContent = await readFile(fix.filePath, 'utf8');
99
+
100
+ // Apply patch to content
101
+ const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
102
+
103
+ // Write modified content back to file
104
+ await writeFile(fix.filePath, modifiedContent, 'utf8');
105
+
106
+ // Store fix information for potential rollback
107
+ this.appliedFixes.set(fix.filePath, {
108
+ original: originalContent,
109
+ backup: this.getBackupPath(fix.filePath)
110
+ });
111
+
112
+ console.log(chalk.green(`āœ… Applied fix to ${fix.filePath}`));
113
+ } catch (error) {
114
+ throw new Error(`Failed to apply fix: ${error instanceof Error ? error.message : 'Unknown error'}`);
115
+ }
116
+ }
117
+
118
+ /**
119
+ * Apply multiple fixes with batch processing
120
+ */
121
+ async applyFixes(fixes: Fix[]): Promise<{ applied: Fix[]; skipped: Fix[]; failed: { fix: Fix; error: string }[] }> {
122
+ const results = {
123
+ applied: [] as Fix[],
124
+ skipped: [] as Fix[],
125
+ failed: [] as { fix: Fix; error: string }[]
126
+ };
127
+
128
+ console.log(chalk.cyan(`\nšŸ”§ Applying ${fixes.length} fixes...\n`));
129
+
130
+ for (const fix of fixes) {
131
+ try {
132
+ const shouldApply = await this.showFixPreview(fix);
133
+
134
+ if (shouldApply) {
135
+ await this.applyFix(fix);
136
+ results.applied.push(fix);
137
+ } else {
138
+ results.skipped.push(fix);
139
+ console.log(chalk.yellow(`ā­ļø Skipped fix for ${fix.filePath}`));
140
+ }
141
+ } catch (error) {
142
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error';
143
+ results.failed.push({ fix, error: errorMessage });
144
+ console.log(chalk.red(`āŒ Failed to apply fix for ${fix.filePath}: ${errorMessage}`));
145
+ }
146
+ }
147
+
148
+ return results;
149
+ }
150
+
151
+ /**
152
+ * Rollback applied fixes
153
+ */
154
+ async rollbackFix(filePath: string): Promise<void> {
155
+ const fixInfo = this.appliedFixes.get(filePath);
156
+
157
+ if (!fixInfo) {
158
+ throw new Error(`No applied fix found for ${filePath}`);
159
+ }
160
+
161
+ try {
162
+ // Restore original content
163
+ await writeFile(filePath, fixInfo.original, 'utf8');
164
+
165
+ // Remove from applied fixes
166
+ this.appliedFixes.delete(filePath);
167
+
168
+ console.log(chalk.green(`āœ… Rolled back fix for ${filePath}`));
169
+ } catch (error) {
170
+ throw new Error(`Failed to rollback fix: ${error instanceof Error ? error.message : 'Unknown error'}`);
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Rollback all applied fixes
176
+ */
177
+ async rollbackAllFixes(): Promise<void> {
178
+ const filePaths = Array.from(this.appliedFixes.keys());
179
+
180
+ console.log(chalk.cyan(`\nšŸ”„ Rolling back ${filePaths.length} fixes...\n`));
181
+
182
+ for (const filePath of filePaths) {
183
+ try {
184
+ await this.rollbackFix(filePath);
185
+ } catch (error) {
186
+ console.log(chalk.red(`āŒ Failed to rollback ${filePath}: ${error instanceof Error ? error.message : 'Unknown error'}`));
187
+ }
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Get list of applied fixes
193
+ */
194
+ getAppliedFixes(): string[] {
195
+ return Array.from(this.appliedFixes.keys());
196
+ }
197
+
198
+ /**
199
+ * Create backup of original file
200
+ */
201
+ private async createBackup(filePath: string): Promise<void> {
202
+ const backupPath = this.getBackupPath(filePath);
203
+ const backupDir = dirname(backupPath);
204
+
205
+ // Ensure backup directory exists
206
+ if (!existsSync(backupDir)) {
207
+ await mkdir(backupDir, { recursive: true });
208
+ }
209
+
210
+ // Copy original file to backup location
211
+ await copyFile(filePath, backupPath);
212
+ }
213
+
214
+ /**
215
+ * Get backup file path
216
+ */
217
+ private getBackupPath(filePath: string): string {
218
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
219
+ return join('.baseguard', 'backups', `${filePath.replace(/[/\\]/g, '_')}.${timestamp}.backup`);
220
+ }
221
+
222
+ /**
223
+ * Apply patch to file content
224
+ */
225
+ private applyPatchToContent(originalContent: string, patch: string): string {
226
+ const lines = originalContent.split('\n');
227
+ const patchLines = patch.split('\n');
228
+
229
+ const result = [...lines];
230
+ let currentLine = 0;
231
+
232
+ for (const patchLine of patchLines) {
233
+ if (patchLine.startsWith('@@')) {
234
+ // Parse hunk header to get line numbers
235
+ const match = patchLine.match(/@@ -(\d+),?\d* \+(\d+),?\d* @@/);
236
+ if (match && match[2]) {
237
+ currentLine = parseInt(match[2]) - 1;
238
+ }
239
+ } else if (patchLine.startsWith('-')) {
240
+ // Remove line
241
+ const lineToRemove = patchLine.substring(1);
242
+ const index = result.findIndex((line, i) => i >= currentLine && line.trim() === lineToRemove.trim());
243
+ if (index !== -1) {
244
+ result.splice(index, 1);
245
+ }
246
+ } else if (patchLine.startsWith('+')) {
247
+ // Add line
248
+ const lineToAdd = patchLine.substring(1);
249
+ result.splice(currentLine, 0, lineToAdd);
250
+ currentLine++;
251
+ } else if (patchLine.startsWith(' ')) {
252
+ // Context line - advance current line
253
+ currentLine++;
254
+ }
255
+ }
256
+
257
+ return result.join('\n');
258
+ }
259
+
260
+ /**
261
+ * Create unified diff between original and modified content
262
+ */
263
+ private createUnifiedDiff(filePath: string, original: string, modified: string): string {
264
+ const originalLines = original.split('\n');
265
+ const modifiedLines = modified.split('\n');
266
+
267
+ let diff = `--- a/${filePath}\n+++ b/${filePath}\n`;
268
+
269
+ // Simple diff algorithm (in production, use a proper diff library)
270
+ let i = 0, j = 0;
271
+ while (i < originalLines.length || j < modifiedLines.length) {
272
+ const originalLine = originalLines[i] || '';
273
+ const modifiedLine = modifiedLines[j] || '';
274
+
275
+ if (originalLine === modifiedLine) {
276
+ diff += ` ${originalLine}\n`;
277
+ i++;
278
+ j++;
279
+ } else {
280
+ // Find next matching line
281
+ let nextMatch = -1;
282
+ for (let k = j + 1; k < modifiedLines.length; k++) {
283
+ if (modifiedLines[k] === originalLine) {
284
+ nextMatch = k;
285
+ break;
286
+ }
287
+ }
288
+
289
+ if (nextMatch !== -1) {
290
+ // Lines were added
291
+ for (let k = j; k < nextMatch; k++) {
292
+ diff += `+${modifiedLines[k]}\n`;
293
+ }
294
+ j = nextMatch;
295
+ } else {
296
+ // Line was removed or changed
297
+ if (i < originalLines.length) {
298
+ diff += `-${originalLine}\n`;
299
+ i++;
300
+ }
301
+ if (j < modifiedLines.length) {
302
+ diff += `+${modifiedLine}\n`;
303
+ j++;
304
+ }
305
+ }
306
+ }
307
+ }
308
+
309
+ return diff;
310
+ }
311
+
312
+ /**
313
+ * Colorize unified diff for better readability
314
+ */
315
+ private colorizeUnifiedDiff(diff: string): string {
316
+ return diff
317
+ .split('\n')
318
+ .map(line => {
319
+ if (line.startsWith('+++') || line.startsWith('---')) {
320
+ return chalk.bold(line);
321
+ } else if (line.startsWith('@@')) {
322
+ return chalk.cyan(line);
323
+ } else if (line.startsWith('+')) {
324
+ return chalk.green(line);
325
+ } else if (line.startsWith('-')) {
326
+ return chalk.red(line);
327
+ } else {
328
+ return chalk.dim(line);
329
+ }
330
+ })
331
+ .join('\n');
332
+ }
333
+
334
+ /**
335
+ * Show detailed diff with more context
336
+ */
337
+ private async showDetailedDiff(fix: Fix): Promise<void> {
338
+ console.log(chalk.cyan(`\nšŸ“‹ Detailed Diff for ${fix.filePath}\n`));
339
+
340
+ try {
341
+ const originalContent = await readFile(fix.filePath, 'utf8');
342
+ const modifiedContent = this.applyPatchToContent(originalContent, fix.patch);
343
+
344
+ const originalLines = originalContent.split('\n');
345
+ const modifiedLines = modifiedContent.split('\n');
346
+
347
+ console.log(chalk.white('Original:'));
348
+ originalLines.forEach((line, i) => {
349
+ console.log(chalk.dim(`${(i + 1).toString().padStart(3)} | `) + line);
350
+ });
351
+
352
+ console.log(chalk.white('\nModified:'));
353
+ modifiedLines.forEach((line, i) => {
354
+ console.log(chalk.dim(`${(i + 1).toString().padStart(3)} | `) + line);
355
+ });
356
+
357
+ console.log();
358
+ } catch (error) {
359
+ console.log(chalk.red(`Failed to show detailed diff: ${error instanceof Error ? error.message : 'Unknown error'}`));
360
+ }
361
+ }
362
+ }