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

Commit 1806367

Browse files
authored
Merge pull request #108 from exoego/complete-fx-extension
Overhaul fs module
2 parents 3334f97 + d0c945f commit 1806367

File tree

6 files changed

+364
-79
lines changed

6 files changed

+364
-79
lines changed

app/current/src/main/scala/io/scalajs/nodejs/fs/FSConstants.scala

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.scalajs.nodejs
22
package fs
33

4+
import com.thoughtworks.enableIf
5+
46
import scala.scalajs.js
57

68
/**
@@ -36,6 +38,24 @@ trait FSConstants extends js.Object {
3638
*/
3739
val X_OK: FileMode = js.native
3840

41+
/////////////////////////////////////////////////////////////////////////////////
42+
// File Mode Constants
43+
//
44+
// The following constants are meant for use with fs.Stats.
45+
///////////////////////////////////////////////////////////////////////////////// `
46+
val S_IRGRP: FileMode = js.native
47+
val S_IROTH: FileMode = js.native
48+
val S_IRUSR: FileMode = js.native
49+
val S_IRWXG: FileMode = js.native
50+
val S_IRWXO: FileMode = js.native
51+
val S_IRWXU: FileMode = js.native
52+
val S_IWGRP: FileMode = js.native
53+
val S_IWOTH: FileMode = js.native
54+
val S_IWUSR: FileMode = js.native
55+
val S_IXGRP: FileMode = js.native
56+
val S_IXOTH: FileMode = js.native
57+
val S_IXUSR: FileMode = js.native
58+
3959
/////////////////////////////////////////////////////////////////////////////////
4060
// File Open Constants
4161
//
@@ -120,6 +140,11 @@ trait FSConstants extends js.Object {
120140
*/
121141
val O_NONBLOCK: Int = js.native
122142

143+
/**
144+
* Flag indicating that the file is opened for synchronized I/O with write operations waiting for data integrity.
145+
*/
146+
val O_DSYNC: Int = js.native
147+
123148
/////////////////////////////////////////////////////////////////////////////////
124149
// File Type Constants
125150
//
@@ -167,4 +192,12 @@ trait FSConstants extends js.Object {
167192
*/
168193
val S_IFSOCK: FileType = js.native
169194

195+
/////////////////////////////////////////////////////////////////////////////////
196+
// File Copy Constants
197+
/////////////////////////////////////////////////////////////////////////////////
198+
val COPYFILE_EXCL: Int = js.native
199+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
200+
val COPYFILE_FICLONE: Int = js.native
201+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
202+
val COPYFILE_FICLONE_FORCE: Int = js.native
170203
}

app/current/src/main/scala/io/scalajs/nodejs/fs/Fs.scala

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import io.scalajs.nodejs.events.IEventEmitter
77

88
import scala.scalajs.js
99
import scala.scalajs.js.annotation.JSImport
10-
import scala.scalajs.js.typedarray.{ArrayBufferView, Uint8Array}
10+
import scala.scalajs.js.typedarray
1111
import scala.scalajs.js.|
1212

1313
/**
@@ -456,10 +456,10 @@ trait Fs extends IEventEmitter with FSConstants {
456456
* @param path the path
457457
* @param mode the mode
458458
*/
459-
def mkdirSync(path: Buffer | String, mode: FileMode = js.native): Unit = js.native
459+
def mkdirSync(path: Path, mode: FileMode = js.native): Unit = js.native
460460

461461
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs10)
462-
def mkdirSync(path: Buffer | String, mode: MkdirOptions): Unit = js.native
462+
def mkdirSync(path: Path, mode: MkdirOptions): Unit = js.native
463463

464464
/**
465465
* Creates a unique temporary directory.
@@ -729,7 +729,7 @@ trait Fs extends IEventEmitter with FSConstants {
729729
* @return the symbolic link's string value.
730730
*/
731731
def readlinkSync(path: Path, options: String | FileEncodingOptions): Output = js.native
732-
def readlinkSync(path: Path): Output = js.native
732+
def readlinkSync(path: Path): String = js.native
733733

734734
/**
735735
* Asynchronous realpath(2).
@@ -980,7 +980,7 @@ trait Fs extends IEventEmitter with FSConstants {
980980
* @param options the [[FSWatcherOptions optional settings]]
981981
* @param listener the callback
982982
*/
983-
def watchFile(filename: Path, options: FileWatcherOptions, listener: FsCallback2[Stats, Stats]): Unit =
983+
def watchFile(filename: Path, options: FileWatcherOptions, listener: js.Function2[Stats, Stats, Any]): Unit =
984984
js.native
985985

986986
/**
@@ -993,7 +993,7 @@ trait Fs extends IEventEmitter with FSConstants {
993993
* @param filename the filename (Buffer | String)
994994
* @param listener the callback
995995
*/
996-
def watchFile(filename: Buffer | String, listener: FsCallback2[Stats, Stats]): Unit = js.native
996+
def watchFile(filename: Path, listener: js.Function2[Stats, Stats, Any]): Unit = js.native
997997

