72 lines
1.6 KiB
TypeScript
72 lines
1.6 KiB
TypeScript
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import { buildProgram } from "../src/cli.js";
|
|
|
|
type RunCliOptions = {
|
|
args: string[];
|
|
cwd?: string;
|
|
env?: NodeJS.ProcessEnv;
|
|
fetchImpl?: typeof fetch;
|
|
};
|
|
|
|
class MemoryWriter {
|
|
private readonly chunks: string[] = [];
|
|
|
|
write(chunk: string | Uint8Array) {
|
|
this.chunks.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8"));
|
|
return true;
|
|
}
|
|
|
|
toString() {
|
|
return this.chunks.join("");
|
|
}
|
|
}
|
|
|
|
export async function runCli(options: RunCliOptions) {
|
|
const stdout = new MemoryWriter();
|
|
const stderr = new MemoryWriter();
|
|
const program = buildProgram({
|
|
cwd: options.cwd,
|
|
env: options.env,
|
|
fetchImpl: options.fetchImpl,
|
|
stdout,
|
|
stderr,
|
|
});
|
|
|
|
await program.parseAsync(options.args, { from: "user" });
|
|
|
|
return {
|
|
stdout: stdout.toString(),
|
|
stderr: stderr.toString(),
|
|
};
|
|
}
|
|
|
|
export function createTempWorkspace() {
|
|
const cwd = mkdtempSync(path.join(tmpdir(), "atlassian-skill-"));
|
|
|
|
return {
|
|
cwd,
|
|
cleanup() {
|
|
rmSync(cwd, { recursive: true, force: true });
|
|
},
|
|
write(relativePath: string, contents: string) {
|
|
const target = path.join(cwd, relativePath);
|
|
writeFileSync(target, contents, "utf8");
|
|
return target;
|
|
},
|
|
};
|
|
}
|
|
|
|
export function jsonResponse(payload: unknown, init?: ResponseInit) {
|
|
return new Response(JSON.stringify(payload), {
|
|
status: init?.status ?? 200,
|
|
statusText: init?.statusText,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
...(init?.headers ?? {}),
|
|
},
|
|
});
|
|
}
|