cc-status
Version:
Focused Claude Code statusline with real subscription usage, context monitoring, and time projections
126 lines (123 loc) • 3.91 kB
JavaScript
// 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 envPath = process.env.CLAUDE_CONFIG_DIR;
if (envPath) {
envPath.split(",").forEach((path) => {
const trimmedPath = path.trim();
if (existsSync(trimmedPath)) {
paths.push(trimmedPath);
}
});
}
if (paths.length === 0) {
const homeDir = homedir();
const configPath = join(homeDir, ".config", "claude");
const claudePath = join(homeDir, ".claude");
if (existsSync(configPath)) {
paths.push(configPath);
} else if (existsSync(claudePath)) {
paths.push(claudePath);
}
}
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) {
}
}
}
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) {
}
}
}
} catch (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) {
}
}
}
} catch (error) {
}
}
return dayTranscripts;
}
export {
getClaudePaths,
findProjectPaths,
findTranscriptFile,
findTodaysTranscripts,
findTranscriptsForDate
};