Skip to content

Commit 2cc9b29

Browse files
author
Serhii Khoma
committed
feat: opendirOptionsDefault
1 parent e98b91c commit 2cc9b29

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

src/Node/FS/Aff.purs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ import Effect.Aff (Aff, Error, makeAff, nonCanceler)
8383
import Node.Buffer (Buffer)
8484
import Node.Encoding (Encoding)
8585
import Node.FS as F
86-
import Node.FS.Async (CpOptions)
87-
import Node.FS.Async (CpOptions, CpForce(..), cpOptionsDefault) as Exports
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)
8888
import Node.FS.Async as A
8989
import Node.FS.Constants (AccessMode, CopyMode)
9090
import Node.FS.Dir (Dir)
@@ -264,7 +264,7 @@ rmdir = toAff1 A.rmdir
264264
-- |
265265
-- | Deletes a directory with options.
266266
-- |
267-
rmdir' :: FilePath -> { maxRetries :: Int, retryDelay :: Int } -> Aff Unit
267+
rmdir' :: FilePath -> RmdirOptions -> Aff Unit
268268
rmdir' = toAff2 A.rmdir'
269269

270270
-- |
@@ -276,7 +276,7 @@ rm = toAff1 A.rm
276276
-- |
277277
-- | Deletes a file or directory with options.
278278
-- |
279-
rm' :: FilePath -> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } -> Aff Unit
279+
rm' :: FilePath -> RmOptions -> Aff Unit
280280
rm' = toAff2 A.rm'
281281

282282
-- |
@@ -502,7 +502,7 @@ opendir = toAff1 A.opendir
502502

503503
-- | Open a directory. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_opendir_path_options_callback)
504504
-- | for details.
505-
opendir' :: FilePath -> { bufferSize :: Int, recursive :: Boolean, encoding :: Encoding } -> Aff Dir
505+
opendir' :: FilePath -> OpendirOptions -> Aff Dir
506506
opendir' = toAff2 A.opendir'
507507

508508
-- | 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)

src/Node/FS/Async.purs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ module Node.FS.Async
2020
, unlink
2121
, rmdir
2222
, rmdir'
23+
, rmdirOptionsDefault
24+
, RmdirOptions
2325
, rm
2426
, rm'
27+
, rmOptionsDefault
28+
, RmOptions
2529
, mkdir
2630
, mkdir'
2731
, readdir
@@ -65,8 +69,10 @@ module Node.FS.Async
6569
, lchown
6670
, lutimes
6771
-- , openAsBlob
68-
, opendir'
6972
, opendir
73+
, opendir'
74+
, OpendirOptions
75+
, opendirOptionsDefault
7076
, readv
7177
, statfs
7278
-- , unwatchFile
@@ -158,8 +164,8 @@ foreign import symlinkImpl :: EffectFn4 FilePath FilePath (Nullable String) (JSC
158164
foreign import readlinkImpl :: EffectFn2 FilePath (JSCallback FilePath) Unit
159165
foreign import realpathImpl :: forall cache. EffectFn3 FilePath { | cache } (JSCallback FilePath) Unit
160166
foreign import unlinkImpl :: EffectFn2 FilePath (JSCallback Unit) Unit
161-
foreign import rmdirImpl :: EffectFn3 FilePath { maxRetries :: Int, retryDelay :: Int } (JSCallback Unit) Unit
162-
foreign import rmImpl :: EffectFn3 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } (JSCallback Unit) Unit
167+
foreign import rmdirImpl :: EffectFn3 FilePath RmdirOptions (JSCallback Unit) Unit
168+
foreign import rmImpl :: EffectFn3 FilePath RmOptions (JSCallback Unit) Unit
163169
foreign import mkdirImpl :: EffectFn3 FilePath { recursive :: Boolean, mode :: String } (JSCallback Unit) Unit
164170
-- if { withFileTypes: false, recursive: false } => ['Tidy']
165171
-- if { withFileTypes: false, recursive: true } => [ 'Tidy', 'Tidy/Codegen', 'Tidy/Codegen.purs', 'Tidy/Codegen/Class.purs', .. ]
@@ -290,32 +296,42 @@ unlink
290296
-> Effect Unit
291297
unlink file cb = runEffectFn2 unlinkImpl file (handleCallback cb)
292298

299+
type RmdirOptions = { maxRetries :: Int, retryDelay :: Int }
300+
301+
rmdirOptionsDefault :: RmdirOptions
302+
rmdirOptionsDefault = { maxRetries: 0, retryDelay: 100 }
303+
293304
-- | Deletes a directory.
294305
rmdir
295306
:: FilePath
296307
-> Callback Unit
297308
-> Effect Unit
298-
rmdir path cb = rmdir' path { maxRetries: 0, retryDelay: 100 } cb
309+
rmdir path cb = rmdir' path rmdirOptionsDefault cb
299310

300311
-- | Deletes a directory with options.
301312
rmdir'
302313
:: FilePath
303-
-> { maxRetries :: Int, retryDelay :: Int }
314+
-> RmdirOptions
304315
-> Callback Unit
305316
-> Effect Unit
306317
rmdir' path opts cb = runEffectFn3 rmdirImpl path opts (handleCallback cb)
307318

319+
type RmOptions = { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int }
320+
321+
rmOptionsDefault :: RmOptions
322+
rmOptionsDefault = { force: false, maxRetries: 100, recursive: false, retryDelay: 1000 }
323+
308324
-- | Deletes a file or directory.
309325
rm
310326
:: FilePath
311327
-> Callback Unit
312328
-> Effect Unit
313-
rm path = rm' path { force: false, maxRetries: 100, recursive: false, retryDelay: 1000 }
329+
rm path = rm' path rmOptionsDefault
314330

315331
-- | Deletes a file or directory with options.
316332
rm'
317333
:: FilePath
318-
-> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int }
334+
-> RmOptions
319335
-> Callback Unit
320336
-> Effect Unit
321337
rm' path opts cb = runEffectFn3 rmImpl path opts (handleCallback cb)
@@ -655,16 +671,21 @@ lutimes file atime mtime cb = runEffectFn4 lutimesImpl file (datetimeToUnixEpoch
655671
-- openAsBlob :: FilePath -> Promise Blob -> Effect Unit
656672
-- openAsBlob path cb = runEffectFn2 openAsBlobImpl path (handleCallback cb)
657673

674+
type OpendirOptions = { encoding :: Encoding, bufferSize :: Int, recursive :: Boolean }
675+
676+
opendirOptionsDefault :: OpendirOptions
677+
opendirOptionsDefault = { bufferSize: 32, recursive: false, encoding: UTF8 }
678+
658679
-- | Open a directory. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_opendir_path_options_callback)
659680
-- | for details.
660-
opendir' :: FilePath -> { encoding :: Encoding, bufferSize :: Int, recursive :: Boolean } -> Callback Dir -> Effect Unit
681+
opendir' :: FilePath -> OpendirOptions -> Callback Dir -> Effect Unit
661682
opendir' path { encoding, bufferSize, recursive } cb = runEffectFn3 opendirImpl path { encoding: encodingToNode encoding, bufferSize, recursive } (handleCallback cb)
662683

663684
-- | Open a directory. See the [Node Documentation](https://nodejs.org/api/fs.html#fs_fs_opendir_path_options_callback)
664685
-- | for details.
665686
-- | NOTE: encoding: 'buffer' is not supported, will throw error "TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Buffer"
666687
opendir :: FilePath -> Callback Dir -> Effect Unit
667-
opendir path = opendir' path { bufferSize: 32, recursive: false, encoding: UTF8 }
688+
opendir path = opendir' path opendirOptionsDefault
668689

669690
-- | 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)
670691
-- | for details.

test/Test.purs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Effect.Exception (Error, catchException, error, message, throw, throwExce
1111
import Node.Buffer as Buffer
1212
import Node.Encoding (Encoding(..))
1313
import Node.FS (FileFlags(..), SymlinkType(..))
14+
import Node.FS.Aff (rmOptionsDefault)
1415
import Node.FS.Async as A
1516
import Node.FS.Constants (copyFile_EXCL, r_OK, w_OK)
1617
import Node.FS.Perms (mkPerms, permsAll)
@@ -80,6 +81,7 @@ main = do
8081
log "statusChangedTime:"
8182
log $ show $ statusChangedTime stats
8283

84+
S.rm' (fp [ "tmp", "TestSymlink.js" ]) (rmOptionsDefault { force = true })
8385
S.symlink (fp [ "tmp", "Test1.js" ]) (fp [ "tmp", "TestSymlink.js" ]) FileLink
8486

8587
lstats <- S.lstat (fp [ "tmp", "TestSymlink.js" ])

test/TestDirEntries.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Effect.Class (liftEffect)
1111
import Effect.Console (log)
1212
import Effect.Exception (Error)
1313
import Node.Encoding (Encoding(..))
14+
import Node.FS.Aff (opendirOptionsDefault, rmOptionsDefault)
1415
import Node.FS.Aff as A
1516
import Node.FS.Aff.Dir (close, entries, read)
1617
import Node.FS.Dirent (Dirent, DirentNameTypeString)
@@ -24,7 +25,7 @@ outerTmpDir = "./tmp/dir-entries-test"
2425

2526
prepare :: Aff Unit
2627
prepare = do
27-
A.rm' outerTmpDir { force: true, maxRetries: 100, recursive: true, retryDelay: 1000 }
28+
A.rm' outerTmpDir (rmOptionsDefault { recursive = true, force = true })
2829
A.mkdir' outerTmpDir { recursive: true, mode: permsAll }
2930
A.writeTextFile UTF8 (Path.concat [ outerTmpDir, "1.txt" ]) "1"
3031
A.writeTextFile UTF8 (Path.concat [ outerTmpDir, "2.txt" ]) "2"
@@ -34,7 +35,7 @@ prepare = do
3435

3536
test1 :: Aff Unit
3637
test1 = do
37-
dir <- A.opendir' outerTmpDir { bufferSize: 32, recursive: true, encoding: UTF8 }
38+
dir <- A.opendir' outerTmpDir (opendirOptionsDefault { recursive = true })
3839
files' <- entries dir
3940
liftEffect $ assertEqual
4041
{ actual: show files'
@@ -74,7 +75,7 @@ test1 = do
7475

7576
test2 :: Aff Unit
7677
test2 = do
77-
dir <- A.opendir' outerTmpDir { bufferSize: 32, recursive: false, encoding: UTF8 }
78+
dir <- A.opendir' outerTmpDir (opendirOptionsDefault { recursive = false })
7879
read dir >>= \file -> liftEffect $ assertEqual
7980
{ actual: show file
8081
, expected: "(Just Dirent Dirent {\n name: 'dir1',\n parentPath: './tmp/dir-entries-test',\n path: './tmp/dir-entries-test',\n [Symbol(type)]: 2\n})"

0 commit comments

Comments
 (0)