MCPcopy Create free account
hub / github.com/cloudflare/computer / exec

Function exec

examples/artifacts/src/index.ts:197–215  ·  view source on GitHub ↗
(
  ws: WorkspaceClient,
  command: string,
  options: { cwd?: string; secretToRedact?: string } = {},
)

Source from the content-addressed store, hash-verified

195}
196
197async function exec(
198 ws: WorkspaceClient,
199 command: string,
200 options: { cwd?: string; secretToRedact?: string } = {},
201): Promise<string> {
202 const handle = await ws.runtime.exec(command, { cwd: options.cwd, encoding: "utf8" });
203 try {
204 const result = await handle.result();
205 if (result.exitCode === 0) return result.stdout;
206
207 const raw = result.stderr || result.stdout || `exit code ${result.exitCode}`;
208 const output = options.secretToRedact
209 ? raw.replaceAll(options.secretToRedact, "<artifact-remote>")
210 : raw;
211 throw new Error(`command failed: ${output}`);
212 } finally {
213 handle[Symbol.dispose]?.();
214 }
215}
216
217function parseJSON<T>(text: string): T {
218 return JSON.parse(text) as T;

Callers 1

handleCreateFunction · 0.70

Calls 2

execMethod · 0.65
resultMethod · 0.65

Tested by

no test coverage detected