|
1 |
| -import { spawn } from "child_process" |
2 |
| - |
3 |
| -type RunCodeServerCommandOptions = { |
4 |
| - stderr?: "log" |
5 |
| - stdout?: "log" |
6 |
| - ignoreFail?: boolean |
7 |
| -} |
8 |
| - |
9 |
| -interface ErrorWithMoreInfo extends Error { |
10 |
| - stderr: string |
11 |
| - stdout: string |
12 |
| -} |
13 |
| - |
14 |
| -type CSCmd = |
15 |
| - | { |
16 |
| - code: number | null |
17 |
| - signal: NodeJS.Signals | null |
18 |
| - stdout: string |
19 |
| - stderr: string |
20 |
| - } |
21 |
| - | ErrorWithMoreInfo |
| 1 | +import { exec } from "child_process" |
| 2 | +import { promisify } from "util" |
22 | 3 |
|
23 | 4 | /**
|
24 | 5 | *
|
25 | 6 | * A helper function for integration tests to run code-server commands.
|
26 | 7 | */
|
27 |
| -export async function runCodeServerCommand(argv: string[], options: RunCodeServerCommandOptions = {}): Promise<CSCmd> { |
| 8 | +export async function runCodeServerCommand(argv: string[]): Promise<{ stdout: string; stderr: string }> { |
28 | 9 | const CODE_SERVER_COMMAND = process.env.CODE_SERVER_PATH || "/var/tmp/coder/code-server/bin/code-server"
|
29 |
| - // Credit: https://github.com/vercel/next.js/blob/canary/test/lib/next-test-utils.js#L139 |
30 |
| - return new Promise((resolve, reject) => { |
31 |
| - console.log(`Running command "${CODE_SERVER_COMMAND} ${argv.join(" ")}"`) |
32 |
| - const instance = spawn(CODE_SERVER_COMMAND, [...argv]) |
33 |
| - let mergedStdio = "" |
34 |
| - |
35 |
| - let stderrOutput = "" |
36 |
| - if (options.stderr) { |
37 |
| - instance.stderr.on("data", function (chunk) { |
38 |
| - mergedStdio += chunk |
39 |
| - stderrOutput += chunk |
40 |
| - |
41 |
| - if (options.stderr === "log") { |
42 |
| - console.log(chunk.toString()) |
43 |
| - } |
44 |
| - }) |
45 |
| - } else { |
46 |
| - instance.stderr.on("data", function (chunk) { |
47 |
| - mergedStdio += chunk |
48 |
| - }) |
49 |
| - } |
50 |
| - |
51 |
| - let stdoutOutput = "" |
52 |
| - if (options.stdout) { |
53 |
| - instance.stdout.on("data", function (chunk) { |
54 |
| - mergedStdio += chunk |
55 |
| - stdoutOutput += chunk |
56 |
| - |
57 |
| - if (options.stdout === "log") { |
58 |
| - console.log(chunk.toString()) |
59 |
| - } |
60 |
| - }) |
61 |
| - } else { |
62 |
| - instance.stdout.on("data", function (chunk) { |
63 |
| - mergedStdio += chunk |
64 |
| - }) |
65 |
| - } |
66 |
| - |
67 |
| - instance.on("close", (code, signal) => { |
68 |
| - if (!options.stderr && !options.stdout && !options.ignoreFail && code !== 0) { |
69 |
| - return reject(new Error(`command failed with code ${code}\n${mergedStdio}`)) |
70 |
| - } |
71 |
| - |
72 |
| - resolve({ |
73 |
| - code, |
74 |
| - signal, |
75 |
| - stdout: stdoutOutput, |
76 |
| - stderr: stderrOutput, |
77 |
| - }) |
78 |
| - }) |
| 10 | + const { stdout, stderr } = await promisify(exec)(`${CODE_SERVER_COMMAND} ${argv.join(" ")}`) |
79 | 11 |
|
80 |
| - instance.on("error", (err: ErrorWithMoreInfo) => { |
81 |
| - err.stdout = stdoutOutput |
82 |
| - err.stderr = stderrOutput |
83 |
| - reject(err) |
84 |
| - }) |
85 |
| - }) |
| 12 | + return { stdout, stderr } |
86 | 13 | }
|
0 commit comments