UNPKG

cc-status

Version:

Focused Claude Code statusline with real subscription usage, context monitoring, and time projections

117 lines (114 loc) 3.93 kB
#!/usr/bin/env node // src/services/claude-paths.ts import { readdir } from "fs/promises"; import { existsSync } from "fs"; import { join } from "path"; import { homedir } from "os"; function getClaudePaths() { const paths = []; const homeDir = homedir(); const defaultPath = join(homeDir, ".claude"); if (existsSync(defaultPath)) { paths.push(defaultPath); } return paths; } async function findProjectPaths(claudePaths) { const projectPaths = []; for (const claudePath of claudePaths) { const projectsDir = join(claudePath, "projects"); if (existsSync(projectsDir)) { try { const entries = await readdir(projectsDir, { withFileTypes: true }); for (const entry of entries) { if (entry.isDirectory()) { const projectPath = join(projectsDir, entry.name); projectPaths.push(projectPath); } } } catch (error) { console.debug(`Failed to read projects directory ${projectsDir}:`, error); } } } return projectPaths; } async function findTranscriptFile(sessionId) { const claudePaths = getClaudePaths(); const projectPaths = await findProjectPaths(claudePaths); for (const projectPath of projectPaths) { const transcriptPath = join(projectPath, `${sessionId}.jsonl`); if (existsSync(transcriptPath)) { return transcriptPath; } } return null; } async function findTodaysTranscripts() { const claudePaths = getClaudePaths(); const projectPaths = await findProjectPaths(claudePaths); const today = /* @__PURE__ */ new Date(); today.setHours(0, 0, 0, 0); const todaysTranscripts = []; for (const projectPath of projectPaths) { try { const entries = await readdir(projectPath, { withFileTypes: true }); for (const entry of entries) { if (entry.isFile() && entry.name.endsWith(".jsonl")) { const transcriptPath = join(projectPath, entry.name); try { const stats = await import("fs/promises").then((fs) => fs.stat(transcriptPath)); const modifiedDate = new Date(stats.mtime); modifiedDate.setHours(0, 0, 0, 0); if (modifiedDate.getTime() >= today.getTime()) { todaysTranscripts.push(transcriptPath); } } catch (statError) { console.debug(`Failed to stat transcript file ${transcriptPath}:`, statError); } } } } catch (error) { console.debug(`Failed to read project directory ${projectPath}:`, error); } } return todaysTranscripts; } async function findTranscriptsForDate(date) { const claudePaths = getClaudePaths(); const projectPaths = await findProjectPaths(claudePaths); const targetDate = new Date(date); targetDate.setHours(0, 0, 0, 0); const nextDay = new Date(targetDate); nextDay.setDate(nextDay.getDate() + 1); const dayTranscripts = []; for (const projectPath of projectPaths) { try { const entries = await readdir(projectPath, { withFileTypes: true }); for (const entry of entries) { if (entry.isFile() && entry.name.endsWith(".jsonl")) { const transcriptPath = join(projectPath, entry.name); try { const stats = await import("fs/promises").then((fs) => fs.stat(transcriptPath)); const modifiedTime = stats.mtime.getTime(); if (modifiedTime >= targetDate.getTime() && modifiedTime < nextDay.getTime()) { dayTranscripts.push(transcriptPath); } } catch (statError) { console.debug(`Failed to stat transcript file ${transcriptPath}:`, statError); } } } } catch (error) { console.debug(`Failed to read project directory ${projectPath}:`, error); } } return dayTranscripts; } export { getClaudePaths, findProjectPaths, findTranscriptFile, findTodaysTranscripts, findTranscriptsForDate };