|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +NodeSwap is a Windows-only Node.js version manager written in C# (.NET 8.0). It's similar to NVM but specifically designed for Windows. The application uses symlinks to manage different Node.js versions and requires administrator privileges for symlink creation. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +### Solution Structure |
| 12 | +- **NodeSwap** - Main console application (.NET 8.0-windows) |
| 13 | +- **NodeSwap.Tests** - MSTest unit tests using Shouldly assertions |
| 14 | +- **NodeSwap.Installer** - WiX installer project for Windows |
| 15 | + |
| 16 | +### Core Components |
| 17 | +- **Program.cs** - Entry point with dependency injection setup using Microsoft.Extensions.DependencyInjection |
| 18 | +- **GlobalContext.cs** - Shared configuration and paths (storage, symlink, version tracking) |
| 19 | +- **Commands/** - CLI command implementations using DotMake.CommandLine with full dependency injection |
| 20 | +- **Interfaces/** - Abstraction interfaces for external dependencies (file system, process elevation, etc.) |
| 21 | +- **Services/** - Service implementations wrapping external dependencies |
| 22 | +- **NodeJs.cs** & **NodeJsWebApi.cs** - Node.js version management and API integration |
| 23 | +- **Utils/** - Helper utilities for console output, process elevation, and list operations |
| 24 | + |
| 25 | +### Dependency Injection Architecture |
| 26 | +The application uses comprehensive dependency injection for improved testability: |
| 27 | + |
| 28 | +#### Interfaces (NodeSwap/Interfaces/) |
| 29 | +- **IProcessElevation** - Windows process elevation and administrator checks |
| 30 | +- **IConsoleWriter** - Console output abstraction |
| 31 | +- **IFileSystem** - File system operations (read/write/delete/symlinks) |
| 32 | +- **INodeJsWebApi** - Node.js API integration for version lookup and downloads |
| 33 | +- **INodeJs** - Local Node.js version management |
| 34 | + |
| 35 | +#### Services (NodeSwap/Services/) |
| 36 | +- **ProcessElevationService** - Windows-specific elevation implementation |
| 37 | +- **ConsoleWriterService** - Console.WriteLine wrapper |
| 38 | +- **FileSystemService** - System.IO wrapper with symlink support |
| 39 | +- **NodeJsWebApiService** - HTTP-based Node.js API client |
| 40 | +- **NodeJsService** - Local version management implementation |
| 41 | + |
| 42 | +### Key Dependencies |
| 43 | +- **DotMake.CommandLine** - Primary CLI framework |
| 44 | +- **System.CommandLine** - Additional command line support |
| 45 | +- **NuGet.Versioning** - Version parsing and comparison |
| 46 | +- **ShellProgressBar** - Progress indication for downloads |
| 47 | +- **Microsoft.Extensions.DependencyInjection** - Dependency injection container |
| 48 | + |
| 49 | +## Development Commands |
| 50 | + |
| 51 | +### Building |
| 52 | +```bash |
| 53 | +dotnet build NodeSwap.sln |
| 54 | +dotnet build -c Release NodeSwap.sln |
| 55 | +``` |
| 56 | + |
| 57 | +### Testing |
| 58 | +```bash |
| 59 | +dotnet test NodeSwap.Tests/ |
| 60 | +dotnet test --verbosity normal |
| 61 | +``` |
| 62 | + |
| 63 | +### Publishing |
| 64 | +```bash |
| 65 | +dotnet publish NodeSwap/NodeSwap.csproj -c Release |
| 66 | +``` |
| 67 | + |
| 68 | +### Running Locally |
| 69 | +```bash |
| 70 | +dotnet run --project NodeSwap/ |
| 71 | +``` |
| 72 | + |
| 73 | +## Environment Requirements |
| 74 | + |
| 75 | +### Prerequisites |
| 76 | +- .NET 8.0 runtime or SDK |
| 77 | +- Windows operating system (uses Windows-specific symlinks) |
| 78 | +- `NODESWAP_STORAGE` environment variable must be set to a valid directory path |
| 79 | + |
| 80 | +### Admin Privileges |
| 81 | +The `use` command requires administrator privileges to create/update symlinks. The application will prompt for elevation when needed. |
| 82 | + |
| 83 | +## Key Implementation Details |
| 84 | + |
| 85 | +### Version Management |
| 86 | +- Node.js versions are downloaded and stored in `%NODESWAP_STORAGE%` |
| 87 | +- Active version tracked via symlink at `%NODESWAP_STORAGE%/current` |
| 88 | +- Version history maintained in `last-used` and `previous-used` files |
| 89 | +- Supports fuzzy version matching (e.g., "22" → "22.x.x") |
| 90 | + |
| 91 | +### Command Structure |
| 92 | +All commands use dependency injection and inherit from DotMake.CommandLine patterns: |
| 93 | +- `list` - Show installed versions |
| 94 | +- `avail [min_version]` - Show available downloads |
| 95 | +- `install <version>` - Download and install Node.js version |
| 96 | +- `uninstall <version>` - Remove installed version |
| 97 | +- `use <version>` - Switch active version (requires admin) |
| 98 | +- `prev` - Switch to previously used version |
| 99 | +- `file` - Use or create a .nodeswap file to manage Node.js version for the current directory |
| 100 | + |
| 101 | +#### Command Dependencies |
| 102 | +All commands now accept required dependencies through constructor injection: |
| 103 | +- **UseCommand** - Takes GlobalContext, INodeJs, IProcessElevation, IConsoleWriter, IFileSystem |
| 104 | +- **InstallCommand** - Takes GlobalContext, INodeJsWebApi, INodeJs, IConsoleWriter, IFileSystem |
| 105 | +- **PrevCommand** - Takes GlobalContext, INodeJs, IProcessElevation, IConsoleWriter, IFileSystem |
| 106 | +- **FileCommand** - Takes GlobalContext, INodeJs, IProcessElevation, IConsoleWriter, IFileSystem |
| 107 | + |
| 108 | +### Testing Architecture |
| 109 | + |
| 110 | +#### Test Framework |
| 111 | +- **MSTest** framework with **Shouldly** assertions |
| 112 | +- Comprehensive mock-based testing for isolation |
| 113 | +- 60+ unit tests covering all commands and utilities |
| 114 | + |
| 115 | +#### Mock Services (NodeSwap.Tests/TestUtils/MockServices.cs) |
| 116 | +Custom mock implementations for complete test isolation: |
| 117 | +- **MockProcessElevation** - Configurable administrator status and elevation behavior |
| 118 | +- **MockConsoleWriter** - Captures output and error messages for verification |
| 119 | +- **MockFileSystem** - In-memory file system with symlink simulation |
| 120 | +- **MockNodeJs** - Configurable installed versions and active version tracking |
| 121 | +- **MockNodeJsWebApi** - Configurable API responses and exception testing |
| 122 | + |
| 123 | +#### Test Structure |
| 124 | +- **Command Tests** - All commands have comprehensive test coverage using mocks |
| 125 | +- **Utility Tests** - Version parsing, list operations, and helper functions |
| 126 | +- **Integration-style Tests** - Some tests use MockServices with dependency injection |
| 127 | +- **Error Scenario Testing** - Exception handling, validation, and edge cases |
| 128 | + |
| 129 | +#### Key Test Files |
| 130 | +- **FileCommandTests.cs** - Tests for .nodeswap file management (6 tests) |
| 131 | +- **UseCommandTests.cs** - Tests for version switching with mocks (7 tests) |
| 132 | +- **UseCommandTestsWithMocks.cs** - Additional comprehensive use command tests (10 tests) |
| 133 | +- **InstallCommandTestsWithMocks.cs** - Installation command tests (8 tests) |
| 134 | +- **PrevCommandTests.cs** - Previous version switching tests (6 tests) |
| 135 | +- **ListCommandTests.cs**, **UninstallCommandTests.cs**, **AvailCommandTests.cs** - Additional command coverage |
| 136 | + |
| 137 | +#### Running Tests |
| 138 | +```bash |
| 139 | +# Run all tests |
| 140 | +dotnet test NodeSwap.Tests/ --verbosity normal |
| 141 | + |
| 142 | +# Run specific test class |
| 143 | +dotnet test NodeSwap.Tests/ --filter "FileCommandTests" --verbosity normal |
| 144 | + |
| 145 | +# Run tests before making changes to core logic |
| 146 | +dotnet test NodeSwap.Tests/ |
| 147 | +``` |
0 commit comments