extract shared file and path utilities#34
Merged
Merged
Conversation
- Create shared/file.utils.ts for safe file operations - Create shared/path.utils.ts for path manipulation - Refactor 6 modules to use shared utilities - Add comprehensive tests and implementation plan Eliminates duplicate file reading patterns and path matching logic. close #31
- Add tsconfig.build.json excluding test files - Update build script to use tsconfig.build.json - Fix type assertion in test file refs #31
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extract Shared File and Path Utilities to Eliminate Duplication
📋 Summary
Refactors duplicated file reading patterns and path matching logic into shared utility modules, following DRY (Don't Repeat Yourself) principles. This eliminates code duplication across 6 modules and ensures consistent error handling and path manipulation behavior.
Closes #31
🎯 Problem
DRY Violations
During rapid feature development, identical logic patterns were duplicated across multiple modules:
File Reading Pattern Duplication (6 locations)
catchblocksPath Matching Logic Duplication (3 locations)
/and\manually)Business Impact
✨ Solution
Shared Utility Modules
Created two new utility modules that consolidate duplicated patterns:
shared/file.utils.ts) - Safe file operationsshared/path.utils.ts) - Path manipulation1. File Utilities (
shared/file.utils.ts)Functions:
safeReadFile(filePath): Returnsstring | nullnullif file doesn't exist (ENOENT)tryReadFile(filePath): Returnsstring | undefinedundefinedon any errorsafeReadDirWithTypes(dirPath): ReturnsDirent[]Key Features:
2. Path Utilities (
shared/path.utils.ts)Functions:
normalizePath(filePath): Returnsstring\) to forward slashes (/)pathContainsSegment(filePath, segment): Returnsboolean/dir/and\dir\checksKey Features:
3. Refactored Modules
File Reading Refactoring (6 files):
ignore.parser.tsfs.readFile+ try-catch → return emptysafeReadFile→ return null checkcontext.loader.ts(loadContextFile)fs.readFile+ try-catch → return nulltryReadFile→ return null checkcontext.loader.ts(getAllFiles)fs.readdir+ try-catch → skipsafeReadDirWithTypes→ iteratepackage.analyzer.tsfs.readFile+ try-catch → return nullsafeReadFile→ return null checkconfig.analyzer.ts(×3)fs.readFile+ try-catch → skiptryReadFile→ undefined checkPath Matching Refactoring (3 files):
code.sampler.tspathContainsDir()helperpathContainsSegment()from utilsdirectory.analyzer.tsconfig.analyzer.ts🧪 Testing
Test Coverage
File Utils: 8 tests covering:
Path Utils: 10 tests covering:
Total: 18 new tests, all passing ✅
Existing Tests
All existing tests continue to pass:
🎯 Benefits
1. Reduced Maintenance Burden
File reading logic exists in a single location. Changes (e.g., adding logging, changing error handling) require updates in one place.
2. Consistent Behavior
All modules use the same error handling patterns, preventing behavioral drift.
3. Improved Code Quality
4. Easier Onboarding
New developers learn one implementation instead of multiple.
5. Reduced Bug Risk
Single implementation means bugs are fixed once, not in multiple places.
📖 Usage Examples
Before (Duplicated Pattern)
After (Shared Utilities)
Path Matching Example
Before:
After:
🔗 Related Documentation
📝 Design Decisions
Why Three File Reading Functions?
safeReadFile: For cases where "file not found" is expected and should return null (e.g., optional config files)tryReadFile: For skip-on-error patterns where any error should be silently ignored (e.g., reading multiple config files)safeReadDirWithTypes: For directory scanning where empty array is appropriate fallbackWhy Check ENOENT Specifically?
Why Path Normalization?
\, Unix uses/Why Case-Insensitive Segment Matching?
✅ Acceptance Criteria
🚀 Impact
Code Quality Metrics
Risk Assessment
shared/has no dependencies onconfig/oranalyzer/