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 #238 from exoego/fs
Browse files Browse the repository at this point in the history
[fs] Various updates for Node.js v14
  • Loading branch information
exoego authored May 28, 2020
2 parents a3b8817 + 2aa0137 commit 84b2992
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 22 deletions.
19 changes: 19 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/FSWatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.scalajs.nodejs
package fs

import _root_.net.exoego.scalajs.types.util.Factory
import com.thoughtworks.enableIf
import io.scalajs.nodejs.events.IEventEmitter

import scala.scalajs.js
Expand All @@ -18,6 +19,24 @@ trait FSWatcher extends IEventEmitter {
* @since 0.5.8
*/
def close(): Unit = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def ref(): FSWatcher = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def unref(): FSWatcher = js.native
}

/**
* A successful call to fs.watchFile() method will return a new fs.StatWatcher object.
*/
@js.native
trait FSStatWatcher extends IEventEmitter {
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def ref(): FSStatWatcher = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def unref(): FSStatWatcher = js.native
}

@Factory
Expand Down
89 changes: 69 additions & 20 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/Fs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import _root_.net.exoego.scalajs.types.util.Factory

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.typedarray
import scala.scalajs.js.|

/**
Expand Down Expand Up @@ -430,9 +429,11 @@ trait Fs extends js.Object with FSConstants {
/**
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
* mode defaults to 0o777.
*
* After v13.11.0, in recursive mode, the `callback`` now receives the first created path as an 2nd argument.
* @example fs.mkdir(path[, mode], callback)
*/
def mkdir(path: Path, mode: MkdirOptions, callback: FsCallback0): Unit = js.native
def mkdir(path: Path, mode: MkdirOptions, callback: FsCallback0 | FsRecursiveCallback0): Unit = js.native

/**
* Asynchronous mkdir(2). No arguments other than a possible exception are given to the completion callback.
Expand All @@ -448,7 +449,10 @@ trait Fs extends js.Object with FSConstants {
*/
def mkdirSync(path: Path, mode: FileMode = js.native): Unit = js.native

def mkdirSync(path: Path, mode: MkdirOptions): Unit = js.native
/**
* @return After Node.js v13.11.0, in recursive mode, the first created path is returned now. Otherwise undefined
*/
def mkdirSync(path: Path, mode: MkdirOptions): js.UndefOr[Path] = js.native

/**
* Creates a unique temporary directory.
Expand Down Expand Up @@ -595,6 +599,9 @@ trait Fs extends js.Object with FSConstants {
callback: FsCallback2[Int, Buffer]
): Unit = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def read(fd: FileDescriptor, options: ReadOptions, callback: FsCallback2[Int, Buffer]): Unit = js.native

/**
* Synchronous version of fs.read().
* @param fd is the file descriptor
Expand All @@ -609,6 +616,17 @@ trait Fs extends js.Object with FSConstants {

def readSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int, position: Int): Int = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def readv(fd: FileDescriptor,
buffers: js.Array[js.typedarray.ArrayBufferView],
options: ReadOptions,
callback: FsCallback2[Int, js.Array[js.typedarray.ArrayBufferView]]
): Unit = js.native

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def readvSync(fd: FileDescriptor, buffers: js.Array[js.typedarray.ArrayBufferView], options: ReadOptions): Unit =
js.native

/**
* Asynchronous readdir(3). Reads the contents of a directory.
* @param path the path (Buffer | String)
Expand Down Expand Up @@ -974,7 +992,7 @@ trait Fs extends js.Object with FSConstants {
* @param options the [[FSWatcherOptions optional settings]]
* @param listener the callback
*/
def watchFile(filename: Path, options: FileWatcherOptions, listener: js.Function2[Stats, Stats, Any]): Unit =
def watchFile(filename: Path, options: FileWatcherOptions, listener: js.Function2[Stats, Stats, Any]): FSStatWatcher =
js.native

