|
| 1 | +# Configuration Migration Guide |
| 2 | + |
| 3 | +This guide explains how to migrate packages in the socket-cli monorepo to use the shared configuration architecture. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The monorepo now has shared configuration files in `.config/` at the root level. Packages should extend these base configurations instead of duplicating settings. |
| 8 | + |
| 9 | +## Shared Configuration Files |
| 10 | + |
| 11 | +### Location: `.config/` |
| 12 | + |
| 13 | +- `tsconfig.base.json` - Base TypeScript settings |
| 14 | +- `tsconfig.build.json` - For build outputs with declarations |
| 15 | +- `tsconfig.test.json` - For test files |
| 16 | +- `vitest.config.base.mts` - Base Vitest test configuration |
| 17 | +- `eslint.config.mjs` - ESLint flat config (monorepo-wide) |
| 18 | +- `esbuild-inject-import-meta.mjs` - Import.meta polyfill for esbuild |
| 19 | + |
| 20 | +## Migration Steps by Configuration Type |
| 21 | + |
| 22 | +### TypeScript Configuration |
| 23 | + |
| 24 | +#### Before (Duplicated Config) |
| 25 | + |
| 26 | +```json |
| 27 | +{ |
| 28 | + "compilerOptions": { |
| 29 | + "target": "ES2024", |
| 30 | + "module": "nodenext", |
| 31 | + "strict": true, |
| 32 | + "exactOptionalPropertyTypes": true, |
| 33 | + "noUncheckedIndexedAccess": true, |
| 34 | + // ... 30+ more options |
| 35 | + }, |
| 36 | + "include": ["src/**/*.mts"], |
| 37 | + "exclude": ["src/**/*.test.mts", "dist/**"] |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +#### After (Extended Config) |
| 42 | + |
| 43 | +```json |
| 44 | +{ |
| 45 | + "extends": "../../.config/tsconfig.base.json", |
| 46 | + "include": ["src/**/*.mts"], |
| 47 | + "exclude": ["src/**/*.test.mts", "dist/**"] |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +#### With Custom Paths |
| 52 | + |
| 53 | +For packages that need path mappings (like CLI with local dependencies): |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "extends": "../../.config/tsconfig.base.json", |
| 58 | + "compilerOptions": { |
| 59 | + "paths": { |
| 60 | + "@socketsecurity/lib": ["../../socket-lib/dist/index.d.ts"], |
| 61 | + "@socketsecurity/registry": ["../../socket-registry/registry/dist/index.d.ts"] |
| 62 | + } |
| 63 | + }, |
| 64 | + "include": ["src/**/*.mts"], |
| 65 | + "exclude": ["src/**/*.test.mts", "dist/**"] |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Vitest Configuration |
| 70 | + |
| 71 | +#### Before (Duplicated Config) |
| 72 | + |
| 73 | +```typescript |
| 74 | +import { defineConfig } from 'vitest/config' |
| 75 | + |
| 76 | +export default defineConfig({ |
| 77 | + test: { |
| 78 | + globals: false, |
| 79 | + environment: 'node', |
| 80 | + include: ['test/**/*.test.{mts,ts}'], |
| 81 | + exclude: [ |
| 82 | + '**/node_modules/**', |
| 83 | + '**/dist/**', |
| 84 | + // ... many more patterns |
| 85 | + ], |
| 86 | + pool: 'threads', |
| 87 | + poolOptions: { |
| 88 | + threads: { |
| 89 | + singleThread: false, |
| 90 | + maxThreads: 16, |
| 91 | + minThreads: 4, |
| 92 | + isolate: false, |
| 93 | + useAtomics: true, |
| 94 | + }, |
| 95 | + }, |
| 96 | + testTimeout: 30_000, |
| 97 | + hookTimeout: 30_000, |
| 98 | + coverage: { |
| 99 | + provider: 'v8', |
| 100 | + reporter: ['text', 'json', 'html', 'lcov', 'clover'], |
| 101 | + // ... many more options |
| 102 | + }, |
| 103 | + }, |
| 104 | +}) |
| 105 | +``` |
| 106 | + |
| 107 | +#### After (Merged Config) |
| 108 | + |
| 109 | +```typescript |
| 110 | +import { defineConfig, mergeConfig } from 'vitest/config' |
| 111 | +import baseConfig from '../../.config/vitest.config.base.mts' |
| 112 | + |
| 113 | +export default mergeConfig( |
| 114 | + baseConfig, |
| 115 | + defineConfig({ |
| 116 | + test: { |
| 117 | + include: [ |
| 118 | + 'test/**/*.test.{mts,ts}', |
| 119 | + 'src/**/*.test.{mts,ts}', |
| 120 | + ], |
| 121 | + setupFiles: ['./test/setup.mts'], |
| 122 | + }, |
| 123 | + }) |
| 124 | +) |
| 125 | +``` |
| 126 | + |
| 127 | +#### With Custom Settings |
| 128 | + |
| 129 | +For packages with special needs (e.g., longer timeouts, custom coverage): |
| 130 | + |
| 131 | +```typescript |
| 132 | +import { defineConfig, mergeConfig } from 'vitest/config' |
| 133 | +import baseConfig from '../../.config/vitest.config.base.mts' |
| 134 | + |
| 135 | +export default mergeConfig( |
| 136 | + baseConfig, |
| 137 | + defineConfig({ |
| 138 | + test: { |
| 139 | + testTimeout: 120_000, // 2 minutes for slow tests |
| 140 | + hookTimeout: 30_000, |
| 141 | + coverage: { |
| 142 | + thresholds: { |
| 143 | + lines: 0, |
| 144 | + functions: 0, |
| 145 | + branches: 0, |
| 146 | + statements: 0, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }) |
| 151 | +) |
| 152 | +``` |
| 153 | + |
| 154 | +### ESLint Configuration |
| 155 | + |
| 156 | +#### Root Level Only |
| 157 | + |
| 158 | +The ESLint configuration lives at the root and applies to the entire monorepo. Individual packages **do not** need their own ESLint configs. |
| 159 | + |
| 160 | +#### Package-Specific Rules (Rare) |
| 161 | + |
| 162 | +If a package truly needs custom ESLint rules, extend the root config: |
| 163 | + |
| 164 | +```javascript |
| 165 | +import rootConfig from '../../.config/eslint.config.mjs' |
| 166 | + |
| 167 | +export default [ |
| 168 | + ...rootConfig, |
| 169 | + { |
| 170 | + files: ['src/**/*.mts'], |
| 171 | + rules: { |
| 172 | + // Package-specific overrides |
| 173 | + 'no-console': 'warn', |
| 174 | + }, |
| 175 | + }, |
| 176 | +] |
| 177 | +``` |
| 178 | + |
| 179 | +### esbuild Configuration |
| 180 | + |
| 181 | +#### Before (Relative Paths) |
| 182 | + |
| 183 | +```javascript |
| 184 | +import path from 'node:path' |
| 185 | +import { fileURLToPath } from 'node:url' |
| 186 | + |
| 187 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 188 | + |
| 189 | +export default { |
| 190 | + // ... config |
| 191 | + inject: [path.join(__dirname, 'esbuild-inject-import-meta.mjs')], |
| 192 | +} |
| 193 | +``` |
| 194 | +
|
| 195 | +#### After (Shared Utility) |
| 196 | +
|
| 197 | +```javascript |
| 198 | +import path from 'node:path' |
| 199 | +import { fileURLToPath } from 'node:url' |
| 200 | + |
| 201 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 202 | +const rootPath = path.join(__dirname, '..') |
| 203 | + |
| 204 | +export default { |
| 205 | + // ... config |
| 206 | + inject: [path.join(rootPath, '..', '..', '.config', 'esbuild-inject-import-meta.mjs')], |
| 207 | +} |
| 208 | +``` |
| 209 | +
|
| 210 | +## Package-by-Package Migration Plan |
| 211 | +
|
| 212 | +### Phase 1: Simple Packages (Low Risk) |
| 213 | +
|
| 214 | +Start with packages that have minimal custom configuration: |
| 215 | +
|
| 216 | +1. **packages/cli-with-sentry** |
| 217 | + - Simple vitest config (just timeout overrides) |
| 218 | + - Should extend base config easily |
| 219 | +
|
| 220 | +2. **packages/socketbin-custom-node-from-source** |
| 221 | + - Simple vitest config (just timeout overrides) |
| 222 | + - Should extend base config easily |
| 223 | +
|
| 224 | +3. **packages/socketbin-native-node-sea** |
| 225 | + - Simple vitest config (just timeout overrides) |
| 226 | + - Should extend base config easily |
| 227 | +
|
| 228 | +4. **packages/socket** |
| 229 | + - Simple vitest config with coverage overrides |
| 230 | + - Should extend base config easily |
| 231 | +
|
| 232 | +### Phase 2: Complex Package (Higher Risk) |
| 233 | +
|
| 234 | +5. **packages/cli** |
| 235 | + - Has complex .config/ subdirectory |
| 236 | + - Multiple TypeScript configs (base, check) |
| 237 | + - Custom esbuild configs |
| 238 | + - Should migrate carefully, keeping package-specific build configs |
| 239 | +
|
| 240 | +### Phase 3: Root Configs |
| 241 | +
|
| 242 | +6. **Root vitest configs** |
| 243 | + - `vitest.config.mts` - Already serves as template for base config |
| 244 | + - `vitest.config.isolated.mts` - Keep for isolated test runs |
| 245 | + - `vitest.e2e.config.mts` - Keep for E2E tests |
| 246 | +
|
| 247 | +## Migration Checklist |
| 248 | +
|
| 249 | +For each package: |
| 250 | +
|
| 251 | +- [ ] Read current tsconfig.json and identify custom settings |
| 252 | +- [ ] Create new tsconfig.json that extends `../../.config/tsconfig.base.json` |
| 253 | +- [ ] Move custom compilerOptions (paths, typeRoots, etc.) to new config |
| 254 | +- [ ] Keep include/exclude patterns specific to the package |
| 255 | +- [ ] Test: Run `pnpm tsc` to verify type checking still works |
| 256 | +
|
| 257 | +- [ ] Read current vitest.config.mts and identify custom settings |
| 258 | +- [ ] Create new vitest.config.mts that merges with base config |
| 259 | +- [ ] Move custom test settings (timeouts, includes, setupFiles) to new config |
| 260 | +- [ ] Test: Run `pnpm test` to verify tests still pass |
| 261 | +
|
| 262 | +- [ ] Identify any package-specific eslint configs |
| 263 | +- [ ] If none exist, no action needed (root config applies) |
| 264 | +- [ ] If custom rules exist, evaluate if they should move to root or stay local |
| 265 | +
|
| 266 | +- [ ] Update esbuild configs to use shared import.meta inject helper |
| 267 | +- [ ] Test: Run builds to verify output is correct |
| 268 | +
|
| 269 | +- [ ] Remove duplicate configuration files |
| 270 | +- [ ] Update documentation if package has special config needs |
| 271 | +- [ ] Commit changes with descriptive message |
| 272 | +
|
| 273 | +## Validation |
| 274 | +
|
| 275 | +After migration, verify: |
| 276 | +
|
| 277 | +1. **Type checking**: `pnpm tsc` passes |
| 278 | +2. **Linting**: `pnpm check:lint` passes |
| 279 | +3. **Tests**: `pnpm test` passes |
| 280 | +4. **Build**: `pnpm build` produces expected output |
| 281 | +5. **Coverage**: `pnpm run test:unit:coverage` works |
| 282 | +
|
| 283 | +## Rollback Plan |
| 284 | +
|
| 285 | +If migration causes issues: |
| 286 | +
|
| 287 | +1. Keep git history clean with one package per commit |
| 288 | +2. Revert individual package commits if needed |
| 289 | +3. Document any incompatibilities discovered |
| 290 | +4. Update base configs to accommodate edge cases |
| 291 | +
|
| 292 | +## Benefits After Migration |
| 293 | +
|
| 294 | +1. **Reduced Lines of Code**: 50-100+ lines removed per package |
| 295 | +2. **Single Source of Truth**: Update once, applies everywhere |
| 296 | +3. **Consistency**: All packages use same base settings |
| 297 | +4. **Easier Maintenance**: Less configuration to track |
| 298 | +5. **Better Defaults**: Proven settings with documented rationale |
| 299 | +
|
| 300 | +## Questions or Issues? |
| 301 | +
|
| 302 | +If you encounter problems during migration: |
| 303 | +
|
| 304 | +1. Check `.config/README.md` for usage examples |
| 305 | +2. Compare with already-migrated packages |
| 306 | +3. Verify you're using mergeConfig for Vitest (not defineConfig alone) |
| 307 | +4. Ensure extends paths are correct (../../.config/...) |
| 308 | +5. Document any edge cases for future reference |
0 commit comments