-
-
Notifications
You must be signed in to change notification settings - Fork 430
refactor: consolidate scattered constants into centralized consts.internal.ts #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5738c5f
5ee5cdb
7431c1e
def291d
5be5275
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,67 @@ | ||
| import { homedir } from 'node:os'; | ||
|
|
||
| /** | ||
| * URL for LiteLLM's model pricing and context window data | ||
| */ | ||
| export const LITELLM_PRICING_URL | ||
| = 'https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json'; | ||
|
|
||
| /** | ||
| * Default number of recent days to include when filtering blocks | ||
| * Used in both session blocks and commands for consistent behavior | ||
| */ | ||
| export const DEFAULT_RECENT_DAYS = 3; | ||
|
|
||
| /** | ||
| * Threshold percentage for showing usage warnings in blocks command (80%) | ||
| * When usage exceeds this percentage of limits, warnings are displayed | ||
| */ | ||
| export const BLOCKS_WARNING_THRESHOLD = 0.8; | ||
|
|
||
| /** | ||
| * Terminal width threshold for switching to compact display mode in blocks command | ||
| * Below this width, tables use more compact formatting | ||
| */ | ||
| export const BLOCKS_COMPACT_WIDTH_THRESHOLD = 120; | ||
|
|
||
| /** | ||
| * Default terminal width when stdout.columns is not available in blocks command | ||
| * Used as fallback for responsive table formatting | ||
| */ | ||
| export const BLOCKS_DEFAULT_TERMINAL_WIDTH = 120; | ||
|
|
||
| /** | ||
| * Threshold percentage for considering costs as matching (0.1% tolerance) | ||
| * Used in debug cost validation to allow for minor calculation differences | ||
| */ | ||
| export const DEBUG_MATCH_THRESHOLD_PERCENT = 0.1; | ||
|
|
||
| /** | ||
| * Default Claude data directory path (~/.claude) | ||
| * Used as base path for loading usage data from JSONL files | ||
| */ | ||
| export const DEFAULT_CLAUDE_CODE_PATH = '.claude'; | ||
|
|
||
| /** | ||
| * Claude projects directory name within the data directory | ||
| * Contains subdirectories for each project with usage data | ||
| */ | ||
| export const CLAUDE_PROJECTS_DIR_NAME = 'projects'; | ||
|
|
||
| /** | ||
| * JSONL file glob pattern for finding usage data files | ||
| * Used to recursively find all JSONL files in project directories | ||
| */ | ||
| export const USAGE_DATA_GLOB_PATTERN = '**/*.jsonl'; | ||
|
|
||
| /** | ||
| * Default port for MCP server HTTP transport | ||
| * Used when no port is specified for MCP server communication | ||
| */ | ||
| export const MCP_DEFAULT_PORT = 8080; | ||
|
|
||
| /** | ||
| * User's home directory path | ||
| * Centralized access to OS home directory for consistent path building | ||
| */ | ||
| export const USER_HOME_DIR = homedir(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ import { createFixture } from 'fs-fixture'; | |
| import { isDirectorySync } from 'path-type'; | ||
| import { glob } from 'tinyglobby'; | ||
| import { z } from 'zod'; | ||
| import { CLAUDE_PROJECTS_DIR_NAME, DEFAULT_CLAUDE_CODE_PATH, USAGE_DATA_GLOB_PATTERN, USER_HOME_DIR } from './consts.internal.js'; | ||
| import { logger } from './logger.ts'; | ||
| import { | ||
| PricingFetcher, | ||
|
|
@@ -47,35 +48,30 @@ import { | |
| versionSchema, | ||
| } from './types.internal.ts'; | ||
|
|
||
| /** | ||
| * Default Claude data directory path (~/.claude) | ||
| */ | ||
| const DEFAULT_CLAUDE_CODE_PATH = path.join(homedir(), '.claude'); | ||
|
|
||
| /** | ||
| * Default path for Claude data directory | ||
| * Uses environment variable CLAUDE_CONFIG_DIR if set, otherwise defaults to ~/.claude | ||
| */ | ||
| export function getDefaultClaudePath(): string { | ||
| const envClaudeCodePath = (process.env.CLAUDE_CONFIG_DIR ?? '').trim(); | ||
| if (envClaudeCodePath === '') { | ||
| return DEFAULT_CLAUDE_CODE_PATH; | ||
| return path.join(USER_HOME_DIR, DEFAULT_CLAUDE_CODE_PATH); | ||
| } | ||
|
|
||
| // First validate that the CLAUDE_CONFIG_DIR itself exists and is a directory | ||
| if (!isDirectorySync(envClaudeCodePath)) { | ||
| throw new Error( | ||
| `CLAUDE_CONFIG_DIR path is not a valid directory: ${envClaudeCodePath}. | ||
| Please set CLAUDE_CONFIG_DIR to a valid directory path, or ensure ${DEFAULT_CLAUDE_CODE_PATH} exists. | ||
| Please set CLAUDE_CONFIG_DIR to a valid directory path, or ensure ${path.join(USER_HOME_DIR, DEFAULT_CLAUDE_CODE_PATH)} exists. | ||
| `.trim(), | ||
| ); | ||
| } | ||
|
|
||
| const claudeCodeProjectsPath = path.join(envClaudeCodePath, 'projects'); | ||
| const claudeCodeProjectsPath = path.join(envClaudeCodePath, CLAUDE_PROJECTS_DIR_NAME); | ||
| if (!isDirectorySync(claudeCodeProjectsPath)) { | ||
| throw new Error( | ||
| `Claude data directory does not exist: ${claudeCodeProjectsPath}. | ||
| Please set CLAUDE_CONFIG_DIR to a valid path, or ensure ${DEFAULT_CLAUDE_CODE_PATH} exists. | ||
| Please set CLAUDE_CONFIG_DIR to a valid path, or ensure ${path.join(USER_HOME_DIR, DEFAULT_CLAUDE_CODE_PATH)} exists. | ||
|
Comment on lines
73
to
+74
|
||
| `.trim(), | ||
| ); | ||
| } | ||
|
|
@@ -586,8 +582,8 @@ export async function loadDailyUsageData( | |
| options?: LoadOptions, | ||
| ): Promise<DailyUsage[]> { | ||
| const claudePath = options?.claudePath ?? getDefaultClaudePath(); | ||
| const claudeDir = path.join(claudePath, 'projects'); | ||
| const files = await glob(['**/*.jsonl'], { | ||
| const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME); | ||
| const files = await glob([USAGE_DATA_GLOB_PATTERN], { | ||
| cwd: claudeDir, | ||
| absolute: true, | ||
| }); | ||
|
|
@@ -708,8 +704,8 @@ export async function loadSessionData( | |
| options?: LoadOptions, | ||
| ): Promise<SessionUsage[]> { | ||
| const claudePath = options?.claudePath ?? getDefaultClaudePath(); | ||
| const claudeDir = path.join(claudePath, 'projects'); | ||
| const files = await glob(['**/*.jsonl'], { | ||
| const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME); | ||
| const files = await glob([USAGE_DATA_GLOB_PATTERN], { | ||
| cwd: claudeDir, | ||
| absolute: true, | ||
| }); | ||
|
|
@@ -944,8 +940,8 @@ export async function loadSessionBlockData( | |
| options?: LoadOptions, | ||
| ): Promise<SessionBlock[]> { | ||
| const claudePath = options?.claudePath ?? getDefaultClaudePath(); | ||
| const claudeDir = path.join(claudePath, 'projects'); | ||
| const files = await glob(['**/*.jsonl'], { | ||
| const claudeDir = path.join(claudePath, CLAUDE_PROJECTS_DIR_NAME); | ||
| const files = await glob([USAGE_DATA_GLOB_PATTERN], { | ||
| cwd: claudeDir, | ||
| absolute: true, | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,16 +1,12 @@ | ||||||
| import { readFile } from 'node:fs/promises'; | ||||||
| import { homedir } from 'node:os'; | ||||||
| import path from 'node:path'; | ||||||
| import { createFixture } from 'fs-fixture'; | ||||||
| import { glob } from 'tinyglobby'; | ||||||
| import { CLAUDE_PROJECTS_DIR_NAME, DEBUG_MATCH_THRESHOLD_PERCENT, DEFAULT_CLAUDE_CODE_PATH, USAGE_DATA_GLOB_PATTERN, USER_HOME_DIR } from './consts.internal.js'; | ||||||
|
||||||
| import { CLAUDE_PROJECTS_DIR_NAME, DEBUG_MATCH_THRESHOLD_PERCENT, DEFAULT_CLAUDE_CODE_PATH, USAGE_DATA_GLOB_PATTERN, USER_HOME_DIR } from './consts.internal.js'; | |
| import { CLAUDE_PROJECTS_DIR_NAME, DEBUG_MATCH_THRESHOLD_PERCENT, DEFAULT_CLAUDE_CODE_PATH, USAGE_DATA_GLOB_PATTERN, USER_HOME_DIR } from './consts.internal.ts'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSDoc for
DEFAULT_CLAUDE_CODE_PATHcould be more precise. It currently states it's the "Default Claude data directory path (~/.claude)". However, with the refactoring, this constant now holds just the directory name (e.g.,.claude), and the full path is constructed usingUSER_HOME_DIR(e.g.,path.join(USER_HOME_DIR, DEFAULT_CLAUDE_CODE_PATH)).Consider updating the JSDoc to reflect that this constant represents the name or relative path component of the default Claude data directory, typically located within the user's home directory.