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

Commit 87349b8

Browse files
authored
Merge pull request #354 from exoego/dir-dirent-separation
💥[fs] Dir & Dirent is now Dir[T] & Dirent[T] (parameterized for path name)
2 parents 7e6583e + c8e2c52 commit 87349b8

File tree

2 files changed

+80
-51
lines changed

2 files changed

+80
-51
lines changed

app/nodejs-v14/src/main/scala/io/scalajs/nodejs/fs/Fs.scala

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -528,15 +528,17 @@ trait Fs extends js.Object with FSConstants {
528528
def openSync(path: Path): FileDescriptor = js.native
529529

530530
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
531-
def opendir(path: Path, options: OpendirOptions, callback: FsCallback1[Fs.Dir]): Unit = js.native
531+
def opendir(path: Path, options: OpendirOptions, callback: FsCallback1[Fs.Dir[String] | Fs.Dir[Buffer]]): Unit =
532+
js.native
532533

533534
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
534-
def opendir(path: Path, callback: FsCallback1[Fs.Dir]): Unit = js.native
535+
def opendir(path: Path, callback: FsCallback1[Fs.Dir[String]]): Unit = js.native
535536

536537
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendirSync(path: Path,
537538
options: OpendirOptions
538-
): Fs.Dir = js.native
539-
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendirSync(path: Path): Fs.Dir = js.native
539+
): Fs.Dir[String] | Fs.Dir[Buffer] = js.native
540+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendirSync(path: Path): Fs.Dir[String] =
541+
js.native
540542

