Skip to content

Commit

Permalink
Removing the nio as it doesnt support scalaJS yet
Browse files Browse the repository at this point in the history
As of 19th April zio-nio doesnt support scalaJS so using java to read file and zio core for resource management
  • Loading branch information
vivasvan1 committed Apr 19, 2024
1 parent f8cba2a commit 9fbc76d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
3 changes: 1 addition & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ lazy val zioCli = crossProject(JSPlatform, JVMPlatform, NativePlatform)
"dev.zio" %%% "zio-json" % "0.6.2",
"dev.zio" %%% "zio-streams" % zioVersion,
"dev.zio" %%% "zio-test" % zioVersion % Test,
"dev.zio" %%% "zio-test-sbt" % zioVersion % Test,
"dev.zio" %% "zio-nio" % "2.0.0"
"dev.zio" %%% "zio-test-sbt" % zioVersion % Test
)
)
.jvmSettings(
Expand Down
34 changes: 23 additions & 11 deletions zio-cli/shared/src/main/scala/zio/cli/CliApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import zio.cli.completion.{Completion, CompletionScript}
import zio.cli.figlet.FigFont

import scala.annotation.tailrec
import zio.nio.file.{Files, Path}
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import scala.io.Source
import java.nio.file.Paths

/**
* A `CliApp[R, E]` is a complete description of a command-line application, which requires environment `R`, and may
Expand Down Expand Up @@ -86,7 +89,7 @@ object CliApp {
// Use ZIO to filter the paths
ZIO
.foreach(pathsToCheck) { path =>
Files.exists(Path(path, filename))
ZIO.succeed(Files.exists(Path.of(path, filename)))
}
.map(_.zip(pathsToCheck).collect { case (exists, path) if exists => path })
}
Expand All @@ -111,15 +114,24 @@ object CliApp {
}

def loadOptionsFromFile(topLevelCommand: String): ZIO[Any, IOException, List[String]] =
checkAndGetOptionsFilePaths(topLevelCommand).flatMap { filePaths =>
ZIO.foreach(filePaths) { filePath =>
readFileAsString(Path(filePath, s".$topLevelCommand"))
}
}.map(_.flatten).refineToOrDie[IOException]

def readFileAsString(path: zio.nio.file.Path): Task[List[String]] =
Files
.readAllLines(path)
for {
filePaths <- self
.checkAndGetOptionsFilePaths(topLevelCommand)
.refineToOrDie[IOException]
lines <- ZIO
.foreach(filePaths) { filePath =>
ZIO.acquireReleaseWith(
ZIO.attempt(
Source.fromFile(Paths.get(filePath, "." + topLevelCommand).toFile)
)
)(source => ZIO.attempt(source.close()).orDie
) { source =>
ZIO.attempt(source.getLines().toList.filter(_.trim.nonEmpty))
}
}
.map(_.flatten)
.refineToOrDie[IOException]
} yield lines

def run(args: List[String]): ZIO[R, CliError[E], Option[A]] = {
def executeBuiltIn(builtInOption: BuiltInOption): ZIO[R, CliError[E], Option[A]] =
Expand Down

0 comments on commit 9fbc76d

Please sign in to comment.