( cmd: string, args: ReadonlyArray<string>, env?: Record<string, string | undefined>, )
| 106 | } |
| 107 | |
| 108 | const runCommand = ( |
| 109 | cmd: string, |
| 110 | args: ReadonlyArray<string>, |
| 111 | env?: Record<string, string | undefined>, |
| 112 | ): Effect.Effect<CommandResult, Error> => |
| 113 | Effect.callback<CommandResult, Error>((resume) => { |
| 114 | const options = env |
| 115 | ? { encoding: "utf8" as const, env: { ...process.env, ...env } } |
| 116 | : { encoding: "utf8" as const }; |
| 117 | execFile(cmd, [...args], options, (error, stdout, stderr) => { |
| 118 | // A string `code` (ENOENT etc.) means the command could not be spawned. |
| 119 | if (error && typeof (error as { code?: unknown }).code === "string") { |
| 120 | resume( |
| 121 | Effect.fail(new Error(`Failed to run \`${cmd}\`: ${(error as { code: string }).code}`)), |
| 122 | ); |
| 123 | return; |
| 124 | } |
| 125 | const code = |
| 126 | error && typeof (error as { code?: unknown }).code === "number" |
| 127 | ? (error as { code: number }).code |
| 128 | : 0; |
| 129 | resume(Effect.succeed({ stdout: stdout ?? "", stderr: stderr ?? "", code })); |
| 130 | }); |
| 131 | }); |
| 132 | |
| 133 | const currentUid = (): number => { |
| 134 | const getuid = (process as { getuid?: () => number }).getuid; |
no test coverage detected