Repository: apple/pkl
File: pkl-cli/src/main/kotlin/org/pkl/cli/CliFormatterCommand.kt (Kotlin)

Code before:
    consoleWriter.flush()
  }

  private fun allPaths(): Stream<Path> {
    return paths
      .distinct()
      .stream()
      .flatMap { path ->
        if (path.isDirectory()) {
          Files.walk(path).filter { it.extension == "pkl" || it.name == "PklProject" }
        } else {
          Stream.of(path)
        }
      }
      .map { it.normalize().toAbsolutePath() }
  }

  override fun doRun() {
    val status = Status(SUCCESS)

    // Handle stdin input when path is "-"
    if (paths.size == 1 && paths[0].toString() == "-") {
      handleStdin(status)
    } else {
      handleFilePaths(status)
    }

    when (status.status) {
      FORMATTING_VIOLATION -> {
        if (!silent) {
          writeErr("Formatting violations were found.")
        }
        // using CliTestException instead of CliException because we want full control on how to
        // print errors
        throw CliTestException("", status.status)
      }
      ERROR -> {
        if (!silent) {
          writeErr("An error occurred during formatting.")
        }
        throw CliTestException("", status.status)
      }
    }
  }

  private fun handleStdin(status: Status) {
    try {
      val contents = System.`in`.bufferedReader().use { it.readText() }
      val formatted = format(contents)

      if (contents != formatted) {

Review comment (suggestion):
This code reads: "`-` only means stdin if it is the only argument".

I think we should be able to handle multiple paths that include stdin just like we do with `pkl eval`.
We can change `allPaths(): Stream<Path>` to `allSources(): Stream<ModuleSource>`, and do:

```kt
  private fun allSources(): Stream<ModuleSource> {
    return paths
      .distinct()
      .stream()
      .flatMap { path ->
        when {
          path.toString() == "-" -> Stream.of(ModuleSource.text(IoUtils.readString(System.`in`)))
          path.isDirectory() ->
            Files.walk(path).filter { it.extension == "pkl" || it.name == "PklProject" }
              .map(ModuleSource::path)
          else -> Stream.of(ModuleSource.path(path))
        }
      }
  }
```

Then, we'd just throw if asked to write to URI `repl:text`.

Code after:
    if (silent) return
    consoleWriter.write(message)
    consoleWriter.appendLine()
    consoleWriter.flush()
  }

  private fun allSources(): Stream<ModuleSource> {
    return paths.distinct().stream().flatMap { path ->
      when {
        path.toString() == "-" -> Stream.of(ModuleSource.text(IoUtils.readString(System.`in`)))
        path.isDirectory() ->
          Files.walk(path)
            .filter { it.extension == "pkl" || it.name == "PklProject" }
            .map(ModuleSource::path)
        else -> Stream.of(ModuleSource.path(path))
      }
    }
  }

  override fun doRun() {
    val status = Status(SUCCESS)

    handleSources(status)

    when (status.status) {
      FORMATTING_VIOLATION -> {
        // using CliTestException instead of CliException because we want full control on how to
        // print errors
        throw CliTestException("", status.status)
      }
      ERROR -> {
        if (!silent) {
          writeErr("An error occurred during formatting.")
        }
        throw CliTestException("", status.status)
      }
    }
  }

  private fun handleSources(status: Status) {
    for (source in allSources()) {
      val path = if (source.uri == VmUtils.REPL_TEXT_URI) Path.of("-") else Path.of(source.uri)
      try {
        val contents =
          if (source.contents != null) {
            if (overwrite) {
              writeErr("Cannot write to stdin.")
              throw CliTestException("", ERROR)
            }
            source.contents!!
          } else {