Files
AgentHub/server/lib/gh.js
T

37 lines
1.3 KiB
JavaScript

/**
* @file Thin execFile wrapper around the GitHub CLI (`gh`), used by
* server/lib/ci-status.js to read Actions run state. Mirrors worktree.js's
* git() helper's shape (cwd-scoped, throws with structured .gh details on
* failure) but has no env-scrubbing to do — gh doesn't read GIT_* variables.
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
*/
const { execFile } = require("node:child_process");
const { promisify } = require("node:util");
const execFileAsync = promisify(execFile);
/**
* Run `gh <args>` in `cwd`. Resolves `{ stdout, stderr }` on a zero exit.
* On failure, throws an Error with `.gh = { args, code, stderr }` — `.code`
* is `"ENOGH"` when the binary itself is missing, otherwise the process's
* exit code.
*
* @param {string} cwd
* @param {string[]} args
* @returns {Promise<{stdout: string, stderr: string}>}
*/
async function gh(cwd, args) {
try {
const result = await execFileAsync("gh", args, { cwd, maxBuffer: 8 * 1024 * 1024 });
return { stdout: result.stdout, stderr: result.stderr };
} catch (err) {
const error = new Error(`gh ${args[0]} failed`);
error.code = err.code === "ENOENT" ? "ENOGH" : err.code;
error.gh = { args, code: err.code, stderr: err.stderr || "" };
throw error;
}
}
module.exports = { gh };