refactor(agents): dedupe plugin hooks and test helpers

This commit is contained in:
Peter Steinberger
2026-02-22 07:38:24 +00:00
parent 75c1bfbae8
commit 185fba1d22
16 changed files with 661 additions and 579 deletions

View File

@@ -17,6 +17,16 @@ export function captureEnv(keys: string[]) {
};
}
function applyEnvValues(env: Record<string, string | undefined>): void {
for (const [key, value] of Object.entries(env)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
}
export function captureFullEnv() {
const snapshot: Record<string, string | undefined> = { ...process.env };
@@ -41,13 +51,7 @@ export function captureFullEnv() {
export function withEnv<T>(env: Record<string, string | undefined>, fn: () => T): T {
const snapshot = captureEnv(Object.keys(env));
try {
for (const [key, value] of Object.entries(env)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
applyEnvValues(env);
return fn();
} finally {
snapshot.restore();
@@ -60,13 +64,7 @@ export async function withEnvAsync<T>(
): Promise<T> {
const snapshot = captureEnv(Object.keys(env));
try {
for (const [key, value] of Object.entries(env)) {
if (value === undefined) {
delete process.env[key];
} else {
process.env[key] = value;
}
}
applyEnvValues(env);
return await fn();
} finally {
snapshot.restore();