141 lines · 3.8 KB · TypeScript
RawDownload
import type { ExecFileOptions } from "node:child_process";import { access } from "node:fs/promises";import { constants } from "node:fs";import { Data, Effect } from "effect";import { getBirdCommand } from "./config";import { runEffectPromise } from "./effect-runtime";import { runSubprocessEffect, SubprocessError } from "./subprocess"; export class BirdCommandUnavailableError extends Data.TaggedError(	"BirdCommandUnavailableError",)<{	readonly message: string;	readonly command: string;	readonly cause?: unknown;}> {} export class BirdCommandExecutionError extends Data.TaggedError(	"BirdCommandExecutionError",)<{	readonly message: string;	readonly stdout?: string;	readonly stderr?: string;	readonly useFallbackMessage?: boolean;	readonly cause?: unknown;}> {} function isPathCommand(command: string) {	return command.includes("/") || command.startsWith(".");} function formatBirdInstallHint(command: string) {	return [		`bird command unavailable: ${command}`,		"Install bird on PATH, set BIRDCLAW_BIRD_COMMAND, or update ~/.birdclaw/config.json mentions.birdCommand.",	].join("\n");} function isUnavailableExecError(error: unknown) {	return (		error &&		typeof error === "object" &&		"code" in error &&		(error.code === "ENOENT" || error.code === "EACCES")	);} function execFailureFromCause(command: string, cause: unknown) {	if (isUnavailableExecError(cause)) {		return new BirdCommandUnavailableError({			message: formatBirdInstallHint(command),			command,			cause,		});	}	if (cause instanceof SubprocessError && !cause.causeWasError) {		return new BirdCommandExecutionError({			message: "",			useFallbackMessage: true,			cause,		});	}	if (cause instanceof Error) {		const output = cause as Error & {			stdout?: unknown;			stderr?: unknown;		};		return new BirdCommandExecutionError({			message: cause.message,			stdout: typeof output.stdout === "string" ? output.stdout : undefined,			stderr: typeof output.stderr === "string" ? output.stderr : undefined,			cause,		});	}	return new BirdCommandExecutionError({		message: "",		useFallbackMessage: true,		cause,	});} function assertBirdCommandAvailableEffect(command: string) {	if (!isPathCommand(command)) {		return Effect.void;	} 	return Effect.tryPromise({		try: () => Promise.resolve(access(command, constants.X_OK)),		catch: (cause) =>			new BirdCommandUnavailableError({				message: formatBirdInstallHint(command),				command,				cause,			}),	}).pipe(Effect.asVoid);} function getBirdCommandEffect() {	return Effect.try({		try: () => getBirdCommand(),		catch: (cause) =>			cause instanceof Error ? cause : new Error(String(cause)),	});} export function runBirdCommandEffect(	args: string[],	options?: ExecFileOptions,): Effect.Effect<	{ stdout: string; stderr: string },	BirdCommandExecutionError | BirdCommandUnavailableError | Error> {	return Effect.gen(function* () {		const birdCommand = yield* getBirdCommandEffect();		yield* assertBirdCommandAvailableEffect(birdCommand); 		return yield* runSubprocessEffect({			command: birdCommand,			args,			...(typeof options?.cwd === "string" ? { cwd: options.cwd } : {}),			...(options?.env ? { env: options.env } : {}),			...(typeof options?.timeout === "number"				? { timeoutMs: options.timeout }				: {}),			...(typeof options?.maxBuffer === "number"				? { maxBufferBytes: options.maxBuffer }				: {}),			...(options?.signal ? { signal: options.signal } : {}),			...(options?.killSignal ? { killSignal: options.killSignal } : {}),		}).pipe(			Effect.map(({ stdout, stderr }) => ({ stdout, stderr })),			Effect.mapError((cause) => execFailureFromCause(birdCommand, cause)),		);	});} export function runBirdCommand(	args: string[],	options?: ExecFileOptions,): Promise<{ stdout: string; stderr: string }> {	return runEffectPromise(runBirdCommandEffect(args, options));}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t