|
| 1 | +/** |
| 2 | + * File Logger Adapter |
| 3 | + * |
| 4 | + * Implements the Logger interface writing to a local file. |
| 5 | + * This is the adapter layer - contains actual I/O operations. |
| 6 | + */ |
| 7 | + |
| 8 | +import { homedir } from 'node:os'; |
| 9 | +import { appendFileSync } from 'node:fs'; |
| 10 | +import { join } from 'node:path'; |
| 11 | +import type { Logger, LogCategory } from '../types/index.js'; |
| 12 | + |
| 13 | +const LOG_PREFIX = '[opencode-sync]'; |
| 14 | +const LOG_DIR = join(homedir(), '.local/share/opencode/log'); |
| 15 | +const LOG_FILE = join(LOG_DIR, 'opencode-sync.log'); |
| 16 | + |
| 17 | +/** Format a log message with timestamp and optional category */ |
| 18 | +function formatMessage(message: string, category?: LogCategory): string { |
| 19 | + const timestamp = new Date().toISOString(); |
| 20 | + const categoryTag = category ? `[${category}] ` : ''; |
| 21 | + return `${timestamp} ${LOG_PREFIX} ${categoryTag}${message}`; |
| 22 | +} |
| 23 | + |
| 24 | +/** Write a message to the log file */ |
| 25 | +function writeToFile(message: string): void { |
| 26 | + try { |
| 27 | + appendFileSync(LOG_FILE, message + '\n'); |
| 28 | + } catch { |
| 29 | + // Fallback to console if file write fails |
| 30 | + console.error(message); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** File-based logger implementation */ |
| 35 | +export const fileLogger: Logger = { |
| 36 | + log(message: string, category?: LogCategory): void { |
| 37 | + writeToFile(formatMessage(message, category)); |
| 38 | + }, |
| 39 | + |
| 40 | + debug(message: string, category?: LogCategory): void { |
| 41 | + writeToFile(formatMessage(`[DEBUG] ${message}`, category)); |
| 42 | + }, |
| 43 | + |
| 44 | + info(message: string, category?: LogCategory): void { |
| 45 | + writeToFile(formatMessage(`[INFO] ${message}`, category)); |
| 46 | + }, |
| 47 | + |
| 48 | + warn(message: string, category?: LogCategory): void { |
| 49 | + writeToFile(formatMessage(`[WARN] ${message}`, category)); |
| 50 | + }, |
| 51 | + |
| 52 | + error(message: string, category?: LogCategory): void { |
| 53 | + writeToFile(formatMessage(`[ERROR] ${message}`, category)); |
| 54 | + }, |
| 55 | + |
| 56 | + startOperation(name: string): (result?: string) => void { |
| 57 | + const start = Date.now(); |
| 58 | + writeToFile(formatMessage(`Starting: ${name}`, 'SYNC')); |
| 59 | + return (result?: string): void => { |
| 60 | + const duration = Date.now() - start; |
| 61 | + const suffix = result ? ` - ${result}` : ''; |
| 62 | + writeToFile(formatMessage(`Completed: ${name} (${String(duration)}ms)${suffix}`, 'SYNC')); |
| 63 | + }; |
| 64 | + }, |
| 65 | +}; |
| 66 | + |
| 67 | +/** |
| 68 | + * Convenience exports for direct use (backwards compatibility). |
| 69 | + * Prefer injecting the Logger interface when possible. |
| 70 | + */ |
| 71 | +export function log(message: string, category?: LogCategory): void { |
| 72 | + fileLogger.log(message, category); |
| 73 | +} |
| 74 | + |
| 75 | +export function syncLog(message: string): void { |
| 76 | + fileLogger.log(message, 'SYNC'); |
| 77 | +} |
| 78 | + |
| 79 | +export function logProgress(message: string): void { |
| 80 | + fileLogger.log(message, 'REPO'); |
| 81 | +} |
| 82 | + |
| 83 | +export function syncDebug(message: string): void { |
| 84 | + fileLogger.debug(message, 'SYNC'); |
| 85 | +} |
| 86 | + |
| 87 | +export function startOperation(name: string): (result?: string) => void { |
| 88 | + return fileLogger.startOperation(name); |
| 89 | +} |
| 90 | + |
| 91 | +/** Log setup instructions when configuration is missing */ |
| 92 | +export function logSetupInstructions(): void { |
| 93 | + log('To configure, either:', 'PLUGIN'); |
| 94 | + log(' 1. Set GITHUB_TOKEN environment variable (with repo scope)', 'PLUGIN'); |
| 95 | + log(' 2. Create ~/.config/opencode/opencode-sync.json with token', 'PLUGIN'); |
| 96 | +} |
0 commit comments