| 1 | #!/usr/bin/env node |
| 2 | /** |
| 3 | * CLI helper: discard pending manual edits from the buffer without applying. |
| 4 | * |
| 5 | * Reads .impeccable/live/pending-manual-edits.json, drops entries, writes back. |
| 6 | * No source-file writes. Use this when the user wants to throw away unsaved |
| 7 | * manual edits. |
| 8 | * |
| 9 | * Trigger: only when the user explicitly asks the AI to discard / throw away / |
| 10 | * clear pending manual edits. |
| 11 | * |
| 12 | * Usage: |
| 13 | * node live-discard-manual-edits.mjs # discard all pending |
| 14 | * node live-discard-manual-edits.mjs --page-url=/ # discard only entries for "/" |
| 15 | * |
| 16 | * Output JSON: { discarded: N, entries: [...discardedEntries], totalCount: N } |
| 17 | */ |
| 18 | |
| 19 | import { readBuffer, removeEntries, truncateBuffer } from './live-manual-edits-buffer.mjs'; |
| 20 | |
| 21 | function argVal(args, name) { |
| 22 | const prefix = name + '='; |
| 23 | for (const a of args) { |
| 24 | if (a === name) return true; |
| 25 | if (a.startsWith(prefix)) return a.slice(prefix.length); |
| 26 | } |
| 27 | return null; |
| 28 | } |
| 29 | |
| 30 | const args = process.argv.slice(2); |
| 31 | if (args.includes('--help') || args.includes('-h')) { |
| 32 | console.log('Usage: node live-discard-manual-edits.mjs [--page-url=<url>]'); |
| 33 | process.exit(0); |
| 34 | } |
| 35 | |
| 36 | const pageUrlFilter = argVal(args, '--page-url'); |
| 37 | const cwd = process.cwd(); |
| 38 | |
| 39 | let discarded; |
| 40 | let entries; |
| 41 | const buffer = readBuffer(cwd); |
| 42 | if (pageUrlFilter) { |
| 43 | entries = buffer.entries.filter((entry) => entry.pageUrl === pageUrlFilter); |
| 44 | discarded = removeEntries(cwd, (entry) => entry.pageUrl === pageUrlFilter); |
| 45 | } else { |
| 46 | entries = buffer.entries; |
| 47 | discarded = truncateBuffer(cwd); |
| 48 | } |
| 49 | |
| 50 | const remaining = readBuffer(cwd).entries.reduce((n, e) => n + e.ops.length, 0); |
| 51 | console.log(JSON.stringify({ discarded, entries, totalCount: remaining })); |
| 52 |