/**
Expand All @@ -987,7 +1005,7 @@ trait Fs extends js.Object with FSConstants {
* @param filename the filename (Buffer | String)
* @param listener the callback
*/
def watchFile(filename: Path, listener: js.Function2[Stats, Stats, Any]): Unit = js.native
def watchFile(filename: Path, listener: js.Function2[Stats, Stats, Any]): FSStatWatcher = js.native

/**
* Write buffer to the file specified by fd.
Expand All @@ -1007,7 +1025,7 @@ trait Fs extends js.Object with FSConstants {
* @example {{{ fs.write(fd, buffer[, offset[, length[, position]]], callback) }}}
**/
def write(fd: FileDescriptor,
buffer: typedarray.Uint8Array,
buffer: js.typedarray.Uint8Array,
offset: Int | Null,
length: Int | Null,
position: Int | Null,
Expand Down Expand Up @@ -1054,9 +1072,9 @@ trait Fs extends js.Object with FSConstants {
* The encoding option is ignored if data is a buffer. It defaults to 'utf8'
* @example fs.writeFile(file, data[, options], callback)
*/
def writeFile(file: String, data: typedarray.Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
def writeFile(file: String, data: js.typedarray.Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
js.native
def writeFile(file: String, data: typedarray.Uint8Array, callback: FsCallback0): Unit = js.native
def writeFile(file: String, data: js.typedarray.Uint8Array, callback: FsCallback0): Unit = js.native
def writeFile(file: String, data: String, options: FileWriteOptions, callback: FsCallback0): Unit =
js.native
def writeFile(file: String, data: String, callback: FsCallback0): Unit = js.native
Expand All @@ -1070,7 +1088,7 @@ trait Fs extends js.Object with FSConstants {
* @example fs.writeFileSync(file, data[, options])
*/
def writeFileSync(file: Path | FileDescriptor,
data: typedarray.Uint8Array,
data: js.typedarray.Uint8Array,
options: FileWriteOptions = js.native
): Unit =
js.native
Expand All @@ -1092,14 +1110,14 @@ trait Fs extends js.Object with FSConstants {
* @example {{{ fs.writeSync(fd, buffer[, offset[, length[, position]]]) }}}
*/
def writeSync(fd: FileDescriptor,
buffer: typedarray.Uint8Array,
buffer: js.typedarray.Uint8Array,
offset: Int,
length: Int,
position: Int = js.native
): Unit =
js.native
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array, offset: Int): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: js.typedarray.Uint8Array, offset: Int): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: js.typedarray.Uint8Array): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int, position: Int): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int): Unit = js.native
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int): Unit = js.native
Expand All @@ -1120,13 +1138,15 @@ trait Fs extends js.Object with FSConstants {

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
def writev(fd: FileDescriptor,
buffers: js.Array[typedarray.ArrayBufferView],
buffers: js.Array[js.typedarray.ArrayBufferView],
position: Int,
fsCallback2: FsCallback2[Int, js.Array[typedarray.ArrayBufferView]]
fsCallback2: FsCallback2[Int, js.Array[js.typedarray.ArrayBufferView]]
): Unit = js.native
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
def writevSync(fd: FileDescriptor, buffers: js.Array[typedarray.ArrayBufferView], position: Int = js.native): Unit =
js.native
def writevSync(fd: FileDescriptor,
buffers: js.Array[js.typedarray.ArrayBufferView],
position: Int = js.native
): Unit = js.native
}

