* Run `fn` with a snapshot of `process.env` taken beforehand, restoring the original environment * afterwards (even if `fn` throws). Linting loads `.env` via `dotenv.config` and injects generated * integration env vars into `process.env`; without this a lint run would permanently mutate the * pro
(fn: () => Promise<T>)
| 53 | * was reported missing would silently start passing) and clobbering caller-provided env values. |
| 54 | */ |
| 55 | async function withRestoredEnv<T>(fn: () => Promise<T>): Promise<T> { |
| 56 | const originalEnv = { ...process.env } |
| 57 | try { |
| 58 | return await fn() |
| 59 | } finally { |
| 60 | // Reset to the snapshot: delete keys added during the run, then restore originals. Assigning |
| 61 | // `process.env = originalEnv` would replace the live env object some libraries hold a reference |
| 62 | // to, so mutate the existing object in place instead. |
| 63 | for (const key of Object.keys(process.env)) { |
| 64 | if (!(key in originalEnv)) { |
| 65 | delete process.env[key] |
| 66 | } |
| 67 | } |
| 68 | Object.assign(process.env, originalEnv) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Derive a stable, snake_case issue code from an error's class name. |
no outgoing calls
no test coverage detected