|
| 1 | +/** |
| 2 | + * Snapshot CLI command. |
| 3 | + * Takes a point-in-time snapshot of what changed after a task completes. |
| 4 | + */ |
| 5 | + |
| 6 | +import { Command } from 'commander'; |
| 7 | +import chalk from 'chalk'; |
| 8 | +import { ContextCapture } from '../../core/worktree/capture.js'; |
| 9 | + |
| 10 | +export function createSnapshotCommand(): Command { |
| 11 | + const cmd = new Command('snapshot') |
| 12 | + .alias('snap') |
| 13 | + .description('Point-in-time snapshot of work (what changed and why)'); |
| 14 | + |
| 15 | + // Capture current state |
| 16 | + cmd |
| 17 | + .command('save') |
| 18 | + .alias('s') |
| 19 | + .description('Save a snapshot of current branch state') |
| 20 | + .option('-t, --task <name>', 'Task name or description') |
| 21 | + .option( |
| 22 | + '-b, --base <branch>', |
| 23 | + 'Base branch to diff against (default: auto-detect)' |
| 24 | + ) |
| 25 | + .option( |
| 26 | + '-d, --decision <decisions...>', |
| 27 | + 'Key decisions made during this task' |
| 28 | + ) |
| 29 | + .option('--json', 'Output as JSON') |
| 30 | + .action((options) => { |
| 31 | + const capture = new ContextCapture(); |
| 32 | + |
| 33 | + const result = capture.capture({ |
| 34 | + task: options.task, |
| 35 | + baseBranch: options.base, |
| 36 | + decisions: options.decision, |
| 37 | + }); |
| 38 | + |
| 39 | + if (options.json) { |
| 40 | + console.log(JSON.stringify(result, null, 2)); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + console.log(chalk.green('\nSnapshot saved.\n')); |
| 45 | + console.log(chalk.gray(` Branch: ${result.branch}`)); |
| 46 | + console.log(chalk.gray(` Base: ${result.baseBranch}`)); |
| 47 | + console.log(chalk.gray(` Changed: ${result.filesChanged.length} files`)); |
| 48 | + console.log(chalk.gray(` Created: ${result.filesCreated.length} files`)); |
| 49 | + console.log(chalk.gray(` Deleted: ${result.filesDeleted.length} files`)); |
| 50 | + console.log(chalk.gray(` Commits: ${result.commits.length}`)); |
| 51 | + |
| 52 | + if (result.decisions.length > 0) { |
| 53 | + console.log(chalk.cyan('\n Decisions:')); |
| 54 | + result.decisions.forEach((d) => console.log(chalk.gray(` - ${d}`))); |
| 55 | + } |
| 56 | + |
| 57 | + if (result.duration) { |
| 58 | + console.log(chalk.gray(` Duration: ${result.duration}`)); |
| 59 | + } |
| 60 | + |
| 61 | + console.log(chalk.gray(`\n Saved: ${result.id}`)); |
| 62 | + }); |
| 63 | + |
| 64 | + // List captures |
| 65 | + cmd |
| 66 | + .command('list') |
| 67 | + .alias('ls') |
| 68 | + .description('List recent snapshots') |
| 69 | + .option('-n, --limit <n>', 'Number of captures to show', '10') |
| 70 | + .option('--json', 'Output as JSON') |
| 71 | + .action((options) => { |
| 72 | + const capture = new ContextCapture(); |
| 73 | + const captures = capture.list(parseInt(options.limit)); |
| 74 | + |
| 75 | + if (captures.length === 0) { |
| 76 | + console.log(chalk.yellow('No snapshots found.')); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (options.json) { |
| 81 | + console.log(JSON.stringify(captures, null, 2)); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + console.log(chalk.cyan(`\nRecent Snapshots (${captures.length}):\n`)); |
| 86 | + |
| 87 | + for (const cap of captures) { |
| 88 | + const date = new Date(cap.timestamp).toLocaleDateString(); |
| 89 | + const files = cap.filesChanged.length + cap.filesCreated.length; |
| 90 | + console.log( |
| 91 | + chalk.gray( |
| 92 | + ` ${date} ${cap.branch.padEnd(30)} ${files} files ${cap.commits.length} commits` |
| 93 | + ) |
| 94 | + ); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + // Show a specific capture or latest |
| 99 | + cmd |
| 100 | + .command('show [branch]') |
| 101 | + .description('Show snapshot details (latest or by branch)') |
| 102 | + .option('--json', 'Output as JSON') |
| 103 | + .action((branch, options) => { |
| 104 | + const capturer = new ContextCapture(); |
| 105 | + const result = capturer.getLatest(branch); |
| 106 | + |
| 107 | + if (!result) { |
| 108 | + console.log( |
| 109 | + chalk.yellow( |
| 110 | + branch |
| 111 | + ? `No snapshot found for branch: ${branch}` |
| 112 | + : 'No snapshots found.' |
| 113 | + ) |
| 114 | + ); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (options.json) { |
| 119 | + console.log(JSON.stringify(result, null, 2)); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + console.log(capturer.format(result)); |
| 124 | + }); |
| 125 | + |
| 126 | + return cmd; |
| 127 | +} |
0 commit comments