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

Code before:
import kotlin.math.max
import org.pkl.commons.cli.CliBaseOptions
import org.pkl.commons.cli.CliCommand
import org.pkl.commons.cli.CliTestException
import org.pkl.formatter.Formatter
import org.pkl.formatter.GrammarVersion
import org.pkl.parser.GenericParserError

class CliFormatterCommand
@JvmOverloads
constructor(
  private val paths: List<Path>,
  private val grammarVersion: GrammarVersion,
  private val overwrite: Boolean,
  private val names: Boolean,
  private val silent: Boolean,
  private val consoleWriter: Writer = System.out.writer(),
  private val errWriter: Writer = System.err.writer(),
) : CliCommand(CliBaseOptions()) {

  private fun format(contents: String): String {
    return Formatter().format(contents, grammarVersion)
  }

  private fun writeErr(error: String) {
    if (silent) return
    errWriter.write(error)
    errWriter.appendLine()
    errWriter.flush()
  }

  private fun write(message: String) {
    if (silent) return
    consoleWriter.write(message)
    consoleWriter.appendLine()
    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() }
  }

Review comment (suggestion):
I think we should continue writing to stderr for the error case; it makes sense to me that an unexpected error will still show me error output.

Code after:
import kotlin.io.path.writeText
import kotlin.math.max
import org.pkl.commons.cli.CliBaseOptions
import org.pkl.commons.cli.CliCommand
import org.pkl.commons.cli.CliTestException
import org.pkl.core.ModuleSource
import org.pkl.core.runtime.VmUtils
import org.pkl.core.util.IoUtils
import org.pkl.formatter.Formatter
import org.pkl.formatter.GrammarVersion
import org.pkl.parser.GenericParserError

class CliFormatterCommand
@JvmOverloads
constructor(
  private val paths: List<Path>,
  private val grammarVersion: GrammarVersion,
  private val overwrite: Boolean,
  private val diffNameOnly: Boolean,
  private val silent: Boolean,
  private val consoleWriter: Writer = System.out.writer(),
  private val errWriter: Writer = System.err.writer(),
) : CliCommand(CliBaseOptions()) {

  private fun format(contents: String): String {
    return Formatter().format(contents, grammarVersion)
  }

  private fun writeErr(error: String) {
    errWriter.write(error)
    errWriter.appendLine()
    errWriter.flush()
  }

  private fun write(message: String) {
    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))
      }