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