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,32 @@ 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(path, new ReaddirOptions (encoding = " buffer" ), callback.asInstanceOf [FsCallback1 [ReaddirArrays ]])
126
+ }
127
+ promiseWithError1[FileIOError , js.Array [Buffer ]](callback)
96
128
}
97
129
98
130
@ inline
99
- def readFileFuture (file : String , options : FileInputOptions = null ): Future [js.Any ] = {
100
- promiseWithError1[FileIOError , js.Any ](fs.readFile(file, options, _))
131
+ def readdirDirentFuture (path : Buffer | String ): Future [js.Array [Dirent ]] = {
132
+ val callback : FsCallback1 [js.Array [Dirent ]] => Unit = { callback =>
133
+ fs.readdir(path, new ReaddirOptions (withFileTypes = true ), callback.asInstanceOf [FsCallback1 [ReaddirArrays ]])
134
+ }
135
+ promiseWithError1[FileIOError , js.Array [Dirent ]](callback)
136
+ }
137
+
138
+ @ inline
139
+ def readFileFuture (file : String , options : ReadFileOptions = null ): Future [Output ] = {
140
+ promiseWithError1[FileIOError , Output ](fs.readFile(file, options, _))
101
141
}
102
142
103
143
@ inline
@@ -106,8 +146,13 @@ package object fs {
106
146
}
107
147
108
148
@ inline
109
- def realpathFuture (path : String , options : FileEncodingOptions = null ): Future [String ] = {
110
- promiseWithError1[FileIOError , String ](fs.realpath(path, options, _))
149
+ def realpathFuture (path : String ): Future [String ] = {
150
+ promiseWithError1[FileIOError , String ](fs.realpath(path, _))
151
+ }
152
+
153
+ @ inline
154
+ def realpathFuture (path : String , options : FileEncodingOptions ): Future [Output ] = {
155
+ promiseWithError1[FileIOError , Output ](fs.realpath(path, options, _))
111
156
}
112
157
113
158
@ inline
@@ -162,10 +207,19 @@ package object fs {
162
207
}
163
208
164
209
@ inline
165
- def writeFileFuture (file : String , data : Buffer | String , options : FileOutputOptions = null ): Future [Unit ] = {
210
+ def writeFileFuture (file : String , data : Buffer , options : FileOutputOptions = null ): Future [Unit ] = {
166
211
promiseWithError0[FileIOError ](fs.writeFile(file, data, options, _))
167
212
}
168
213
214
+ @ inline
215
+ def writeFileFuture (file : String , data : String , options : FileOutputOptions ): Future [Unit ] = {
216
+ promiseWithError0[FileIOError ](fs.writeFile(file, data, options, _))
217
+ }
218
+
219
+ @ inline
220
+ def writeFileFuture (file : String , data : String ): Future [Unit ] = {
221
+ promiseWithError0[FileIOError ](fs.writeFile(file, data, _))
222
+ }
169
223
}
170
224
171
225
}
0 commit comments