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

Code before:
          "Cannot substitute output path placeholder `%{moduleDir}` " +
            "because module `$uri` does not have a file system path."
        )
      }
      val absolutePath = workingDir.resolve(substituted).normalize()
      absolutePath
    }
  }

  /** Renders each module's `output.text`, writing it to the specified output file. */
  private fun writeOutput(builder: EvaluatorBuilder) {
    val evaluator = builder.setOutputFormat(options.outputFormat).build()
    evaluator.use {
      val outputFiles = fileOutputPaths
      if (outputFiles != null) {
        // files that we've written non-empty output to
        // YamlRenderer produces empty output if `isStream` is true and `output.value` is empty
        // collection
        val writtenFiles = mutableSetOf<Path>()

        for ((moduleUri, outputFile) in outputFiles) {
          val moduleSource = toModuleSource(moduleUri, consoleReader)
          val output = evaluator.evaluateExpressionString(moduleSource, options.expression)
          if (Files.isDirectory(outputFile)) {
            throw CliException(
              "Ouput file is a directory ('$outputFile'). " +
                "Did you mean '--multiple-file-output-path'?"
            )
          }
          outputFile.createParentDirectories()
          if (!writtenFiles.contains(outputFile)) {
            // write file even if output is empty to overwrite output from previous runs
            outputFile.writeString(output)
            if (output.isNotEmpty()) {
              writtenFiles.add(outputFile)
            }
          } else {
            if (output.isNotEmpty()) {
              outputFile.writeString(
                options.moduleOutputSeparator + '\n',
                Charsets.UTF_8,
                StandardOpenOption.WRITE,
                StandardOpenOption.APPEND,
              )
              outputFile.writeString(
                output,
                Charsets.UTF_8,
                StandardOpenOption.WRITE,
                StandardOpenOption.APPEND,
              )
            }

Review comment (suggestion):
```suggestion
              "Output file `$outputFile` is a directory. " +
```

Code after:
          "Cannot substitute output path placeholder `%{moduleDir}` " +
            "because module `$uri` does not have a file system path."
        )
      }
      val absolutePath = workingDir.resolve(substituted).normalize()
      absolutePath
    }
  }

  /** Renders each module's `output.text`, writing it to the specified output file. */
  private fun writeOutput(builder: EvaluatorBuilder) {
    val evaluator = builder.setOutputFormat(options.outputFormat).build()
    evaluator.use {
      val outputFiles = fileOutputPaths
      if (outputFiles != null) {
        // files that we've written non-empty output to
        // YamlRenderer produces empty output if `isStream` is true and `output.value` is empty
        // collection
        val writtenFiles = mutableSetOf<Path>()

        for ((moduleUri, outputFile) in outputFiles) {
          val moduleSource = toModuleSource(moduleUri, consoleReader)
          val output = evaluator.evaluateExpressionString(moduleSource, options.expression)
          if (Files.isDirectory(outputFile)) {
            throw CliException(
              "Output file `$outputFile` is a directory. " +
                "Did you mean `--multiple-file-output-path`?"
            )
          }
          outputFile.createParentDirectories()
          if (!writtenFiles.contains(outputFile)) {
            // write file even if output is empty to overwrite output from previous runs
            outputFile.writeString(output)
            if (output.isNotEmpty()) {
              writtenFiles.add(outputFile)
            }
          } else {
            if (output.isNotEmpty()) {
              outputFile.writeString(
                options.moduleOutputSeparator + '\n',
                Charsets.UTF_8,
                StandardOpenOption.WRITE,
                StandardOpenOption.APPEND,
              )
              outputFile.writeString(
                output,
                Charsets.UTF_8,
                StandardOpenOption.WRITE,
                StandardOpenOption.APPEND,
              )
            }