(rootDir: string, patterns: string[])
| 357 | * index in the same process sees the new patterns. |
| 358 | */ |
| 359 | export function addIncludeIgnoredPatterns(rootDir: string, patterns: string[]): number { |
| 360 | const file = path.join(rootDir, PROJECT_CONFIG_FILENAME); |
| 361 | let config: Record<string, unknown> = {}; |
| 362 | let raw: string | null = null; |
| 363 | try { |
| 364 | raw = fs.readFileSync(file, 'utf-8'); |
| 365 | } catch { |
| 366 | raw = null; // missing file — create a fresh one below |
| 367 | } |
| 368 | if (raw !== null) { |
| 369 | let parsed: unknown; |
| 370 | try { |
| 371 | parsed = JSON.parse(raw); |
| 372 | } catch { |
| 373 | throw new Error(`${PROJECT_CONFIG_FILENAME} is not valid JSON — fix it by hand, then re-run.`); |
| 374 | } |
| 375 | if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { |
| 376 | config = parsed as Record<string, unknown>; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | const existing = Array.isArray(config.includeIgnored) |
| 381 | ? (config.includeIgnored as unknown[]).filter((p): p is string => typeof p === 'string') |
| 382 | : []; |
| 383 | const merged = [...existing]; |
| 384 | const seen = new Set(existing); |
| 385 | let added = 0; |
| 386 | for (const p of patterns) { |
| 387 | if (seen.has(p)) continue; |
| 388 | seen.add(p); |
| 389 | merged.push(p); |
| 390 | added++; |
| 391 | } |
| 392 | config.includeIgnored = merged; |
| 393 | fs.writeFileSync(file, JSON.stringify(config, null, 2) + '\n'); |
| 394 | clearProjectConfigCache(); |
| 395 | return added; |
| 396 | } |
no test coverage detected