/**
Expand Down Expand Up @@ -1190,7 +1210,10 @@ object Fs extends Fs {
offset: Int | Null,
length: Int | Null,
position: Int | Null
): js.Promise[BufferIOResult[T]] = js.native
): js.Promise[BufferIOResult[T]] = js.native
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
def readd[T <: js.typedarray.TypedArray[_, _]](options: ReadOptions): js.Promise[BufferIOResult[T]] = js.native

def readFile(): js.Promise[Buffer] = js.native
def readFile(encoding: String): js.Promise[String] = js.native
def readFile(options: ReadFileOptions): js.Promise[Output] = js.native
Expand Down Expand Up @@ -1264,6 +1287,14 @@ trait FileEncodingOptions {
var encoding: js.UndefOr[String] = js.undefined
}

@Factory
trait ReadOptions extends js.Object {
var buffer: js.UndefOr[BufferLike] = js.undefined
var offset: js.UndefOr[Int] = js.undefined
var length: js.UndefOr[Int] = js.undefined
var position: js.UndefOr[Int] = js.undefined
}

@Factory
trait ReaddirOptions extends js.Object {
var encoding: js.UndefOr[String] = js.undefined
Expand All @@ -1272,7 +1303,8 @@ trait ReaddirOptions extends js.Object {

@Factory
trait OpendirOptions extends js.Object {
var encoding: js.UndefOr[String] = js.undefined
var encoding: js.UndefOr[String] = js.undefined
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
var bufferSize: js.UndefOr[Double] = js.undefined
}

Expand All @@ -1292,6 +1324,9 @@ trait FileInputOptions extends js.Object {
var start: js.UndefOr[Int] = js.undefined
var end: js.UndefOr[Int] = js.undefined
var highWaterMark: js.UndefOr[Int] = js.undefined

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
var fs: js.UndefOr[js.Object] = js.undefined
}

@Factory
Expand All @@ -1303,6 +1338,9 @@ trait FileOutputOptions extends js.Object {
var autoClose: js.UndefOr[Boolean] = js.undefined
var emitClose: js.UndefOr[Boolean] = js.undefined
var start: js.UndefOr[Int] = js.undefined

@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14)
var fs: js.UndefOr[js.Object] = js.undefined
}

@Factory
Expand All @@ -1324,8 +1362,19 @@ trait MkdirOptions extends js.Object {

@Factory
trait RmdirOptions extends js.Object {
var emfileWait: js.UndefOr[Int] = js.undefined
var maxBusyTries: js.UndefOr[Int] = js.undefined
@deprecated(
"Tha option has been removed, and EMFILE errors use the same retry logic as other errors.",
"Node.js v13.3.0, v12.16.0"
)
var emfileWait: js.UndefOr[Int] = js.undefined
@deprecated("Use maxRetries", "Node.js v13.3.0, v12.16.0")
var maxBusyTries: js.UndefOr[Int] = js.undefined
var maxRetries: js.UndefOr[Int] = js.undefined
@deprecated(
"Tha option has been removed, and EMFILE errors use the same retry logic as other errors.",
"Node.js v13.3.0, v12.16.0"
)
var retryDelay: js.UndefOr[Int] = js.undefined
var recursive: js.UndefOr[Boolean] = js.undefined
}

Expand Down
4 changes: 2 additions & 2 deletions app/current/src/main/scala/io/scalajs/nodejs/fs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ package object fs {
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
@inline
def rmdirRecursiveFuture(path: Path, options: RmdirOptions): Future[Unit] = {
val recursiveEnabled =
RmdirOptions(recursive = true, maxBusyTries = options.maxBusyTries, emfileWait = options.emfileWait)
val recursiveEnabled = js.Object.assign(js.Object(), options).asInstanceOf[RmdirOptions]
recursiveEnabled.recursive = true
promiseWithError0[FileIOError](instance.rmdir(path, recursiveEnabled, _))
}

Expand Down
2 changes: 2 additions & 0 deletions app/current/src/main/scala/io/scalajs/nodejs/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ package object nodejs {

type FsCallback3[A, B, C] = js.Function4[FileIOError, A, B, C, Any]

type FsRecursiveCallback0 = js.Function2[FileIOError, fs.Path, Any]

type GID = Int

type UID = Int
Expand Down

0 comments on commit 84b2992

Please sign in to comment.