()
| 54 | } |
| 55 | |
| 56 | export function commitCustomize(): void { |
| 57 | const pendingPath = getPendingPath(); |
| 58 | if (!fs.existsSync(pendingPath)) { |
| 59 | throw new Error('No active customize session. Run startCustomize() first.'); |
| 60 | } |
| 61 | |
| 62 | const pending = parse( |
| 63 | fs.readFileSync(pendingPath, 'utf-8'), |
| 64 | ) as PendingCustomize; |
| 65 | const cwd = process.cwd(); |
| 66 | |
| 67 | // Find files that changed |
| 68 | const changedFiles: string[] = []; |
| 69 | for (const relativePath of Object.keys(pending.file_hashes)) { |
| 70 | const fullPath = path.join(cwd, relativePath); |
| 71 | if (!fs.existsSync(fullPath)) { |
| 72 | // File was deleted — counts as changed |
| 73 | changedFiles.push(relativePath); |
| 74 | continue; |
| 75 | } |
| 76 | const currentHash = computeFileHash(fullPath); |
| 77 | if (currentHash !== pending.file_hashes[relativePath]) { |
| 78 | changedFiles.push(relativePath); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (changedFiles.length === 0) { |
| 83 | console.log( |
| 84 | 'No files changed during customize session. Nothing to commit.', |
| 85 | ); |
| 86 | fs.unlinkSync(pendingPath); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | // Generate unified diff for each changed file |
| 91 | const baseDir = path.join(cwd, BASE_DIR); |
| 92 | let combinedPatch = ''; |
| 93 | |
| 94 | for (const relativePath of changedFiles) { |
| 95 | const basePath = path.join(baseDir, relativePath); |
| 96 | const currentPath = path.join(cwd, relativePath); |
| 97 | |
| 98 | // Use /dev/null if either side doesn't exist |
| 99 | const oldPath = fs.existsSync(basePath) ? basePath : '/dev/null'; |
| 100 | const newPath = fs.existsSync(currentPath) ? currentPath : '/dev/null'; |
| 101 | |
| 102 | try { |
| 103 | const diff = execFileSync('diff', ['-ruN', oldPath, newPath], { |
| 104 | encoding: 'utf-8', |
| 105 | }); |
| 106 | combinedPatch += diff; |
| 107 | } catch (err: unknown) { |
| 108 | const execErr = err as { status?: number; stdout?: string }; |
| 109 | if (execErr.status === 1 && execErr.stdout) { |
| 110 | // diff exits 1 when files differ — that's expected |
| 111 | combinedPatch += execErr.stdout; |
| 112 | } else if (execErr.status === 2) { |
| 113 | throw new Error( |
no test coverage detected