|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Postlink hook for time-tracking plugin. |
| 6 | + * |
| 7 | + * Creates ~/time_tracking directory with required subdirectories |
| 8 | + * and symlinks .opencode/time_tracking -> ~/time_tracking so that |
| 9 | + * time tracking data is stored globally and shared across projects. |
| 10 | + */ |
| 11 | +export default function postlink({ targetDir, homeDir, log }) { |
| 12 | + const timeTrackingDir = path.join(homeDir, 'time_tracking'); |
| 13 | + const symlinkPath = path.join(targetDir, 'time_tracking'); |
| 14 | + |
| 15 | + // 1. Create ~/time_tracking + subdirs if missing |
| 16 | + for (const subdir of ['', 'bookings', 'charts', 'reports']) { |
| 17 | + const dirPath = path.join(timeTrackingDir, subdir); |
| 18 | + if (!fs.existsSync(dirPath)) { |
| 19 | + fs.mkdirSync(dirPath, { recursive: true }); |
| 20 | + log.success(`Created ${dirPath.replace(homeDir, '~')}`); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + // 2. Symlink .opencode/time_tracking -> ~/time_tracking |
| 25 | + let targetExists = false; |
| 26 | + let targetIsSymlink = false; |
| 27 | + try { |
| 28 | + const stat = fs.lstatSync(symlinkPath); |
| 29 | + targetExists = true; |
| 30 | + targetIsSymlink = stat.isSymbolicLink(); |
| 31 | + } catch { |
| 32 | + targetExists = false; |
| 33 | + } |
| 34 | + |
| 35 | + if (targetExists && targetIsSymlink) { |
| 36 | + const existing = fs.readlinkSync(symlinkPath); |
| 37 | + if (existing === timeTrackingDir) return; // already correct |
| 38 | + fs.unlinkSync(symlinkPath); |
| 39 | + fs.symlinkSync(timeTrackingDir, symlinkPath); |
| 40 | + log.warning(`time_tracking -> ~/time_tracking (overridden)`); |
| 41 | + } else if (targetExists) { |
| 42 | + log.warning(`time_tracking (real directory, not overriding)`); |
| 43 | + } else { |
| 44 | + fs.symlinkSync(timeTrackingDir, symlinkPath); |
| 45 | + log.success(`time_tracking -> ~/time_tracking`); |
| 46 | + } |
| 47 | +} |
0 commit comments