refactor: dedupe shared normalizer readers

This commit is contained in:
Peter Steinberger
2026-04-07 08:25:45 +01:00
parent 2197ce62bd
commit 02c08b3929
7 changed files with 32 additions and 37 deletions

View File

@@ -7,6 +7,7 @@ import type {
PluginHookReplyDispatchEvent,
PluginHookReplyDispatchResult,
} from "../plugins/types.js";
import { normalizeOptionalString } from "../shared/string-coerce.js";
export { getAcpSessionManager };
export { AcpRuntimeError, isAcpRuntimeError } from "../acp/runtime/errors.js";
@@ -42,17 +43,12 @@ function loadDispatchAcpRuntime() {
}
function hasExplicitCommandCandidate(ctx: PluginHookReplyDispatchEvent["ctx"]): boolean {
const commandBody = ctx.CommandBody;
if (typeof commandBody === "string" && commandBody.trim().length > 0) {
const commandBody = normalizeOptionalString(ctx.CommandBody);
if (commandBody) {
return true;
}
const bodyForCommands = ctx.BodyForCommands;
if (typeof bodyForCommands !== "string") {
return false;
}
const normalized = bodyForCommands.trim();
const normalized = normalizeOptionalString(ctx.BodyForCommands);
if (!normalized) {
return false;
}

View File

@@ -1,5 +1,6 @@
import { readFileSync, statSync } from "node:fs";
import path from "node:path";
import { normalizeOptionalString } from "../shared/string-coerce.js";
export type WindowsSpawnResolution =
| "direct"
@@ -129,7 +130,7 @@ function resolveBinEntry(
binField: string | Record<string, string> | undefined,
): string | null {
if (typeof binField === "string") {
const trimmed = binField.trim();
const trimmed = normalizeOptionalString(binField);
return trimmed || null;
}
if (!binField || typeof binField !== "object") {
@@ -138,14 +139,17 @@ function resolveBinEntry(
if (packageName) {
const preferred = binField[packageName];
if (typeof preferred === "string" && preferred.trim()) {
return preferred.trim();
const normalizedPreferred =
typeof preferred === "string" ? normalizeOptionalString(preferred) : undefined;
if (normalizedPreferred) {
return normalizedPreferred;
}
}
for (const value of Object.values(binField)) {
if (typeof value === "string" && value.trim()) {
return value.trim();
const normalizedValue = typeof value === "string" ? normalizeOptionalString(value) : undefined;
if (normalizedValue) {
return normalizedValue;
}
}
return null;