Skip to content

refactor: extract magic values into shared constants file #56

@gsong

Description

@gsong

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-"

    • Used in /Users/george/src/gsong/ccmcp/src/selection-cache.ts:54 (getCacheFilePath function)
    • Used in /Users/george/src/gsong/ccmcp/src/selection-cache.ts:111 (clearCache function)
    • Used in /Users/george/src/gsong/ccmcp/src/cleanup.ts:62 (getAllCacheFiles function)
  • Cache version: 1

    • Used in /Users/george/src/gsong/ccmcp/src/selection-cache.ts:8 (SelectionCache interface)
    • Used in /Users/george/src/gsong/ccmcp/src/selection-cache.ts:67 (loadSelections function)
    • Used in /Users/george/src/gsong/ccmcp/src/selection-cache.ts:97 (saveSelections function)
    • Used in /Users/george/src/gsong/ccmcp/src/cleanup.ts:75 (loadCacheFile function)

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

  • Claude directory: ".claude"

    • Used in /Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:31
    • Used in /Users/george/src/gsong/ccmcp/src/index.ts:176
    • Used in /Users/george/src/gsong/ccmcp/src/index.ts:210
  • MCP configs directory: "mcp-configs"

    • Used in /Users/george/src/gsong/ccmcp/src/mcp-scanner.ts:31
    • Used in /Users/george/src/gsong/ccmcp/src/index.ts:176
    • Used in /Users/george/src/gsong/ccmcp/src/index.ts:210

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

  1. Single source of truth: Changes to these values only need to be made in one place
  2. Easier maintenance: Clear, searchable constant names make the code more maintainable
  3. Better discoverability: Developers can quickly find all configurable values in one file
  4. Reduced error risk: Eliminates typos from manual string duplication
  5. Better IDE support: Constants can be auto-completed and easily refactored

Implementation Steps

  1. Create src/constants.ts with the constants listed above
  2. 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)
  3. Run tests to ensure no regressions: pnpm run test
  4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions