Skip to content

Commit e48eea5

Browse files
committed
feat: move Options to separate file
1 parent 13cf95d commit e48eea5

File tree

13 files changed

+859
-490
lines changed

13 files changed

+859
-490
lines changed

src/Node/FS.purs

+2-39
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,8 @@
1-
module Node.FS
2-
( FileDescriptor
3-
, FileMode
4-
, SymlinkType(..)
5-
, symlinkTypeToNode
6-
, BufferLength
7-
, BufferOffset
8-
, ByteCount
9-
, FilePosition
10-
, module Exports
11-
) where
1+
module Node.FS (module Exports) where
122

133
import Prelude
144

155
import Data.Nullable (Nullable)
166
import Data.Nullable as Nullable
177
import Node.FS.Constants (FileFlags(..), fileFlagsToNode) as Exports
18-
19-
foreign import data FileDescriptor :: Type
20-
21-
type FileMode = Int
22-
type FilePosition = Int
23-
type BufferLength = Int
24-
type BufferOffset = Int
25-
type ByteCount = Int
26-
27-
-- | Symlink varieties.
28-
data SymlinkType = FileLink | DirLink | JunctionLink | AutodetectLink
29-
30-
-- | Convert a `SymlinkType` to a `String` in the format expected by the
31-
-- | Node.js filesystem API.
32-
symlinkTypeToNode :: SymlinkType -> Nullable String
33-
symlinkTypeToNode ty = case ty of
34-
FileLink -> Nullable.notNull "file"
35-
DirLink -> Nullable.notNull "dir"
36-
JunctionLink -> Nullable.notNull "junction"
37-
AutodetectLink -> Nullable.null
38-
39-
instance showSymlinkType :: Show SymlinkType where
40-
show FileLink = "FileLink"
41-
show DirLink = "DirLink"
42-
show JunctionLink = "JunctionLink"
43-
show AutodetectLink = "AutodetectLink"
44-
45-
derive instance eqSymlinkType :: Eq SymlinkType
8+
import Node.FS.Types as Exports

src/Node/FS/Aff.purs

+59-96
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ module Node.FS.Aff
99
, truncate
1010
, chown
1111
, chmod
12-
, stat
1312
, lstat
13+
, stat
1414
, link
1515
, symlink
1616
, readlink
@@ -40,8 +40,11 @@ module Node.FS.Aff
4040
, appendTextFile
4141
, fdOpen
4242
, fdRead
43+
, fdRead'
4344
, fdNext
4445
, fdWrite
46+
, fdWrite'
47+
, fdWriteString
4548
, fdAppend
4649
, fdClose
4750
, cp
@@ -69,7 +72,6 @@ module Node.FS.Aff
6972
-- , watch
7073
-- , watchFile
7174
, writev
72-
, module Exports
7375
) where
7476

7577
import Prelude
@@ -78,84 +80,20 @@ import Data.DateTime (DateTime)
7880
import Data.Either (Either(..))
7981
import Data.Maybe (Maybe)
8082
import Data.Tuple (Tuple)
81-
import Effect (Effect)
8283
import Effect.Aff (Aff, Error, makeAff, nonCanceler)
8384
import Node.Buffer (Buffer)
8485
import Node.Encoding (Encoding)
85-
import Node.FS as F
86-
import Node.FS.Async (CpOptions, CpForce(..), cpOptionsDefault, OpendirOptions , opendirOptionsDefault, RmOptions, rmOptionsDefault, RmdirOptions, rmdirOptionsDefault) as Exports
87-
import Node.FS.Async (CpOptions, OpendirOptions, RmdirOptions, RmOptions)
86+
import Node.FS.Types (BufferLength, BufferOffset, ByteCount, FileDescriptor, FileMode, FilePosition, SymlinkType)
87+
import Node.FS.Internal.AffUtils (toAff1, toAff2, toAff3, toAff4, toAff5)
88+
import Node.FS.Options (CpOptions, FdReadOptions, FdWriteOptions, OpendirOptions, RealpathOptions, RmOptions, RmdirOptions)
89+
import Node.FS.Constants (AccessMode, CopyMode, FileFlags)
8890
import Node.FS.Async as A
89-
import Node.FS.Constants (AccessMode, CopyMode)
9091
import Node.FS.Dir (Dir)
9192
import Node.FS.Dirent (Dirent, DirentNameTypeBuffer, DirentNameTypeString)
9293
import Node.FS.Perms (Perms)
9394
import Node.FS.Stats (Stats)
9495
import Node.Path (FilePath)
9596

96-
toAff
97-
:: forall a
98-
. (A.Callback a -> Effect Unit)
99-
-> Aff a
100-
toAff p = makeAff \k -> p k $> nonCanceler
101-
102-
toAff1
103-
:: forall a x
104-
. (x -> A.Callback a -> Effect Unit)
105-
-> x
106-
-> Aff a
107-
toAff1 f a = toAff (f a)
108-
109-
toAff2
110-
:: forall a x y
111-
. (x -> y -> A.Callback a -> Effect Unit)
112-
-> x
113-
-> y
114-
-> Aff a
115-
toAff2 f a b = toAff (f a b)
116-
117-
toAff3
118-
:: forall a x y z
119-
. (x -> y -> z -> A.Callback a -> Effect Unit)
120-
-> x
121-
-> y
122-
-> z
123-
-> Aff a
124-
toAff3 f a b c = toAff (f a b c)
125-
126-
-- toAff4
127-
-- :: forall a x y z
128-
-- . (x -> y -> z -> y -> A.Callback a -> Effect Unit)
129-
-- -> x
130-
-- -> y
131-
-- -> z
132-
-- -> y
133-
-- -> Aff a
134-
-- toAff4 f a b c d = toAff (f a b c d)
135-
136-
toAff5
137-
:: forall a w v x y z
138-
. (w -> v -> x -> y -> z -> A.Callback a -> Effect Unit)
139-
-> w
140-
-> v
141-
-> x
142-
-> y
143-
-> z
144-
-> Aff a
145-
toAff5 f a b c d e = toAff (f a b c d e)
146-
147-
-- toAff6
148-
-- :: forall a w v x y z t
149-
-- . (w -> v -> x -> y -> z -> t -> A.Callback a -> Effect Unit)
150-
-- -> w
151-
-- -> v
152-
-- -> x
153-
-- -> y
154-
-- -> z
155-
-- -> t
156-
-- -> Aff a
157-
-- toAff6 f a b c d e t = toAff (f a b c d e t)
158-
15997
access :: FilePath -> Aff (Maybe Error)
16098
access path = makeAff \k -> do
16199
A.access path (k <<< Right)
@@ -226,7 +164,7 @@ link = toAff2 A.link
226164
symlink
227165
:: FilePath
228166
-> FilePath
229-
-> F.SymlinkType
167+
-> SymlinkType
230168
-> Aff Unit
231169
symlink = toAff3 A.symlink
232170

@@ -246,7 +184,7 @@ realpath = toAff1 A.realpath
246184
-- | Find the canonicalized absolute location for a path using a cache object
247185
-- | for already resolved paths.
248186
-- |
249-
realpath' :: forall cache. FilePath -> { | cache } -> Aff FilePath
187+
realpath' :: FilePath -> RealpathOptions -> Aff FilePath
250188
realpath' = toAff2 A.realpath'
251189

252190
-- |
@@ -376,46 +314,71 @@ appendTextFile = toAff3 A.appendTextFile
376314
-- | for details.
377315
fdOpen
378316
:: FilePath
379-
-> F.FileFlags
380-
-> Maybe F.FileMode
381-
-> Aff F.FileDescriptor
317+
-> FileFlags
318+
-> Maybe FileMode
319+
-> Aff FileDescriptor
382320
fdOpen = toAff3 A.fdOpen
383321

384322
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback)
385323
-- | for details.
386324
fdRead
387-
:: F.FileDescriptor
325+
:: FileDescriptor
388326
-> Buffer
389-
-> F.BufferOffset
390-
-> F.BufferLength
391-
-> Maybe F.FilePosition
392-
-> Aff F.ByteCount
327+
-> BufferOffset
328+
-> BufferLength
329+
-> Maybe FilePosition
330+
-> Aff ByteCount
393331
fdRead = toAff5 A.fdRead
394332

333+
-- | Read from a file asynchronously. See the [Node Documentation](https://nodejs.org/docs/latest/api/fs.html#fsreadfd-options-callback)
334+
-- | for details.
335+
fdRead'
336+
:: FileDescriptor
337+
-> FdReadOptions
338+
-> Aff (Tuple ByteCount Buffer)
339+
fdRead' = toAff2 A.fdRead'
340+
395341
-- | Convenience function to fill the whole buffer from the current
396342
-- | file position.
397-
fdNext :: F.FileDescriptor -> Buffer -> Aff F.ByteCount
343+
fdNext :: FileDescriptor -> Buffer -> Aff ByteCount
398344
fdNext = toAff2 A.fdNext
399345

400346
-- | Write to a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback)
401347
-- | for details.
402348
fdWrite
403-
:: F.FileDescriptor
349+
:: FileDescriptor
404350
-> Buffer
405-
-> F.BufferOffset
406-
-> F.BufferLength
407-
-> Maybe F.FilePosition
408-
-> Aff F.ByteCount
351+
-> BufferOffset
352+
-> BufferLength
353+
-> Maybe FilePosition
354+
-> Aff ByteCount
409355
fdWrite = toAff5 A.fdWrite
410356

