Skip to content

Commit

Permalink
Merge pull request #2789 from armanbilge/feature/cwd-and-home
Browse files Browse the repository at this point in the history
Add `userHome`, `currentWorkingDirectory` to `Files` API
  • Loading branch information
mpilquist authored Jan 13, 2022
2 parents aee10a7 + 1485b61 commit 8b285a6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions io/js/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import fs2.internal.jsdeps.node.fsMod
import fs2.internal.jsdeps.node.fsPromisesMod
import fs2.internal.jsdeps.node.nodeStrings
import fs2.internal.jsdeps.node.osMod
import fs2.internal.jsdeps.node.processMod
import fs2.io.file.Files.UnsealedFiles

import scala.concurrent.duration._
Expand Down Expand Up @@ -133,6 +134,12 @@ private[fs2] trait FilesCompanionPlatform {
F.fromPromise(F.delay(fsPromisesMod.rm(path.toString)))
).adaptError { case IOException(ex) => ex }

def currentWorkingDirectory: F[Path] =
F.delay(Path(processMod.cwd()))

def userHome: F[Path] =
F.delay(Path(osMod.homedir()))

override def delete(path: Path): F[Unit] =
rmMaybeDir(path)

Expand Down
6 changes: 6 additions & 0 deletions io/jvm/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ private[file] trait FilesCompanionPlatform {
)
}).map(Path.fromNioPath)

def currentWorkingDirectory: F[Path] =
Sync[F].delay(Path(Option(System.getProperty("user.dir")).get))

def userHome: F[Path] =
Sync[F].delay(Path(Option(System.getProperty("user.home")).get))

def delete(path: Path): F[Unit] =
Sync[F].blocking(JFiles.delete(path.toNioPath))

Expand Down
6 changes: 6 additions & 0 deletions io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
permissions: Option[Permissions]
): F[Path]

/** User's current working directory */
def currentWorkingDirectory: F[Path]

/** Deletes the specified file or empty directory, failing if it does not exist. */
def delete(path: Path): F[Unit]

Expand Down Expand Up @@ -350,6 +353,9 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
permissions: Option[Permissions]
): Resource[F, Path]

/** User's home directory */
def userHome: F[Path]

/** Creates a stream of paths contained in a given file tree. Depth is unlimited. */
def walk(start: Path): Stream[F, Path] =
walk(start, Int.MaxValue, false)
Expand Down

0 comments on commit 8b285a6

Please # to comment.