| 468 | * index in the same process sees the new patterns. |
| 469 | */ |
| 470 | export function addIncludeIgnoredPatterns(rootDir: string, patterns: string[]): number { |
| 471 | const file = path.join(rootDir, PROJECT_CONFIG_FILENAME); |
| 472 | let config: Record<string, unknown> = {}; |
| 473 | let raw: string | null = null; |
| 474 | try { |
| 475 | raw = fs.readFileSync(file, 'utf-8'); |
| 476 | } catch { |
| 477 | raw = null; // missing file — create a fresh one below |
| 478 | } |
| 479 | if (raw !== null) { |
| 480 | let parsed: unknown; |
| 481 | try { |
| 482 | parsed = JSON.parse(raw); |
| 483 | } catch { |
| 484 | throw new Error(`${PROJECT_CONFIG_FILENAME} is not valid JSON — fix it by hand, then re-run.`); |
| 485 | } |
| 486 | if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { |
| 487 | config = parsed as Record<string, unknown>; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | const existing = Array.isArray(config.includeIgnored) |
| 492 | ? (config.includeIgnored as unknown[]).filter((p): p is string => typeof p === 'string') |
| 493 | : []; |
| 494 | const merged = [...existing]; |
| 495 | const seen = new Set(existing); |
| 496 | let added = 0; |
| 497 | for (const p of patterns) { |
| 498 | if (seen.has(p)) continue; |
| 499 | seen.add(p); |
| 500 | merged.push(p); |
| 501 | added++; |
| 502 | } |
| 503 | config.includeIgnored = merged; |
| 504 | fs.writeFileSync(file, JSON.stringify(config, null, 2) + '\n'); |
| 505 | clearProjectConfigCache(); |
| 506 | return added; |
| 507 | } |