998998
/**
999999
* Write buffer to the file specified by fd.
@@ -1013,7 +1013,7 @@ trait Fs extends IEventEmitter with FSConstants {
10131013
* @example {{{ fs.write(fd, buffer[, offset[, length[, position]]], callback) }}}
10141014
**/
10151015
def write(fd: FileDescriptor,
1016-
buffer: Uint8Array,
1016+
buffer: typedarray.Uint8Array,
10171017
offset: Int | Null,
10181018
length: Int | Null,
10191019
position: Int | Null,
@@ -1049,19 +1049,18 @@ trait Fs extends IEventEmitter with FSConstants {
10491049
string: String,
10501050
position: Int,
10511051
encoding: String,
1052-
callback: FsCallback2[Int, String]): Unit = js.native
1053-
def write(fd: FileDescriptor, string: String, position: Int, callback: FsCallback2[Int, String]): Unit = js.native
1054-
def write(fd: FileDescriptor, string: String, encoding: String, callback: FsCallback2[Int, String]): Unit = js.native
1055-
def write(fd: FileDescriptor, string: String, callback: FsCallback2[Int, String]): Unit = js.native
1052+
callback: FsCallback2[Int, String]): Unit = js.native
1053+
def write(fd: FileDescriptor, string: String, position: Int, callback: FsCallback2[Int, String]): Unit = js.native
1054+
def write(fd: FileDescriptor, string: String, callback: FsCallback2[Int, String]): Unit = js.native
10561055

10571056
/**
10581057
* Asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer.
10591058
* The encoding option is ignored if data is a buffer. It defaults to 'utf8'
10601059
* @example fs.writeFile(file, data[, options], callback)
10611060
*/
1062-
def writeFile(file: String, data: Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
1061+
def writeFile(file: String, data: typedarray.Uint8Array, options: FileWriteOptions, callback: FsCallback0): Unit =
10631062
js.native
1064-
def writeFile(file: String, data: Uint8Array, callback: FsCallback0): Unit = js.native
1063+
def writeFile(file: String, data: typedarray.Uint8Array, callback: FsCallback0): Unit = js.native
10651064
def writeFile(file: String, data: String, options: FileWriteOptions, callback: FsCallback0): Unit =
10661065
js.native
10671066
def writeFile(file: String, data: String, callback: FsCallback0): Unit = js.native
@@ -1076,7 +1075,9 @@ trait Fs extends IEventEmitter with FSConstants {
10761075
* @return undefined.
10771076
* @example fs.writeFileSync(file, data[, options])
10781077
*/
1079-
def writeFileSync(file: Path | FileDescriptor, data: Uint8Array, options: FileWriteOptions = js.native): Unit =
1078+
def writeFileSync(file: Path | FileDescriptor,
1079+
data: typedarray.Uint8Array,
1080+
options: FileWriteOptions = js.native): Unit =
10801081
js.native
10811082
def writeFileSync(file: Path | FileDescriptor, data: String, options: FileWriteOptions): Unit =
10821083
js.native
@@ -1097,10 +1098,14 @@ trait Fs extends IEventEmitter with FSConstants {
10971098
* @param position refers to the offset from the beginning of the file where this data should be written.
10981099
* @example {{{ fs.writeSync(fd, buffer[, offset[, length[, position]]]) }}}
10991100
*/
1100-
def writeSync(fd: FileDescriptor, buffer: Uint8Array, offset: Int, length: Int, position: Int = js.native): Unit =
1101+
def writeSync(fd: FileDescriptor,
1102+
buffer: typedarray.Uint8Array,
1103+
offset: Int,
1104+
length: Int,
1105+
position: Int = js.native): Unit =
11011106
js.native
1102-
def writeSync(fd: FileDescriptor, buffer: Uint8Array, offset: Int): Unit = js.native
1103-
def writeSync(fd: FileDescriptor, buffer: Uint8Array): Unit = js.native
1107+
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array, offset: Int): Unit = js.native
1108+
def writeSync(fd: FileDescriptor, buffer: typedarray.Uint8Array): Unit = js.native
11041109
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int, position: Int): Unit = js.native
11051110
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int, length: Int): Unit = js.native
11061111
def writeSync(fd: FileDescriptor, buffer: BufferLike, offset: Int): Unit = js.native
@@ -1119,11 +1124,14 @@ trait Fs extends IEventEmitter with FSConstants {
11191124
def writeSync(fd: FileDescriptor, data: String, encoding: String): Unit = js.native
11201125
def writeSync(fd: FileDescriptor, data: String): Unit = js.native
11211126

1127+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
11221128
def writev(fd: FileDescriptor,
1123-
buffers: js.Array[ArrayBufferView],
1129+
buffers: js.Array[typedarray.ArrayBufferView],
11241130
position: Int,
1125-
fsCallback2: FsCallback2[Int, js.Array[ArrayBufferView]]): Unit = js.native
1126-
def writevSync(fd: FileDescriptor, buffers: js.Array[ArrayBufferView], position: Int = js.native): Unit = js.native
1131+
fsCallback2: FsCallback2[Int, js.Array[typedarray.ArrayBufferView]]): Unit = js.native
1132+
@enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12)
1133+
def writevSync(fd: FileDescriptor, buffers: js.Array[typedarray.ArrayBufferView], position: Int = js.native): Unit =
1134+
js.native
11271135
}
11281136

11291137
/**
@@ -1295,6 +1303,7 @@ class RmdirOptions(var emfileWait: js.UndefOr[Int] = 1000,
12951303

12961304
@js.native
12971305
trait RealpathObject extends js.Object {
1298-
def native(path: Path, options: FileEncodingOptions = js.native): Output = js.native
1299-
def native(path: Path, encoding: String): String = js.native
1306+
def native(path: Path, options: FileEncodingOptions, callback: FsCallback1[Output]): Unit = js.native
1307+
def native(path: Path, encoding: String, callback: FsCallback1[Output]): Unit = js.native
1308+
def native(path: Path, callback: FsCallback1[String]): Unit = js.native
13001309
}

0 commit comments

Comments
 (0)