Skip to content

Refactor: Migrate to src/ directory structure for better project organization #2

@ml0-1337

Description

@ml0-1337

Description

I'd like to propose migrating the project to use a src/ directory structure to improve code organization and align with TypeScript ecosystem conventions. This change would group all source code under a single directory while keeping configuration files at the root level.

Current Structure

ccusage/
├── index.ts              # Mixed with config files
├── calculate-cost.ts
├── calculate-cost.test.ts
├── data-loader.ts
├── data-loader.test.ts
├── logger.ts
├── shared-args.ts
├── types.ts
├── utils.ts
├── utils.test.ts
├── commands/
│   ├── daily.ts
│   └── session.ts
├── package.json          # Config files
├── tsconfig.json
└── ...

Proposed Structure

ccusage/
├── src/                  # All source code
│   ├── index.ts
│   ├── commands/        # CLI commands
│   │   ├── daily.ts
│   │   └── session.ts
│   ├── core/            # Core business logic
│   │   ├── calculate-cost.ts
│   │   ├── calculate-cost.test.ts
│   │   ├── data-loader.ts
│   │   ├── data-loader.test.ts
│   │   └── types.ts
│   ├── cli/             # CLI utilities
│   │   └── shared-args.ts
│   └── utils/           # General utilities
│       ├── logger.ts
│       ├── utils.ts
│       └── utils.test.ts
├── dist/                # Build output
├── package.json         # Config at root
├── tsconfig.json
└── ...

Benefits

  1. Better Organization: Logical grouping of related functionality
  2. Cleaner Root: Separates source code from configuration files
  3. Scalability: Easier to add new features without cluttering the root
  4. Convention: Aligns with TypeScript/Node.js ecosystem standards
  5. Developer Experience: Clearer project structure for contributors

Implementation Plan

  1. Create new directory structure under src/
  2. Move files using git mv to preserve history
  3. Update import paths throughout the codebase
  4. Update build configuration in package.json
  5. Update tsconfig.json to include src/
  6. Ensure all tests pass
  7. Verify build output remains unchanged

Changes Required

Import Updates: ~15 import statements need path updates

// Example: commands/daily.ts
// Before
import { loadUsageData } from "../data-loader.ts";

// After
import { loadUsageData } from "../core/data-loader.ts";

Configuration Updates:

  • package.json: Update build script to use ./src/index.ts
  • tsconfig.json: Add "include": ["src/**/*"]

Impact

  • No breaking changes for npm package users (dist/ output unchanged)
  • No API changes
  • No runtime behavior changes
  • ✅ Maintains co-located test structure

Questions

  1. Would you be open to this organizational change?
  2. Any concerns about the proposed structure?
  3. Should we keep the current test co-location or prefer a separate tests/ directory?

Implementation Offer

I'm happy to implement this change if you think it would benefit the project. I can:

What I'll deliver:

  • Create a PR with all the necessary changes
  • Ensure all tests pass (bun test)
  • Verify the build works correctly (bun run build)
  • Test CLI functionality (bun run report daily/session)
  • Include the migration script I've prepared for transparency

My approach:

  1. Fork and create a feature branch
  2. Run the automated migration script
  3. Manually verify and adjust any edge cases
  4. Update documentation as needed
  5. Submit PR with detailed description

I've already prepared a migration script that automates most of the work while preserving git history. Happy to make any adjustments based on your feedback!

Let me know if you'd like me to proceed.

Checklist

  • I've verified this doesn't break existing functionality
  • I'm willing to implement this change
  • I've considered backward compatibility
  • I've reviewed the impact on development workflow

Alternative: Minimal Migration

If the full restructure seems too much, here's a minimal alternative:

ccusage/
├── src/
│   ├── index.ts
│   ├── *.ts            # All other .ts files
│   ├── *.test.ts       # All test files
│   └── commands/       # Keep commands subdirectory
├── dist/
└── [config files]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions