This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
/
Aff.purs
319 lines (275 loc) · 6.74 KB
/
Aff.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
module Node.FS.Aff
( access
, access'
, copyFile
, copyFile'
, mkdtemp
, mkdtemp'
, rename
, truncate
, chown
, chmod
, stat
, link
, symlink
, readlink
, realpath
, realpath'
, unlink
, rmdir
, rmdir'
, rm
, rm'
, mkdir
, mkdir'
, readdir
, utimes
, readFile
, readTextFile
, writeFile
, writeTextFile
, appendFile
, appendTextFile
, fdOpen
, fdRead
, fdNext
, fdWrite
, fdAppend
, fdClose
) where
import Prelude
import Data.DateTime (DateTime)
import Data.Either (Either(..))
import Data.Maybe (Maybe)
import Effect (Effect)
import Effect.Aff (Aff, Error, makeAff, nonCanceler)
import Node.Buffer (Buffer)
import Node.Encoding (Encoding)
import Node.FS as F
import Node.FS.Async as A
import Node.FS.Constants (AccessMode, CopyMode)
import Node.FS.Perms (Perms)
import Node.FS.Stats (Stats)
import Node.Path (FilePath)
toAff
:: forall a
. (A.Callback a -> Effect Unit)
-> Aff a
toAff p = makeAff \k -> p k $> nonCanceler
toAff1
:: forall a x
. (x -> A.Callback a -> Effect Unit)
-> x
-> Aff a
toAff1 f a = toAff (f a)
toAff2
:: forall a x y
. (x -> y -> A.Callback a -> Effect Unit)
-> x
-> y
-> Aff a
toAff2 f a b = toAff (f a b)
toAff3
:: forall a x y z
. (x -> y -> z -> A.Callback a -> Effect Unit)
-> x
-> y
-> z
-> Aff a
toAff3 f a b c = toAff (f a b c)
toAff5
:: forall a w v x y z
. (w -> v -> x -> y -> z -> A.Callback a -> Effect Unit)
-> w
-> v
-> x
-> y
-> z
-> Aff a
toAff5 f a b c d e = toAff (f a b c d e)
access :: String -> Aff (Maybe Error)
access path = makeAff \k -> do
A.access path (k <<< Right)
pure nonCanceler
access' :: String -> AccessMode -> Aff (Maybe Error)
access' path mode = makeAff \k -> do
A.access' path mode (k <<< Right)
pure nonCanceler
copyFile :: String -> String -> Aff Unit
copyFile = toAff2 A.copyFile
copyFile' :: String -> String -> CopyMode -> Aff Unit
copyFile' = toAff3 A.copyFile'
mkdtemp :: String -> Aff String
mkdtemp = toAff1 A.mkdtemp
mkdtemp' :: String -> Encoding -> Aff String
mkdtemp' = toAff2 A.mkdtemp'
-- |
-- | Rename a file.
-- |
rename :: FilePath -> FilePath -> Aff Unit
rename = toAff2 A.rename
-- |
-- | Truncates a file to the specified length.
-- |
truncate :: FilePath -> Int -> Aff Unit
truncate = toAff2 A.truncate
-- |
-- | Changes the ownership of a file.
-- |
chown :: FilePath -> Int -> Int -> Aff Unit
chown = toAff3 A.chown
-- |
-- | Changes the permissions of a file.
-- |
chmod :: FilePath -> Perms -> Aff Unit
chmod = toAff2 A.chmod
-- |
-- | Gets file statistics.
-- |
stat :: FilePath -> Aff Stats
stat = toAff1 A.stat
-- |
-- | Creates a link to an existing file.
-- |
link :: FilePath -> FilePath -> Aff Unit
link = toAff2 A.link
-- |
-- | Creates a symlink.
-- |
symlink
:: FilePath
-> FilePath
-> F.SymlinkType
-> Aff Unit
symlink = toAff3 A.symlink
-- |
-- | Reads the value of a symlink.
-- |
readlink :: FilePath -> Aff FilePath
readlink = toAff1 A.readlink
-- |
-- | Find the canonicalized absolute location for a path.
-- |
realpath :: FilePath -> Aff FilePath
realpath = toAff1 A.realpath
-- |
-- | Find the canonicalized absolute location for a path using a cache object
-- | for already resolved paths.
-- |
realpath' :: forall cache. FilePath -> { | cache } -> Aff FilePath
realpath' = toAff2 A.realpath'
-- |
-- | Deletes a file.
-- |
unlink :: FilePath -> Aff Unit
unlink = toAff1 A.unlink
-- |
-- | Deletes a directory.
-- |
rmdir :: FilePath -> Aff Unit
rmdir = toAff1 A.rmdir
-- |
-- | Deletes a directory with options.
-- |
rmdir' :: FilePath -> { maxRetries :: Int, retryDelay :: Int } -> Aff Unit
rmdir' = toAff2 A.rmdir'
-- |
-- | Deletes a file or directory.
-- |
rm :: FilePath -> Aff Unit
rm = toAff1 A.rmdir
-- |
-- | Deletes a file or directory with options.
-- |
rm' :: FilePath -> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } -> Aff Unit
rm' = toAff2 A.rm'
-- |
-- | Makes a new directory.
-- |
mkdir :: FilePath -> Aff Unit
mkdir = toAff1 A.mkdir
-- |
-- | Makes a new directory with all of its options.
-- |
mkdir' :: FilePath -> { recursive :: Boolean, mode :: Perms } -> Aff Unit
mkdir' = toAff2 A.mkdir'
-- |
-- | Reads the contents of a directory.
-- |
readdir :: FilePath -> Aff (Array FilePath)
readdir = toAff1 A.readdir
-- |
-- | Sets the accessed and modified times for the specified file.
-- |
utimes :: FilePath -> DateTime -> DateTime -> Aff Unit
utimes = toAff3 A.utimes
-- |
-- | Reads the entire contents of a file returning the result as a raw buffer.
-- |
readFile :: FilePath -> Aff Buffer
readFile = toAff1 A.readFile
-- |
-- | Reads the entire contents of a text file with the specified encoding.
-- |
readTextFile :: Encoding -> FilePath -> Aff String
readTextFile = toAff2 A.readTextFile
-- |
-- | Writes a buffer to a file.
-- |
writeFile :: FilePath -> Buffer -> Aff Unit
writeFile = toAff2 A.writeFile
-- |
-- | Writes text to a file using the specified encoding.
-- |
writeTextFile :: Encoding -> FilePath -> String -> Aff Unit
writeTextFile = toAff3 A.writeTextFile
-- |
-- | Appends the contents of a buffer to a file.
-- |
appendFile :: FilePath -> Buffer -> Aff Unit
appendFile = toAff2 A.appendFile
-- |
-- | Appends text to a file using the specified encoding.
-- |
appendTextFile :: Encoding -> FilePath -> String -> Aff Unit
appendTextFile = toAff3 A.appendTextFile
-- | Open a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback)
-- | for details.
fdOpen
:: FilePath
-> F.FileFlags
-> Maybe F.FileMode
-> Aff F.FileDescriptor
fdOpen = toAff3 A.fdOpen
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
-- | for details.
fdRead
:: F.FileDescriptor
-> Buffer
-> F.BufferOffset
-> F.BufferLength
-> Maybe F.FilePosition
-> Aff F.ByteCount
fdRead = toAff5 A.fdRead
-- | Convenience function to fill the whole buffer from the current
-- | file position.
fdNext :: F.FileDescriptor -> Buffer -> Aff F.ByteCount
fdNext = toAff2 A.fdNext
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
-- | for details.
fdWrite
:: F.FileDescriptor
-> Buffer
-> F.BufferOffset
-> F.BufferLength
-> Maybe F.FilePosition
-> Aff F.ByteCount
fdWrite = toAff5 A.fdWrite
-- | Convenience function to append the whole buffer to the current
-- | file position.
fdAppend :: F.FileDescriptor -> Buffer -> Aff F.ByteCount
fdAppend = toAff2 A.fdAppend
-- | Close a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_close_fd_callback)
-- | for details.
fdClose :: F.FileDescriptor -> Aff Unit
fdClose = toAff1 A.fdClose