0

Copy Codes

A CLI tool to streamline code extraction for AI workflows, formatting text files and tracing TypeScript dependencies with ease. Perfect for feature focused coding.

In today's AI driven coding world, feeding the right code to AI models can feel like herding cats. Manually copying files, tracking dependencies, or dumping an entire project while dodging .gitignore clutter is a nightmare. That's why I built Copy Codes, a CLI tool to automate code extraction and formatting, making it a breeze to share code with AI for debugging, feature building, or learning. The real MVP? The ts-deps subcommand, which grabs a TypeScript file and its nested imports in one go. Let's dive into how this tool saves the day.

Why This Tool Exists

AI models thrive on context, but getting that context to them is a pain. Here's what I aimed to fix:

  • Manual File Copying: Copying multiple files by hand to share with AI is slow and error prone, especially for large projects.
  • Dependency Chaos: Tracking down all the files a feature depends on is tedious and impractical.
  • Clean AI Input: AI needs focused, relevant code respecting .gitignore, excluding binaries, and formatted clearly without the mess of an entire project.
  • Feature Focused Work: Most coding tasks are feature specific, so grabbing just the relevant files is key to efficient AI collaboration.

Technologies Used

  • Node.js – Drives file system operations and core logic.
  • Commander.js – Powers the slick CLI interface.
  • ignore – Handles .gitignore style filtering.
  • ts-morph – Traces TypeScript imports for ts-deps.
  • clipboardy – Copies output to your clipboard.

Solutions

Here's how Copy Codes tackles these challenges, with a spotlight on ts-deps.

Smart Directory Scanning

The tool scans your project, grabbing only text files while respecting .gitignore and custom include/exclude patterns. It's like a librarian who knows exactly which books you need.

Conceptual Flow

  • Scan the directory recursively, respecting max depth.
  • Apply .gitignore rules and user-defined patterns.
  • Collect text files, skipping binaries or ignored paths.
  • Format output with file paths and line counts.

Command: copy-codes -i node_modules -a '*.ts'

TypeScript Dependency Superpower

The ts-deps subcommand is the heart of this tool for AI workflows. Need to debug or build a feature? Just point to a TypeScript file, and ts-deps grabs it plus all its nested imports automatically. No more chasing down import statements or missing critical files. This makes feeding AI the exact context for a feature or bug fix a snap.

Conceptual Flow

  • Parse the input TypeScript file using ts-morph.
  • Recursively trace all import statements within the project.
  • Apply ignore/accept filters to exclude irrelevant files.
  • Output the file and its dependencies, formatted for AI.

Command: copy-codes ts-deps src/index.ts

Flexible Output for AI

Whether you're pasting code into an AI chat or saving it for docs, the output is clean and AI friendly:

  • Plain Text: Files separated with headers (path, line count).
  • Markdown: Code blocks with metadata for easy pasting.

Markdown Output

```ts file="src/index.ts" lines="10"
console.log("Hello, world!");
```

Enable markdown with: copy-codes -m

Bonus Features

  • Copy output directly to your clipboard with -c.
  • Visualize your project structure with -t.
  • Limit scan depth with -d for focused extraction.

Results

  • Share feature specific code with AI in seconds, no manual file searching.
  • The ts-deps command quickly gathers essential TypeScript files for easy debugging and feature development.
  • It respects .gitignore ensuring only relevant files are sent to the AI.
  • Automate code extraction and formatting to focus more on coding.

Installation

Ready to level up your AI driven coding? Install Copy Codes globally via npm and start automating your workflow:

npm i -g copy-codes

Check out the project on GitHub for more details and examples!