Summary
Magic values are scattered throughout the codebase, making it harder to maintain consistency and increasing the risk of typos or inconsistent changes. These values should be extracted into a centralized constants file to serve as a single source of truth.
Current State
The following magic values are duplicated across multiple files:
Cache-related constants
File extension constants
- Config file extension:
".json"
- Used in
/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:51 (filtering JSON files)
- Used in
/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:57 (removing extension)
- Used in
/Users/george/src/gsong/ccmcp/src/cleanup.ts:62 (filtering cache files)
- Used in
/Users/george/src/gsong/ccmcp/src/cleanup.ts:194 (constructing config path)
- Used in
/Users/george/src/gsong/ccmcp/src/cleanup.ts:304 (filtering JSON files)
Directory name constants
Proposed Solution
Create a new file src/constants.ts with well-named, exported constants:
/**
* Shared constants used throughout the application
*/
/** Prefix for cache file names */
export const CACHE_FILE_PREFIX = "selections-";
/** File extension for configuration files */
export const CONFIG_FILE_EXTENSION = ".json";
/** Current version of the cache file format */
export const CACHE_VERSION = 1;
/** Name of the Claude configuration directory */
export const CLAUDE_DIR = ".claude";
/** Name of the MCP configurations subdirectory */
export const MCP_CONFIGS_DIR = "mcp-configs";
Benefits
- Single source of truth: Changes to these values only need to be made in one place
- Easier maintenance: Clear, searchable constant names make the code more maintainable
- Better discoverability: Developers can quickly find all configurable values in one file
- Reduced error risk: Eliminates typos from manual string duplication
- Better IDE support: Constants can be auto-completed and easily refactored
Implementation Steps
- Create
src/constants.ts with the constants listed above
- Update all files that use these magic values to import from the constants file:
src/selection-cache.ts
src/cleanup.ts
src/mcp-scanner.ts
src/index.ts
src/console-selector.ts
src/tui/index.tsx (ConfigSelector component)
- Run tests to ensure no regressions:
pnpm run test
- Run type checking:
pnpm run fix
Additional Notes
This refactoring will make future changes (like supporting different cache versions or changing directory structures) much easier and safer to implement.
Summary
Magic values are scattered throughout the codebase, making it harder to maintain consistency and increasing the risk of typos or inconsistent changes. These values should be extracted into a centralized constants file to serve as a single source of truth.
Current State
The following magic values are duplicated across multiple files:
Cache-related constants
Cache file prefix:
"selections-"/Users/george/src/gsong/ccmcp/src/selection-cache.ts:54(getCacheFilePath function)/Users/george/src/gsong/ccmcp/src/selection-cache.ts:111(clearCache function)/Users/george/src/gsong/ccmcp/src/cleanup.ts:62(getAllCacheFiles function)Cache version:
1/Users/george/src/gsong/ccmcp/src/selection-cache.ts:8(SelectionCache interface)/Users/george/src/gsong/ccmcp/src/selection-cache.ts:67(loadSelections function)/Users/george/src/gsong/ccmcp/src/selection-cache.ts:97(saveSelections function)/Users/george/src/gsong/ccmcp/src/cleanup.ts:75(loadCacheFile function)File extension constants
".json"/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:51(filtering JSON files)/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:57(removing extension)/Users/george/src/gsong/ccmcp/src/cleanup.ts:62(filtering cache files)/Users/george/src/gsong/ccmcp/src/cleanup.ts:194(constructing config path)/Users/george/src/gsong/ccmcp/src/cleanup.ts:304(filtering JSON files)Directory name constants
Claude directory:
".claude"/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:31/Users/george/src/gsong/ccmcp/src/index.ts:176/Users/george/src/gsong/ccmcp/src/index.ts:210MCP configs directory:
"mcp-configs"/Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:31/Users/george/src/gsong/ccmcp/src/index.ts:176/Users/george/src/gsong/ccmcp/src/index.ts:210Proposed Solution
Create a new file
src/constants.tswith well-named, exported constants:Benefits
Implementation Steps
src/constants.tswith the constants listed abovesrc/selection-cache.tssrc/cleanup.tssrc/mcp-scanner.tssrc/index.tssrc/console-selector.tssrc/tui/index.tsx(ConfigSelector component)pnpm run testpnpm run fixAdditional Notes
This refactoring will make future changes (like supporting different cache versions or changing directory structures) much easier and safer to implement.