357+
-- | Write from a file asynchronously. See the [Node Documentation](https://nodejs.org/docs/latest/api/fs.html#fswritefd-options-callback)
358+
-- | for details.
359+
fdWrite'
360+
:: FileDescriptor
361+
-> FdWriteOptions
362+
-> Aff (Tuple ByteCount Buffer)
363+
fdWrite' = toAff2 A.fdWrite'
364+
365+
-- It is unsafe to use fs.write() multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream() is recommended.
366+
fdWriteString
367+
:: FileDescriptor
368+
-> String
369+
-> Maybe FilePosition
370+
-> Encoding
371+
-> Aff (Tuple ByteCount String)
372+
fdWriteString = toAff4 A.fdWriteString
373+
411374
-- | Convenience function to append the whole buffer to the current
412375
-- | file position.
413-
fdAppend :: F.FileDescriptor -> Buffer -> Aff F.ByteCount
376+
fdAppend :: FileDescriptor -> Buffer -> Aff ByteCount
414377
fdAppend = toAff2 A.fdAppend
415378

416379
-- | Close a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_close_fd_callback)
417380
-- | for details.
418-
fdClose :: F.FileDescriptor -> Aff Unit
381+
fdClose :: FileDescriptor -> Aff Unit
419382
fdClose = toAff1 A.fdClose
420383

421384
-- | Copy a file asynchronously. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fspromises_copyfile_src_dest_mode)
@@ -428,32 +391,32 @@ cp' = toAff3 A.cp'
428391

429392
-- | Change permissions on a file descriptor. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_fchmod_fd_mode_callback)
430393
-- | for details.
431-
fchmod :: F.FileDescriptor -> Perms -> Aff Unit
394+
fchmod :: FileDescriptor -> Perms -> Aff Unit
432395
fchmod = toAff2 A.fchmod
433396

434397
-- | Change ownership of a file descriptor. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_fchown_fd_uid_gid_callback)
435398
-- | for details.
436-
fchown :: F.FileDescriptor -> Int -> Int -> Aff Unit
399+
fchown :: FileDescriptor -> Int -> Int -> Aff Unit
437400
fchown = toAff3 A.fchown
438401

439402
-- | Synchronize a file's in-core state with storage. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_fdatasync_fd_callback)
440403
-- | for details.
441-
fdatasync :: F.FileDescriptor -> Aff Unit
404+
fdatasync :: FileDescriptor -> Aff Unit
442405
fdatasync = toAff1 A.fdatasync
443406

444407
-- | Get file status information. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_fstat_fd_callback)
445408
-- | for details.
446-
fstat :: F.FileDescriptor -> Aff Stats
409+
fstat :: FileDescriptor -> Aff Stats
447410
fstat = toAff1 A.fstat
448411

449412
-- | Flushes a file descriptor to disk. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_fsync_fd_callback)
450413
-- | for details.
451-
fsync :: F.FileDescriptor -> Aff Unit
414+
fsync :: FileDescriptor -> Aff Unit
452415
fsync = toAff1 A.fsync
453416

454417
-- | Truncate a file to a specified length. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_ftruncate_fd_len_callback)
455418
-- | for details.
456-
ftruncate :: F.FileDescriptor -> Int -> Aff Unit
419+
ftruncate :: FileDescriptor -> Int -> Aff Unit
457420
ftruncate = toAff2 A.ftruncate
458421

459422
-- | Change file timestamps for a file descriptor. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_futimes_fd_atime_mtime_callback)
@@ -507,7 +470,7 @@ opendir' = toAff2 A.opendir'
507470

508471
-- | Read from a file descriptor into a buffer array. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_readv_fd_buffers_position_callback)
509472
-- | for details.
510-
readv :: F.FileDescriptor -> Array Buffer -> Maybe F.FilePosition -> Aff (Tuple F.ByteCount (Array Buffer))
473+
readv :: FileDescriptor -> Array Buffer -> Maybe FilePosition -> Aff (Tuple ByteCount (Array Buffer))
511474
readv = toAff3 A.readv
512475

513476
-- | TODO: bigint, path Buffer Url
@@ -534,5 +497,5 @@ statfs = toAff1 A.statfs
534497

535498
-- | Write from an array of buffers to a file descriptor. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_writev_fd_buffers_position_callback)
536499
-- | for details.
537-
writev :: F.FileDescriptor -> Array Buffer -> Maybe F.FilePosition -> Aff (Tuple F.ByteCount (Array Buffer))
500+
writev :: FileDescriptor -> Array Buffer -> Maybe FilePosition -> Aff (Tuple ByteCount (Array Buffer))
538501
writev = toAff3 A.writev

src/Node/FS/Aff/Dir.purs

+2-14
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,10 @@ import Effect.Class (liftEffect)
1616
import Effect.Exception (Error)
1717
import Effect.Ref (Ref)
1818
import Effect.Ref as Ref
19-
import Node.FS.Dir (Callback2, Dir)
19+
import Node.FS.Dir (Dir)
2020
import Node.FS.Dir as Dir
2121
import Node.FS.Dirent (Dirent, DirentNameTypeString)
22-
23-
toAff
24-
:: forall a
25-
. (Callback2 a -> Effect Unit)
26-
-> Aff a
27-
toAff p = makeAff \k -> p k $> nonCanceler
28-
29-
toAff1
30-
:: forall a x
31-
. (x -> Callback2 a -> Effect Unit)
32-
-> x
33-
-> Aff a
34-
toAff1 f a = toAff (f a)
22+
import Node.FS.Internal.AffUtils
3523

3624
read :: Dir -> Aff (Maybe (Dirent DirentNameTypeString))
3725
read = toAff1 Dir.read

0 commit comments

Comments
 (0)