541543
/** Read data from the file specified by fd.
542544
* @param fd is the file descriptor
@@ -600,7 +602,7 @@ trait Fs extends js.Object with FSConstants {
600602
* of the names of the files in the directory excluding '.' and '..'.
601603
* @example fs.readdir(path[, options], callback)
602604
*/
603-
def readdir(path: Path, options: String, callback: FsCallback1[js.Array[String]]): Unit = js.native
605+
def readdir(path: Path, options: String, callback: FsCallback1[ReaddirArrays]): Unit = js.native
604606
def readdir(path: Path, options: FileEncodingOptions, callback: FsCallback1[ReaddirArrays]): Unit = js.native
605607
def readdir(path: Path, options: ReaddirOptions, callback: FsCallback1[ReaddirArrays2]): Unit = js.native
606608
def readdir(path: Path, callback: FsCallback1[js.Array[String]]): Unit = js.native
@@ -613,9 +615,10 @@ trait Fs extends js.Object with FSConstants {
613615
* to 'buffer', the filenames returned will be passed as Buffer objects.
614616
* @return an array of filenames excluding '.' and '..'.
615617
*/
616-
def readdirSync(path: Path, options: String): js.Array[String] = js.native
617-
def readdirSync(path: Path, options: ReaddirOptions): js.Array[String] = js.native
618-
def readdirSync(path: Path): js.Array[String] = js.native
618+
def readdirSync(path: Path, options: String): ReaddirArrays = js.native
619+
def readdirSync(path: Path, options: FileEncodingOptions): ReaddirArrays = js.native
620+
def readdirSync(path: Path, options: ReaddirOptions): ReaddirArrays2 = js.native
621+
def readdirSync(path: Path): js.Array[String] = js.native
619622

620623
/** Asynchronously reads the entire contents of a file.
621624
* @param file filename or file descriptor
@@ -1108,18 +1111,20 @@ object Fs extends Fs {
11081111
def open(path: Path): js.Promise[FileHandle] = js.native
11091112
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendir(path: Path,
11101113
options: OpendirOptions
1111-
): js.Promise[Dir] = js.native
1112-
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendir(path: Path): js.Promise[Dir] =
1114+
): js.Promise[Dir[String] | Dir[Buffer]] = js.native
1115+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def opendir(
1116+
path: Path
1117+
): js.Promise[Dir[String]] =
11131118
js.native
1114-
def readdir(path: Path, options: ReaddirOptions): js.Promise[js.Array[String] | js.Array[Dirent]] = js.native
1115-
def readdir(path: Path, encoding: String): js.Promise[js.Array[String]] = js.native
1116-
def readdir(path: Path, options: FileEncodingOptions): js.Promise[js.Array[String]] = js.native
1117-
def readdir(path: Path): js.Promise[js.Array[String]] = js.native
1118-
def readlink(path: Path): js.Promise[String] = js.native
1119-
def readlink(path: Path, options: String): js.Promise[Output] = js.native
1120-
def readlink(path: Path, options: FileEncodingOptions): js.Promise[Output] = js.native
1121-
def rename(oldPath: Path, newPath: Path): js.Promise[Unit] = js.native
1122-
def rmdir(path: Path): js.Promise[Unit] = js.native
1119+
def readdir(path: Path, options: ReaddirOptions): js.Promise[ReaddirArrays2] = js.native
1120+
def readdir(path: Path, encoding: String): js.Promise[ReaddirArrays] = js.native
1121+
def readdir(path: Path, options: FileEncodingOptions): js.Promise[ReaddirArrays] = js.native
1122+
def readdir(path: Path): js.Promise[js.Array[String]] = js.native
1123+
def readlink(path: Path): js.Promise[String] = js.native
1124+
def readlink(path: Path, options: String): js.Promise[Output] = js.native
1125+
def readlink(path: Path, options: FileEncodingOptions): js.Promise[Output] = js.native
1126+
def rename(oldPath: Path, newPath: Path): js.Promise[Unit] = js.native
1127+
def rmdir(path: Path): js.Promise[Unit] = js.native
11231128
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
11241129
def rmdir(path: Path, options: RmdirOptions): js.Promise[Unit] = js.native
11251130
def stat(path: Path, options: StatOptions): js.Promise[StatsVariant] = js.native
@@ -1194,27 +1199,27 @@ object Fs extends Fs {
11941199
val promises: FsPromises = js.native
11951200

11961201
@js.native
1197-
class Dirent() extends js.Object {
1202+
class Dirent[TName]() extends js.Object {
11981203
def isBlockDevice(): Boolean = js.native
11991204
def isCharacterDevice(): Boolean = js.native
12001205
def isDirectory(): Boolean = js.native
12011206
def isFIFO(): Boolean = js.native
12021207
def isFile(): Boolean = js.native
12031208
def isSocket(): Boolean = js.native
12041209
def isSymbolicLink(): Boolean = js.native
1205-
val name: String | Buffer = js.native
1210+
val name: TName = js.native
12061211
}
12071212

12081213
@enableMembersIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
12091214
@js.native
1210-
trait Dir extends js.Object {
1211-
def close(): js.Promise[Unit] = js.native
1212-
def close(callback: js.Function1[js.Error, Any]): Unit = js.native
1213-
def closeSync(): Unit = js.native
1214-
def path: String = js.native
1215-
def read(): js.Promise[Dirent] = js.native
1216-
def read(callback: js.Function2[js.Error, Dirent, Any]): Unit = js.native
1217-
def readSync(): Dirent = js.native
1215+
trait Dir[T] extends js.Object {
1216+
def close(): js.Promise[Unit] = js.native
1217+
def close(callback: js.Function1[js.Error, Any]): Unit = js.native
1218+
def closeSync(): Unit = js.native
1219+
def path: String = js.native
1220+
def read(): js.Promise[Dirent[T]] = js.native
1221+
def read(callback: js.Function2[js.Error, Dirent[T], Any]): Unit = js.native
1222+
def readSync(): Dirent[T] = js.native
12181223

12191224
// TODO: Implement AsyncIterable[Dirent]
12201225
}

app/nodejs-v14/src/main/scala/io/scalajs/nodejs/fs/package.scala

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package object fs {
1919
type FileWriteOptions = FileAppendOptions
2020

2121
type ReaddirArrays = js.Array[String] | js.Array[Buffer]
22-
type ReaddirArrays2 = ReaddirArrays | js.Array[fs.Dirent]
22+
type ReaddirArrays2 = ReaddirArrays | js.Array[fs.Dirent[String]] | js.Array[fs.Dirent[Buffer]]
2323

24-
type Dirent = Fs.Dirent
24+
type Dirent[T] = Fs.Dirent[T]
2525

2626
type StatsVariant = Stats | BigIntStats
2727

@@ -221,14 +221,14 @@ package object fs {
221221

222222
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
223223
@inline
224-
def opendirFuture(path: Path, options: OpendirOptions): Future[Fs.Dir] = {
225-
promiseWithError1[FileIOError, Fs.Dir](instance.opendir(path, options, _))
224+
def opendirFuture(path: Path, options: OpendirOptions): Future[Fs.Dir[String] | Fs.Dir[Buffer]] = {
225+
promiseWithError1[FileIOError, Fs.Dir[String] | Fs.Dir[Buffer]](instance.opendir(path, options, _))
226226
}
227227

228228
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
229229
@inline
230-
def opendirFuture(path: Path): Future[Fs.Dir] = {
231-
promiseWithError1[FileIOError, Fs.Dir](instance.opendir(path, _))
230+
def opendirFuture(path: Path): Future[Fs.Dir[String]] = {
231+
promiseWithError1[FileIOError, Fs.Dir[String]](instance.opendir(path, _))
232232
}
233233

234234
@inline
@@ -242,8 +242,8 @@ package object fs {
242242
}
243243

244244
@inline
245-
def readdirFuture(path: Path, encoding: String): Future[js.Array[String]] = {
246-
promiseWithError1[FileIOError, js.Array[String]](instance.readdir(path, encoding, _))
245+
def readdirFuture(path: Path, encoding: String): Future[ReaddirArrays] = {
246+
promiseWithError1[FileIOError, ReaddirArrays](instance.readdir(path, encoding, _))
247247
}
248248

249249
@inline
@@ -252,11 +252,8 @@ package object fs {
252252
}
253253

254254
@inline
255-
def readdirFuture(path: Path, options: FileEncodingOptions): Future[js.Array[String]] = {
256-
val callback: FsCallback1[js.Array[String]] => Unit = { callback =>
257-
instance.readdir(path, options, callback.asInstanceOf[FsCallback1[ReaddirArrays]])
258-
}
259-
promiseWithError1[FileIOError, js.Array[String]](callback)
255+
def readdirFuture(path: Path, options: FileEncodingOptions): Future[ReaddirArrays] = {
256+
promiseWithError1[FileIOError, ReaddirArrays](instance.readdir(path, options, _))
260257
}
261258

262259
@inline
@@ -272,26 +269,53 @@ package object fs {
272269
}
273270

274271
@inline
275-
def readdirDirentFuture(path: Buffer): Future[js.Array[Dirent]] = {
276-
val callback: FsCallback1[js.Array[Dirent]] => Unit = { callback =>
272+
def readdirDirentFuture(path: Buffer): Future[js.Array[Dirent[String]]] = {
273+
val callback: FsCallback1[js.Array[Dirent[String]]] => Unit = { callback =>
277274
instance.readdir(
278275
path,
279276
ReaddirOptions(withFileTypes = true),
280277
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
281278
)
282279
}
283-
promiseWithError1[FileIOError, js.Array[Dirent]](callback)
280+
promiseWithError1[FileIOError, js.Array[Dirent[String]]](callback)
284281
}
285282
@inline
286-
def readdirDirentFuture(path: String): Future[js.Array[Dirent]] = {
287-
val callback: FsCallback1[js.Array[Dirent]] => Unit = { callback =>
283+
def readdirDirentFuture(path: String): Future[js.Array[Dirent[String]]] = {
284+
val callback: FsCallback1[js.Array[Dirent[String]]] => Unit = { callback =>
288285
instance.readdir(
289286
path,
290287
ReaddirOptions(withFileTypes = true),
291288
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
292289
)
293290
}
294-
promiseWithError1[FileIOError, js.Array[Dirent]](callback)
291+
promiseWithError1[FileIOError, js.Array[Dirent[String]]](callback)
292+
}
293+
294+
@inline
295+
def readdirDirentFuture(path: Buffer,
296+
encoding: String
297+
): Future[js.Array[Dirent[String]] | js.Array[Dirent[Buffer]]] = {
298+
val callback: FsCallback1[js.Array[Dirent[String]]] => Unit = { callback =>
299+
instance.readdir(
300+
path,
301+
ReaddirOptions(withFileTypes = true, encoding = encoding),
302+
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
303+
)
304+
}
305+
promiseWithError1[FileIOError, js.Array[Dirent[String]]](callback)
306+
}
307+
@inline
308+
def readdirDirentFuture(path: String,
309+
encoding: String
310+
): Future[js.Array[Dirent[String]] | js.Array[Dirent[Buffer]]] = {
311+
val callback: FsCallback1[js.Array[Dirent[String]]] => Unit = { callback =>
312+
instance.readdir(
313+
path,
314+
ReaddirOptions(withFileTypes = true, encoding = encoding),
315+
callback.asInstanceOf[FsCallback1[ReaddirArrays2]]
316+
)
317+
}
318+
promiseWithError1[FileIOError, js.Array[Dirent[String]]](callback)
295319
}
296320

297321
@inline
@@ -531,11 +555,11 @@ package object fs {
531555
*
532556
* @param instance the given [[Fs.Dir]] instance
533557
*/
534-
implicit final class FsDirExtensions(private val instance: Fs.Dir) extends AnyVal {
558+
implicit final class FsDirExtensions[T](private val instance: Fs.Dir[T]) extends AnyVal {
535559
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
536560
@inline
537-
def readFuture(): Future[Option[Fs.Dirent]] = {
538-
promiseWithError1[js.Error, Option[Fs.Dirent]](f => {
561+
def readFuture(): Future[Option[Fs.Dirent[T]]] = {
562+
promiseWithError1[js.Error, Option[Fs.Dirent[T]]](f => {
539563
instance.read((err, dir) => {
540564
f(err, Option(dir))
541565
})

0 commit comments

Comments
 (0)