UNPKG

cost-claude

Version:

Claude Code cost monitoring, analytics, and optimization toolkit

80 lines 2.99 kB
import path from 'path'; export class ProjectParser { static extractProjectName(filePath) { const parts = filePath.split(path.sep); const projectsIndex = parts.indexOf('projects'); if (projectsIndex >= 0 && projectsIndex < parts.length - 1) { const projectFolder = parts[projectsIndex + 1]; if (projectFolder) { return this.formatProjectName(projectFolder); } } return 'unknown'; } static formatProjectName(folderName) { const parts = folderName.split('-').filter(p => p.length > 0); if (parts.length > 3) { const lastThree = parts.slice(-3).join('-'); const githubIndex = parts.indexOf('github'); if (githubIndex >= 0 && githubIndex < parts.length - 3) { const org = parts[githubIndex + 2]; const repo = parts.slice(githubIndex + 3).join('-'); if (org && repo) { return `${org}/${repo}`; } } if (lastThree.length > 20) { return '...' + lastThree.slice(-20); } return lastThree; } return parts.join('-'); } static groupByProject(messages) { const grouped = new Map(); messages.forEach(msg => { let project = 'unknown'; if (msg.cwd) { const cwdParts = msg.cwd.split(path.sep); const repoIndex = cwdParts.lastIndexOf('github.com'); if (repoIndex >= 0 && repoIndex < cwdParts.length - 2) { const org = cwdParts[repoIndex + 1]; const repo = cwdParts[repoIndex + 2]; if (org && repo) { project = `${org}/${repo}`; } } else { project = cwdParts[cwdParts.length - 1] || 'unknown'; } } if (!grouped.has(project)) { grouped.set(project, []); } grouped.get(project).push(msg); }); return grouped; } static getProjectFromMessage(message, filePath) { if (message.cwd) { const cwdParts = message.cwd.split(path.sep); const repoIndex = cwdParts.lastIndexOf('github.com'); if (repoIndex >= 0 && repoIndex < cwdParts.length - 2) { const org = cwdParts[repoIndex + 1]; const repo = cwdParts[repoIndex + 2]; if (org && repo) { return `${org}/${repo}`; } } const lastPart = cwdParts[cwdParts.length - 1]; if (lastPart && lastPart !== 'undefined') { return lastPart; } } if (filePath) { return this.extractProjectName(filePath); } return 'unknown'; } } //# sourceMappingURL=project-parser.js.map