|
| 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 | +Ez Game Audio Conversion is a batch audio converter designed for game developers. It converts audio files between multiple formats (WAV, MP3, OGG, FLAC, AIFF, M4A) with automatic handling of audio metadata, loop point tags, multi-threaded processing, and intelligent codec selection. The tool uses ffmpeg/ffprobe and Node.js worker threads to maximize performance. |
| 8 | + |
| 9 | +## Development Commands |
| 10 | + |
| 11 | +### Running the Application |
| 12 | + |
| 13 | +```bash |
| 14 | +npm run dev # Run once with ts-node |
| 15 | +npm run dev:watch # Auto-reload on file changes (nodemon) |
| 16 | +npm run dev:js # Build TypeScript then run compiled JS |
| 17 | +``` |
| 18 | + |
| 19 | +### Building |
| 20 | + |
| 21 | +```bash |
| 22 | +npm run build:ts # Compile TypeScript to dist/ |
| 23 | +npm run build:watch # Watch mode compilation |
| 24 | +npm run package # Create standalone executables (requires build:ts first) |
| 25 | +npm run build # Full build: TypeScript + package |
| 26 | +npm run clean # Remove dist/ and release/ folders |
| 27 | +``` |
| 28 | + |
| 29 | +### Testing |
| 30 | + |
| 31 | +```bash |
| 32 | +npm test # Run all tests sequentially |
| 33 | +npm run test:ci # CI mode with open handle detection |
| 34 | +npm run test:coverage # Generate coverage report |
| 35 | +npm run smoke # Package and run smoke tests |
| 36 | + |
| 37 | +# Run single test file: |
| 38 | +npx jest src/__tests__/app.test.js |
| 39 | + |
| 40 | +# Run single test by name: |
| 41 | +npx jest -t "test name pattern" |
| 42 | +``` |
| 43 | + |
| 44 | +### Code Quality |
| 45 | + |
| 46 | +```bash |
| 47 | +npm run lint # ESLint check |
| 48 | +npm run format # Auto-format with Prettier |
| 49 | +``` |
| 50 | + |
| 51 | +Pre-commit hooks automatically run ESLint and Prettier on staged files via Husky + lint-staged. |
| 52 | + |
| 53 | +## High-Level Architecture |
| 54 | + |
| 55 | +### Promise Chain Execution Flow |
| 56 | + |
| 57 | +The application uses a sequential promise chain in `src/app.ts`: |
| 58 | + |
| 59 | +``` |
| 60 | +getUserInput → searchFiles → createConversionList → convertFiles → finalize |
| 61 | +``` |
| 62 | + |
| 63 | +Each function passes a `Settings` object through the chain, modifying it with results. |
| 64 | + |
| 65 | +### Worker Thread Pool Pattern |
| 66 | + |
| 67 | +The conversion process (`src/converterManager.ts`) uses Node.js worker threads: |
| 68 | + |
| 69 | +- Pool size = CPU core count (auto-detected) |
| 70 | +- Workers pull jobs from a shared queue using `.pop()` |
| 71 | +- Each worker spawns ffmpeg processes for individual conversions |
| 72 | +- Parent thread collects results and aggregates success/failure counts |
| 73 | + |
| 74 | +**Critical Detail**: Worker path resolution differs between development and packaged builds: |
| 75 | + |
| 76 | +```typescript |
| 77 | +const workerPath = runningPkg |
| 78 | + ? join(__dirname, 'converterWorker.js') // Packaged |
| 79 | + : join(__dirname, '..', 'dist', 'converterWorker.js') // Development |
| 80 | +``` |
| 81 | + |
| 82 | +### Key Architectural Components |
| 83 | + |
| 84 | +1. **app.ts** - Entry point, initializes `globalThis.env`, orchestrates promise chain |
| 85 | +2. **getUserInput.ts** - Interactive CLI prompts for input/output paths and formats |
| 86 | +3. **searchFiles.ts** - Recursive file system search for audio files |
| 87 | +4. **createConversionList.ts** - Builds conversion jobs with conflict resolution (overwrite/rename/skip) |
| 88 | +5. **converterManager.ts** - Spawns and manages worker thread pool |
| 89 | +6. **converterWorker.ts** - Worker thread that executes ffmpeg commands and extracts metadata |
| 90 | +7. **metadataService.ts** - Handles metadata extraction, loop point conversion, and formatting |
| 91 | +8. **utils.ts** - CSV logging, disk space checks, readline interface |
| 92 | +9. **finalize.ts** - Results display and auto-restart functionality |
| 93 | + |
| 94 | +### Global Environment Configuration |
| 95 | + |
| 96 | +The application uses `globalThis.env` for runtime configuration (defined in `src/types/global.ts`): |
| 97 | + |
| 98 | +- Platform detection (Windows/Mac/Linux) |
| 99 | +- Execution context (dev/debug/pkg) |
| 100 | +- CPU core count |
| 101 | +- Command-line arguments |
| 102 | + |
| 103 | +This is initialized once at startup in `src/app.ts`. |
| 104 | + |
| 105 | +## Important Patterns and Conventions |
| 106 | + |
| 107 | +### Loop Point Sample Rate Conversion |
| 108 | + |
| 109 | +When converting to Opus format, loop points must be adjusted for sample rate changes: |
| 110 | + |
| 111 | +- Opus only supports specific sample rates: 8k, 12k, 16k, 24k, 48k |
| 112 | +- The converter automatically adjusts `LOOPSTART` and `LOOPLENGTH` values based on the ratio between original and target sample rates |
| 113 | +- This preserves timing accuracy when loop-enabled audio changes sample rates |
| 114 | + |
| 115 | +**Limitation**: Loop points can only be written to OGG, FLAC, MP3, and AIFF. They can be read from all formats but cannot be written to WAV or M4A. |
| 116 | + |
| 117 | +### Metadata Preservation |
| 118 | + |
| 119 | +`src/metadataService.ts` extracts 60+ metadata fields using ffprobe and reapplies them via ffmpeg arguments. This includes: |
| 120 | + |
| 121 | +- Standard tags (title, artist, album, etc.) |
| 122 | +- iTunes-specific metadata |
| 123 | +- Podcast metadata |
| 124 | +- ReplayGain tags |
| 125 | +- Loop points (LOOPSTART/LOOPLENGTH) |
| 126 | + |
| 127 | +String sanitization prevents command injection vulnerabilities. |
| 128 | + |
| 129 | +### Child Process Security |
| 130 | + |
| 131 | +Workers use `spawn()` instead of `exec()` to prevent shell injection. Commands are built as arrays: |
| 132 | + |
| 133 | +```typescript |
| 134 | +const args = ['-i', inputFile, '-c:a', codec, outputFile]; |
| 135 | +spawn('ffmpeg', args, { windowsVerbatimArguments: true }); |
| 136 | +``` |
| 137 | + |
| 138 | +The `windowsVerbatimArguments: true` option ensures Windows compatibility. |
| 139 | + |
| 140 | +### Conflict Resolution System |
| 141 | + |
| 142 | +When output files already exist, users can: |
| 143 | + |
| 144 | +- `o` - Overwrite (won't overwrite self) |
| 145 | +- `r` - Rename with `-copy(n)` suffix |
| 146 | +- `s` - Skip |
| 147 | +- `oa`/`ra`/`sa` - Apply choice to all subsequent conflicts |
| 148 | + |
| 149 | +This is handled in `src/createConversionList.ts`. |
| 150 | + |
| 151 | +### CSV Logging |
| 152 | + |
| 153 | +Success logs go to `logs.csv`, errors go to `error.csv`. If files are busy, the logger automatically increments filenames: `logs(1).csv`, `logs(2).csv`, etc. |
| 154 | + |
| 155 | +Logs include: timestamp, exit code, input path, output path, error messages. |
| 156 | + |
| 157 | +## Package Configuration |
| 158 | + |
| 159 | +The `pkg` section in `package.json` creates standalone executables: |
| 160 | + |
| 161 | +```json |
| 162 | +{ |
| 163 | + "targets": ["node24-win-x64", "node24-linux-x64"], |
| 164 | + "assets": ["dist/converterWorker.js"], |
| 165 | + "outputPath": "release" |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +**Critical**: `converterWorker.js` must be included as an asset since worker threads load it at runtime. |
| 170 | + |
| 171 | +**External Requirements**: Executables require `ffmpeg.exe` and `ffprobe.exe` in the same directory. |
| 172 | + |
| 173 | +## Testing Architecture |
| 174 | + |
| 175 | +Tests are located in `src/__tests__/`: |
| 176 | + |
| 177 | +- **Unit tests**: `*.test.js` - Test individual modules |
| 178 | +- **Integration tests**: `integration/*.test.js` - Test complete workflows |
| 179 | +- **Test utilities**: `test-utils/` - Shared helpers and mock data generators |
| 180 | + |
| 181 | +All tests run sequentially (`--runInBand`) to prevent file system race conditions. |
| 182 | + |
| 183 | +Coverage thresholds: 40% branches, 50% functions, 55% lines/statements. |
| 184 | + |
| 185 | +## TypeScript Configuration |
| 186 | + |
| 187 | +- Target: ES2022 |
| 188 | +- Module: CommonJS (required for pkg compatibility) |
| 189 | +- Strict mode enabled |
| 190 | +- Source maps generated for debugging |
| 191 | +- Output directory: `dist/` |
| 192 | + |
| 193 | +Uses a hybrid approach: TypeScript for source, CommonJS `module.exports` for Node.js compatibility. |
| 194 | + |
| 195 | +## Format-Specific Codec Selection |
| 196 | + |
| 197 | +Codecs are hardcoded in `src/converterWorker.ts`: |
| 198 | + |
| 199 | +- **MP3**: libmp3lame with VBR quality 4 (~160kbps) |
| 200 | +- **OGG**: libopus (preferred) or libvorbis with quality 6 |
| 201 | +- **M4A**: AAC at 256k fixed bitrate |
| 202 | +- **WAV/AIFF**: pcm_s16le (16-bit uncompressed) |
| 203 | +- **FLAC**: Compression level 9 (maximum) |
| 204 | + |
| 205 | +To modify bitrates or codecs, edit `getCodec()` and `getBitrate()` functions in `src/converterWorker.ts`. |
| 206 | + |
| 207 | +## Cross-Platform Considerations |
| 208 | + |
| 209 | +- Line endings: LF enforced via `.gitattributes` (CRLF only for `.bat`/`.ps1`) |
| 210 | +- Git hooks: Husky pre-commit hook uses `npx` (not hardcoded Windows paths) |
| 211 | +- Executable extensions: `.exe` suffix automatically added on Windows |
| 212 | +- File paths: Use `path.join()` and `path.resolve()` for cross-platform compatibility |
0 commit comments