Files
reverse-engineering-anything/src/process/readCompletedCommandAnswer.ts
T

24 lines
701 B
TypeScript

import { OwnedCommandFailure } from "./OwnedCommand.js";
/** Decode a complete stdout answer when the command exited successfully before finalization failed. */
export const readCompletedCommandAnswer = <Answer>(
cause: unknown,
decode: (stdout: string) => Answer,
): Answer | undefined => {
if (
!(cause instanceof OwnedCommandFailure) ||
cause.collectionFailure ||
cause.snapshot?.exitCode !== 0 ||
cause.snapshot.outputIncomplete === true
)
return undefined;
const stdout = cause.snapshot.stdout;
if (stdout.invalidUtf8 || stdout.bytes !== stdout.observedBytes)
return undefined;
try {
return decode(stdout.text);
} catch {
return undefined;
}
};