mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
24 lines
701 B
TypeScript
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;
|
|
}
|
|
};
|