ts
import { execFile } from "node:child_process";
import { promisify } from "node:util";
const run = promisify(execFile);
interface SshTarget {
keyPath: string;
user: string;
host: string;
}
export async function dokku(t: SshTarget, args: string[]): Promise<string> {
const { stdout } = await run(
"ssh",
[
"-i",
t.keyPath,
"-o",
"StrictHostKeyChecking=accept-new",
`${t.user}@${t.host}`,
"dokku",
...args,
],
{ maxBuffer: 5 * 1024 * 1024, timeout: 60_000 },
);
return stdout;
}
// Want structured data out of a REPL? Make the REPL emit JSON.
export async function mongoJson(uri: string, expr: string): Promise<unknown> {
const { stdout } = await run(
"mongosh",
[uri, "--quiet", "--eval", `JSON.stringify(${expr})`],
{ maxBuffer: 5 * 1024 * 1024, timeout: 60_000 },
);
return JSON.parse(stdout);
}