|
1 | | -import { CP } from "@coder/protocol"; |
| 1 | +import * as cp from "child_process"; |
| 2 | +import { Client, useBuffer } from "@coder/protocol"; |
2 | 3 | import { client } from "./client"; |
3 | 4 | import { promisify } from "./util"; |
4 | 5 |
|
5 | | -const cp = new CP(client); |
| 6 | +class CP { |
| 7 | + public constructor( |
| 8 | + private readonly client: Client, |
| 9 | + ) { } |
| 10 | + |
| 11 | + public exec = ( |
| 12 | + command: string, |
| 13 | + options?: { encoding?: BufferEncoding | string | "buffer" | null } & cp.ExecOptions | null | ((error: Error | null, stdout: string, stderr: string) => void) | ((error: Error | null, stdout: Buffer, stderr: Buffer) => void), |
| 14 | + callback?: ((error: Error | null, stdout: string, stderr: string) => void) | ((error: Error | null, stdout: Buffer, stderr: Buffer) => void), |
| 15 | + ): cp.ChildProcess => { |
| 16 | + // TODO: Probably should add an `exec` instead of using `spawn`, especially |
| 17 | + // since bash might not be available. |
| 18 | + const childProcess = this.client.spawn("bash", ["-c", command.replace(/"/g, "\\\"")]); |
| 19 | + |
| 20 | + let stdout = ""; |
| 21 | + childProcess.stdout.on("data", (data) => { |
| 22 | + stdout += data.toString(); |
| 23 | + }); |
| 24 | + |
| 25 | + let stderr = ""; |
| 26 | + childProcess.stderr.on("data", (data) => { |
| 27 | + stderr += data.toString(); |
| 28 | + }); |
| 29 | + |
| 30 | + childProcess.on("exit", (exitCode) => { |
| 31 | + const error = exitCode !== 0 ? new Error(stderr) : null; |
| 32 | + if (typeof options === "function") { |
| 33 | + callback = options; |
| 34 | + } |
| 35 | + if (callback) { |
| 36 | + // @ts-ignore not sure how to make this work. |
| 37 | + callback( |
| 38 | + error, |
| 39 | + useBuffer(options) ? Buffer.from(stdout) : stdout, |
| 40 | + useBuffer(options) ? Buffer.from(stderr) : stderr, |
| 41 | + ); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // @ts-ignore TODO: not fully implemented |
| 46 | + return childProcess; |
| 47 | + } |
| 48 | + |
| 49 | + public fork = (modulePath: string, args?: string[] | cp.ForkOptions, options?: cp.ForkOptions): cp.ChildProcess => { |
| 50 | + if (options && options.env && options.env.AMD_ENTRYPOINT) { |
| 51 | + // @ts-ignore TODO: not fully implemented |
| 52 | + return this.client.bootstrapFork( |
| 53 | + options.env.AMD_ENTRYPOINT, |
| 54 | + Array.isArray(args) ? args : [], |
| 55 | + // @ts-ignore TODO: env is a different type |
| 56 | + Array.isArray(args) || !args ? options : args, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + // @ts-ignore TODO: not fully implemented |
| 61 | + return this.client.fork( |
| 62 | + modulePath, |
| 63 | + Array.isArray(args) ? args : [], |
| 64 | + // @ts-ignore TODO: env is a different type |
| 65 | + Array.isArray(args) || !args ? options : args, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public spawn = (command: string, args?: string[] | cp.SpawnOptions, options?: cp.SpawnOptions): cp.ChildProcess => { |
| 70 | + // @ts-ignore TODO: not fully implemented |
| 71 | + return this.client.spawn( |
| 72 | + command, |
| 73 | + Array.isArray(args) ? args : [], |
| 74 | + // @ts-ignore TODO: env is a different type |
| 75 | + Array.isArray(args) || !args ? options : args, |
| 76 | + ); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +const fillCp = new CP(client); |
6 | 81 |
|
7 | 82 | // tslint:disable-next-line no-any makes util.promisify return an object |
8 | | -(cp as any).exec[promisify.customPromisifyArgs] = ["stdout", "stderr"]; |
| 83 | +(fillCp as any).exec[promisify.customPromisifyArgs] = ["stdout", "stderr"]; |
9 | 84 |
|
10 | | -export = cp; |
| 85 | +export = fillCp; |
0 commit comments