|
| 1 | +# Contributing to envdiff |
| 2 | + |
| 3 | +Thank you for considering contributing to envdiff! This document provides guidelines and instructions for contributing. |
| 4 | + |
| 5 | +## Development Setup |
| 6 | + |
| 7 | +1. **Fork and clone the repository** |
| 8 | + ```bash |
| 9 | + git clone https://github.com/YOUR_USERNAME/env-diff-cli.git |
| 10 | + cd env-diff-cli |
| 11 | + ``` |
| 12 | + |
| 13 | +2. **Install dependencies** |
| 14 | + ```bash |
| 15 | + npm install |
| 16 | + ``` |
| 17 | + |
| 18 | +3. **Build the project** |
| 19 | + ```bash |
| 20 | + npm run build |
| 21 | + ``` |
| 22 | + |
| 23 | +4. **Run tests** |
| 24 | + ```bash |
| 25 | + npm test |
| 26 | + ``` |
| 27 | + |
| 28 | +## Project Structure |
| 29 | + |
| 30 | +``` |
| 31 | +src/ |
| 32 | + cli.ts # Commander setup, command registration |
| 33 | + commands/ |
| 34 | + diff.ts # diff two env sources |
| 35 | + check.ts # validate against a baseline |
| 36 | + audit.ts # suspicious value detection |
| 37 | + parsers/ |
| 38 | + dotenv.ts # .env and .env.example |
| 39 | + compose.ts # docker-compose.yml environment blocks |
| 40 | + vercel.ts # vercel.json + .vercel/output |
| 41 | + railway.ts # railway.toml |
| 42 | + core/ |
| 43 | + differ.ts # diff logic, set operations on variable maps |
| 44 | + auditor.ts # suspicious value rules |
| 45 | + reporter.ts # format output (text | json | markdown) |
| 46 | + types.ts # shared types |
| 47 | + __tests__/ # unit tests |
| 48 | + fixtures/ # test data |
| 49 | +``` |
| 50 | + |
| 51 | +## Code Style |
| 52 | + |
| 53 | +- **TypeScript strict mode** - All code must pass strict type checking |
| 54 | +- **Functional style** - Prefer pure functions over classes |
| 55 | +- **Named exports only** - No default exports |
| 56 | +- **Async/await** - Use async functions for all I/O operations |
| 57 | +- **No `any` type** - Use `unknown` and narrow explicitly |
| 58 | +- **ESLint + Prettier** - Code must pass linting and formatting checks |
| 59 | + |
| 60 | +Run formatting and linting: |
| 61 | +```bash |
| 62 | +npm run format |
| 63 | +npm run lint |
| 64 | +``` |
| 65 | + |
| 66 | +## Adding New Features |
| 67 | + |
| 68 | +### Adding a New Parser |
| 69 | + |
| 70 | +To add support for a new configuration format: |
| 71 | + |
| 72 | +1. Create a new parser in `src/parsers/yourformat.ts` |
| 73 | +2. Implement the parser function that returns `Promise<EnvMap>` |
| 74 | +3. Add test fixtures in `src/__tests__/fixtures/` |
| 75 | +4. Write unit tests in `src/__tests__/yourformat.test.ts` |
| 76 | +5. Update the `loadSource` function in `src/commands/diff.ts` |
| 77 | +6. Update README.md and EXAMPLES.md with usage examples |
| 78 | + |
| 79 | +Example parser structure: |
| 80 | +```typescript |
| 81 | +import { readFile } from 'fs/promises' |
| 82 | +import type { EnvMap } from '../types.js' |
| 83 | +import { ParseError } from '../types.js' |
| 84 | + |
| 85 | +export async function parseYourFormat(filePath: string): Promise<EnvMap> { |
| 86 | + try { |
| 87 | + const content = await readFile(filePath, 'utf-8') |
| 88 | + // Parse content and return EnvMap |
| 89 | + return {} |
| 90 | + } catch (error) { |
| 91 | + if ((error as NodeJS.ErrnoException).code === 'ENOENT') { |
| 92 | + return {} |
| 93 | + } |
| 94 | + throw new ParseError(`Failed to parse: ${filePath}`) |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Adding a New Audit Rule |
| 100 | + |
| 101 | +To add a new security audit rule: |
| 102 | + |
| 103 | +1. Add the rule to the `rules` array in `src/core/auditor.ts` |
| 104 | +2. Write tests in `src/__tests__/auditor.test.ts` (positive and negative cases) |
| 105 | +3. Update README.md with the new rule description |
| 106 | + |
| 107 | +Example rule: |
| 108 | +```typescript |
| 109 | +{ |
| 110 | + id: 'YOUR_RULE_ID', |
| 111 | + severity: 'warn' | 'error', |
| 112 | + match: (key: string, value: string) => { |
| 113 | + // Return true if the rule should trigger |
| 114 | + return false |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +## Testing |
| 120 | + |
| 121 | +### Running Tests |
| 122 | + |
| 123 | +```bash |
| 124 | +# Run all tests |
| 125 | +npm test |
| 126 | + |
| 127 | +# Run tests once (no watch mode) |
| 128 | +npm run test:run |
| 129 | + |
| 130 | +# Run with coverage |
| 131 | +npm run test:coverage |
| 132 | +``` |
| 133 | + |
| 134 | +### Writing Tests |
| 135 | + |
| 136 | +- Place tests in `src/__tests__/` |
| 137 | +- Use descriptive test names |
| 138 | +- Test both positive and negative cases |
| 139 | +- Use fixtures in `src/__tests__/fixtures/` for test data |
| 140 | +- Avoid mocking the filesystem - use real files in temp directories |
| 141 | + |
| 142 | +Example test: |
| 143 | +```typescript |
| 144 | +import { describe, it, expect } from 'vitest' |
| 145 | +import { yourFunction } from '../your-module.js' |
| 146 | + |
| 147 | +describe('yourFunction', () => { |
| 148 | + it('should handle valid input', () => { |
| 149 | + const result = yourFunction('input') |
| 150 | + expect(result).toBe('expected') |
| 151 | + }) |
| 152 | + |
| 153 | + it('should throw on invalid input', () => { |
| 154 | + expect(() => yourFunction('')).toThrow() |
| 155 | + }) |
| 156 | +}) |
| 157 | +``` |
| 158 | + |
| 159 | +## Pull Request Process |
| 160 | + |
| 161 | +1. **Create a feature branch** |
| 162 | + ```bash |
| 163 | + git checkout -b feature/your-feature-name |
| 164 | + ``` |
| 165 | + |
| 166 | +2. **Make your changes** |
| 167 | + - Write clean, well-documented code |
| 168 | + - Add tests for new functionality |
| 169 | + - Update documentation as needed |
| 170 | + |
| 171 | +3. **Test your changes** |
| 172 | + ```bash |
| 173 | + npm run lint |
| 174 | + npm run format:check |
| 175 | + npm run test:run |
| 176 | + npm run build |
| 177 | + ``` |
| 178 | + |
| 179 | +4. **Commit your changes** |
| 180 | + ```bash |
| 181 | + git add . |
| 182 | + git commit -m "feat: add your feature description" |
| 183 | + ``` |
| 184 | + |
| 185 | +5. **Push to your fork** |
| 186 | + ```bash |
| 187 | + git push origin feature/your-feature-name |
| 188 | + ``` |
| 189 | + |
| 190 | +6. **Create a Pull Request** |
| 191 | + - Provide a clear description of the changes |
| 192 | + - Link to any related issues |
| 193 | + - Ensure all CI checks pass |
| 194 | + |
| 195 | +## Commit Message Convention |
| 196 | + |
| 197 | +Follow conventional commits: |
| 198 | + |
| 199 | +- `feat:` - New feature |
| 200 | +- `fix:` - Bug fix |
| 201 | +- `docs:` - Documentation changes |
| 202 | +- `test:` - Adding or updating tests |
| 203 | +- `refactor:` - Code refactoring |
| 204 | +- `perf:` - Performance improvements |
| 205 | +- `chore:` - Maintenance tasks |
| 206 | + |
| 207 | +Examples: |
| 208 | +``` |
| 209 | +feat: add support for Kubernetes ConfigMaps |
| 210 | +fix: handle empty environment files correctly |
| 211 | +docs: update installation instructions |
| 212 | +test: add tests for audit rules |
| 213 | +``` |
| 214 | + |
| 215 | +## Questions or Issues? |
| 216 | + |
| 217 | +- Open an issue for bugs or feature requests |
| 218 | +- Start a discussion for questions or ideas |
| 219 | +- Check existing issues before creating new ones |
| 220 | + |
| 221 | +## License |
| 222 | + |
| 223 | +By contributing, you agree that your contributions will be licensed under the MIT License. |
0 commit comments