1
1
package io .scalajs .nodejs
2
2
3
- import io . scalajs . RawOptions
3
+ import com . thoughtworks . enableIf
4
4
import io .scalajs .nodejs .buffer .Buffer
5
+ import io .scalajs .nodejs .url .URL
5
6
import io .scalajs .util .PromiseHelper ._
6
7
7
8
import scala .concurrent .Future
8
9
import scala .scalajs .js
9
- import scala .scalajs .js .typedarray .Uint8Array
10
+ import scala .scalajs .js .typedarray .{ DataView , TypedArray , Uint8Array }
10
11
import scala .scalajs .js .|
11
12
12
13
/**
13
14
* fs package object
14
15
*/
15
16
package object fs {
16
17
18
+ type Path = Uint8Array | String | URL
19
+ type Time = Int | String | js.Date
20
+ type BufferLike = TypedArray [_, _] | DataView
21
+ type Output = String | Buffer
22
+
23
+ @ enableIf(io.scalajs.nodejs.CompilerSwitches .gteNodeJs10)
24
+ type Dirent = Fs .Dirent
25
+
17
26
// ///////////////////////////////////////////////////////////////////////////////
18
27
// Implicit conversions and classes
19
28
// ///////////////////////////////////////////////////////////////////////////////
@@ -22,10 +31,14 @@ package object fs {
22
31
* File System Extensions
23
32
* @param fs the given [[Fs file system ]] instance
24
33
*/
25
- final implicit class FsExtensions (val fs : Fs ) extends AnyVal {
34
+ final implicit class FsExtensions (private val fs : Fs ) extends AnyVal {
35
+ @ inline
36
+ def accessFuture (path : Buffer | String ): Future [Unit ] = {
37
+ promiseWithError0[FileIOError ](fs.access(path, _))
38
+ }
26
39
27
40
@ inline
28
- def accessFuture (path : Buffer | String , mode : FileMode = null ): Future [Unit ] = {
41
+ def accessFuture (path : Buffer | String , mode : FileMode ): Future [Unit ] = {
29
42
promiseWithError0[FileIOError ](fs.access(path, mode, _))
30
43
}
31
44
@@ -51,7 +64,7 @@ package object fs {
51
64
def fdatasyncFuture (fd : FileDescriptor ): Future [Unit ] = promiseWithError0[FileIOError ](fs.fdatasync(fd, _))
52
65
53
66
@ inline
54
- def futimesFuture (fd : FileDescriptor , atime : Integer , mtime : Integer ): Future [Unit ] = {
67
+ def futimesFuture (fd : FileDescriptor , atime : Time , mtime : Time ): Future [Unit ] = {
55
68
promiseWithError0[FileIOError ](fs.futimes(fd, atime, mtime, _))
56
69
}
57
70
@@ -71,14 +84,23 @@ package object fs {
71
84
}
72
85
73
86
@ inline
74
- def mkdirFuture (path : Buffer | String , mode : FileMode = null ): Future [Unit ] = {
87
+ def mkdirFuture (path : Buffer | String , mode : FileMode ): Future [Unit ] = {
75
88
promiseWithError0[FileIOError ](fs.mkdir(path, mode, _))
76
89
}
77
90
78
91
@ inline
79
- def openFuture (path : Buffer | String , flags : Flags , mode : FileMode = null ): Future [FileDescriptor ] = {
92
+ def mkdirFuture (path : Buffer | String ): Future [Unit ] = {
93
+ promiseWithError0[FileIOError ](fs.mkdir(path, _))
94
+ }
95
+
96
+ @ inline
97
+ def openFuture (path : Buffer | String , flags : Flags , mode : FileMode ): Future [FileDescriptor ] = {
80
98
promiseWithError1[FileIOError , FileDescriptor ](fs.open(path, flags, mode, _))
81
99
}
100
+ @ inline
101
+ def openFuture (path : Buffer | String , flags : Flags ): Future [FileDescriptor ] = {
102
+ promiseWithError1[FileIOError , FileDescriptor ](fs.open(path, flags, _))
103
+ }
82
104
83
105
@ inline
84
106
def readFuture (fd : FileDescriptor ,
@@ -90,14 +112,37 @@ package object fs {
90
112
}
91
113
92
114
@ inline
93
- def readdirFuture (path : Buffer | String ,
94
- options : String | FileEncodingOptions | RawOptions = null ): Future [js.Array [String ]] = {
95
- promiseWithError1[FileIOError , js.Array [String ]](fs.readdir(path, options, _))
115
+ def readdirFuture (path : Buffer | String , options : String = " utf8" ): Future [js.Array [String ]] = {
116
+ val callback : FsCallback1 [js.Array [String ]] => Unit = { callback =>
117
+ fs.readdir(path, options, callback.asInstanceOf [FsCallback1 [ReaddirArrays ]])
118
+ }
119
+ promiseWithError1[FileIOError , js.Array [String ]](callback)
120
+ }
121
+
122
+ @ inline
123
+ def readdirBufferFuture (path : Buffer | String ): Future [js.Array [Buffer ]] = {
124
+ val callback : FsCallback1 [js.Array [Buffer ]] => Unit = { callback =>
125
+ fs.readdir(
126
+ path,
127
+ new FileEncodingOptions (encoding = " buffer" ),
128
+ callback.asInstanceOf [FsCallback1 [ReaddirArrays ]]
129
+ )
130
+ }
131
+ promiseWithError1[FileIOError , js.Array [Buffer ]](callback)
96
132
}
97
133
134
+ @ enableIf(io.scalajs.nodejs.CompilerSwitches .gteNodeJs10)
98
135
@ inline
99
- def readFileFuture (file : String , options : FileInputOptions = null ): Future [js.Any ] = {
100
- promiseWithError1[FileIOError , js.Any ](fs.readFile(file, options, _))
136
+ def readdirDirentFuture (path : Buffer | String ): Future [js.Array [Dirent ]] = {
137
+ val callback : FsCallback1 [js.Array [Dirent ]] => Unit = { callback =>
138
+ fs.readdir(path, new ReaddirOptions (withFileTypes = true ), callback.asInstanceOf [FsCallback1 [ReaddirArrays ]])
139
+ }
140
+ promiseWithError1[FileIOError , js.Array [Dirent ]](callback)
141
+ }
142
+
143
+ @ inline
144
+ def readFileFuture (file : String , options : ReadFileOptions = null ): Future [Output ] = {
145
+ promiseWithError1[FileIOError , Output ](fs.readFile(file, options, _))
101
146
}
102
147
103
148
@ inline
@@ -106,8 +151,13 @@ package object fs {
106
151
}
107
152
108
153
@ inline
109
- def realpathFuture (path : String , options : FileEncodingOptions = null ): Future [String ] = {
110
- promiseWithError1[FileIOError , String ](fs.realpath(path, options, _))
154
+ def realpathFuture (path : String ): Future [String ] = {
155
+ promiseWithError1[FileIOError , String ](fs.realpath(path, _))
156
+ }
157
+
158
+ @ inline
159
+ def realpathFuture (path : String , options : FileEncodingOptions ): Future [Output ] = {
160
+ promiseWithError1[FileIOError , Output ](fs.realpath(path, options, _))
111
161
}
112
162
113
163
@ inline
@@ -139,10 +189,10 @@ package object fs {
139
189
140
190
@ inline
141
191
def writeFuture (fd : FileDescriptor ,
142
- buffer : Buffer | Uint8Array ,
143
- offset : Integer = null ,
144
- length : Integer = null ,
145
- position : Integer = null ): Future [(FileType , Buffer )] = {
192
+ buffer : Uint8Array ,
193
+ offset : Int | Null = null ,
194
+ length : Int | Null = null ,
195
+ position : Int | Null = null ): Future [(FileType , Buffer )] = {
146
196
promiseWithError2[FileIOError , Int , Buffer ](fs.write(fd, buffer, offset, length, position, _))
147
197
}
148
198
@@ -162,10 +212,19 @@ package object fs {
162
212
}
163
213
164
214
@ inline
165
- def writeFileFuture (file : String , data : Buffer | String , options : FileOutputOptions = null ): Future [Unit ] = {
215
+ def writeFileFuture (file : String , data : Buffer , options : FileOutputOptions = null ): Future [Unit ] = {
166
216
promiseWithError0[FileIOError ](fs.writeFile(file, data, options, _))
167
217
}
168
218
219
+ @ inline
220
+ def writeFileFuture (file : String , data : String , options : FileOutputOptions ): Future [Unit ] = {
221
+ promiseWithError0[FileIOError ](fs.writeFile(file, data, options, _))
222
+ }
223
+
224
+ @ inline
225
+ def writeFileFuture (file : String , data : String ): Future [Unit ] = {
226
+ promiseWithError0[FileIOError ](fs.writeFile(file, data, _))
227
+ }
169
228
}
170
229
171
230
}
0 commit comments