src/node/cli.ts/shouldOpenInExistingInstance
export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Promise<string | undefined> => {
  // Always use the existing instance if we're running from VS Code's terminal.
  if (process.env.VSCODE_IPC_HOOK_CLI) {
    logger.debug("Found VSCODE_IPC_HOOK_CLI")
    return process.env.VSCODE_IPC_HOOK_CLI
  }

  // If these flags are set then assume the user is trying to open in an
  // existing instance since these flags have no effect otherwise.
  const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
    return args[cur as keyof UserProvidedArgs] ? prev + 1 : prev
  }, 0)
  if (openInFlagCount > 0) {
    logger.debug("Found --reuse-window or --new-window")
    return readSocketPath(DEFAULT_SOCKET_PATH)
  }

  // It's possible the user is trying to spawn another instance of code-server.
  // 1. Check if any unrelated flags are set (this should only run when
  //    code-server is invoked exactly like this: `code-server my-file`).
  // 2. That a file or directory was passed.
  // 3. That the socket is active.
  if (Object.keys(args).length === 1 && typeof args._ !== "undefined" && args._.length > 0) {
    const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH)
    if (socketPath && (await canConnect(socketPath))) {
      logger.debug("Found existing code-server socket")
      return socketPath
    }
  }

  return undefined
}