function runCliWorker(dir, args, options) {
  const result = {
    status: undefined,
    stdout: "",
    stderr: "",
    write: [],
  };
  const { promise, resolve, reject } = promiseWithResolvers();
  const createEpipeErrorHandler = (event) => (error) => {
    if (!error) {
      return;
    }

    // It can fail with `write EPIPE` error when running node with unsupported flags like `--experimental-strip-types`
    // Let's ignore and wait for the `close` event
    if (
      error.code === "EPIPE" &&
      error.syscall === "write" &&
      nodeOptions.length > 0
    ) {
      if (IS_CI) {
        // eslint-disable-next-line no-console
        console.error(
          Object.assign(error, { event, dir, args, options, worker }),
        );
      }
      return;
    }

    reject(error);
  };

  const nodeOptions = options?.nodeOptions ?? [];
  const env = {
    ...process.env,
    ...options.env,
    NO_COLOR: "1",
  };
  delete env.FORCE_COLOR;

  const worker = childProcess.fork(CLI_WORKER_FILE, args, {
    cwd: dir,
    execArgv: [
      "--trace-deprecation",
      ...(SUPPORTS_DISABLE_WARNING_FLAG
        ? ["--disable-warning=ExperimentalWarning"]
        : []),
      ...nodeOptions,
    ],
    stdio: [options.input ? "pipe" : "ignore", "pipe", "pipe", "ipc"],
    env,
    serialization: "advanced",
  });

  worker.on("message", (message) => {
    if (message.type === "cli:write-file") {
      result.write.push(message.data);
    } else if (message.type === "worker:fault") {
      reject(message.error);
    }
  });

  for (const stream of ["stdout", "stderr"]) {
    worker[stream].on("data", (data) => {
      result[stream] += data.toString();
    });
  }

  const removeStdioFinalNewLine = () => {
    for (const stream of ["stdout", "stderr"]) {
      result[stream] = removeFinalNewLine(result[stream]);
    }
  };

  worker.once("close", (code) => {
    result.status = code;
    removeStdioFinalNewLine();
    resolve(result);
  });

  worker.once("error", (error) => {
    reject(error);
  });

  worker.once("spawn", () => {
    if (options.input) {
      worker.stdin.once("error", createEpipeErrorHandler("worker.stdin.end()"));
      worker.stdin.end(options.input);
    }

    worker.send(options, createEpipeErrorHandler("worker.send()"));
  });

  return promise;
}
@param {string} dir
@param {string[]} args
@param {CliTestOptions} options