import { chmod, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { ChildProcessWithoutNullStreams, spawn } from "node:child_process";
import { cleanUpTempDir } from "./execute-code";
import { join } from "node:path";
export default async function bash(
command: string,
spawnOptions?,
): Promise<ChildProcessWithoutNullStreams> {
let tempDir = "";
let tempPath = "";
try {
tempDir = await mkdtemp(join(tmpdir(), "cocalc-"));
tempPath = join(tempDir, "a.sh");
await writeFile(tempPath, command);
await chmod(tempPath, 0o700);
} catch (err) {
await cleanUpTempDir(tempDir);
throw err;
}
const child = spawn("bash", [tempPath], spawnOptions);
child.once("exit", async () => {
await cleanUpTempDir(tempDir);
});
return child;
}