Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/nu-bang/__tests__/run-nu.ts

Raw
import { createNuBashOperations } from "../index.js";

export interface RunNuResult {
	output: string;
	exitCode: number | undefined;
	cancelled: boolean;
}

/** Run Nu source through the same BashOperations the `user_bash` hook hands to Pi. */
export async function runNu(
	command: string,
	cwd: string,
	signal?: AbortSignal,
): Promise<RunNuResult> {
	let output = "";
	const result = await createNuBashOperations().exec(command, cwd, {
		onData(chunk) {
			output += chunk.toString("utf8");
		},
		...(signal ? { signal } : {}),
	});
	return {
		output,
		exitCode: result.exitCode ?? undefined,
		cancelled: signal?.aborted ?? false,
	};
}