Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #103 from exoego/fs-enhancement
Browse files Browse the repository at this point in the history
Fs enhancement
  • Loading branch information
exoego authored Oct 4, 2019
2 parents 9f26aee + 1bb37f0 commit 08959ed
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/Fs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,9 @@ class StatOptions(val bigint: js.UndefOr[Boolean] = js.undefined) extends js.Obj
class MkdirOptions(val recursive: js.UndefOr[Boolean] = js.undefined, val mode: js.UndefOr[FileMode] = js.undefined)
extends js.Object

class RmdirOptions(val emfileWait: js.UndefOr[Int] = js.undefined,
val maxBusyTries: js.UndefOr[Int] = js.undefined,
val recursive: js.UndefOr[Boolean] = js.undefined)
class RmdirOptions(var emfileWait: js.UndefOr[Int] = 1000,
var maxBusyTries: js.UndefOr[Int] = 3,
var recursive: js.UndefOr[Boolean] = js.undefined)
extends js.Object

@js.native
Expand Down
11 changes: 11 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ package object fs {
promiseWithError0[FileIOError](fs.mkdir(path, _))
}

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
@inline
def mkdirFuture(path: Buffer | String, options: MkdirOptions): Future[Unit] = {
promiseWithError0[FileIOError](fs.mkdir(path, options, _))
}

@inline
def openFuture(path: Buffer | String, flags: Flags, mode: FileMode): Future[FileDescriptor] = {
promiseWithError1[FileIOError, FileDescriptor](fs.open(path, flags, mode, _))
Expand Down Expand Up @@ -168,6 +174,11 @@ package object fs {
@inline
def rmdirFuture(path: Buffer | String): Future[Unit] = promiseWithError0[FileIOError](fs.rmdir(path, _))

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
@inline
def rmdirFuture(path: Buffer | String, options: RmdirOptions): Future[Unit] =
promiseWithError0[FileIOError](fs.rmdir(path, options, _))

@inline
def statFuture(path: String): Future[Stats] = promiseWithError1[FileIOError, Stats](fs.stat(path, _))

Expand Down
43 changes: 43 additions & 0 deletions app/current/src/test/scala/io/scalajs/nodejs/fs/FsAsyncTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.scalajs.nodejs.fs

import org.scalatest.{AsyncFunSpec, BeforeAndAfterEach}

import scala.concurrent.ExecutionContext

class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {
override implicit val executionContext = ExecutionContext.Implicits.global

private val dir = "x.v12/x.FsAsyncTest/foo/bar"

override def afterEach(): Unit = {
Seq(
"x.v12/x.FsAsyncTest/foo/bar",
"x.v12/x.FsAsyncTest/foo",
"x.v12/x.FsAsyncTest",
"x.v12"
).foreach { d =>
if (Fs.existsSync(d)) Fs.rmdirSync(d)
}
}

describe("Fs") {
it("should support recursive-rmdir") {
for {
dirExistsBeforeMkdir <- Fs.existsFuture(dir)
_ <- Fs.mkdirFuture(dir, new MkdirOptions(recursive = true))
_ <- Fs.writeFileFuture("x.v12/hoge.txt", "foo")
fileStat <- Fs.statFuture("x.v12/hoge.txt")
dirStat <- Fs.statFuture(dir)
dirExistsAfterMkdir <- Fs.existsFuture(dir)
_ <- Fs.rmdirFuture("x.v12", new RmdirOptions(recursive = true))
dirExistsAfterRmdir <- Fs.existsFuture("x.v12")
} yield {
assert(!dirExistsBeforeMkdir)
assert(fileStat.isFile())
assert(dirStat.isDirectory())
assert(dirExistsAfterMkdir)
assert(!dirExistsAfterRmdir)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.scalajs.nodejs.fs

import org.scalatest.{ AsyncFunSpec, BeforeAndAfterEach }

import scala.concurrent.ExecutionContext

class FsAsyncTest extends AsyncFunSpec with BeforeAndAfterEach {
override implicit val executionContext = ExecutionContext.Implicits.global

private val dir = "x.FsAsyncTest/foo/bar"

override def afterEach(): Unit = {
Seq(
"x.FsAsyncTest/foo/bar",
"x.FsAsyncTest/foo",
"x.FsAsyncTest"
).foreach { d =>
if (Fs.existsSync(d)) Fs.rmdirSync(d)
}
}

describe("Fs") {
it("should support recursive-mkdir") {
for {
dirExistsBeforeMkdir <- Fs.existsFuture(dir)
_ <- Fs.mkdirFuture(dir, new MkdirOptions(recursive = true))
dirStat <- Fs.statFuture(dir)
dirExistsAfterMkdir <- Fs.existsFuture(dir)
} yield {
assert(!dirExistsBeforeMkdir)
assert(dirStat.isDirectory())
assert(dirExistsAfterMkdir)
}
}
}
}

0 comments on commit 08959ed

Please sign in to comment.