Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Wrap new FFI: access, copyFile, mkdtemp #40

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Bugfixes:

Other improvements:

## [v9.2.0](https://github.com/purescript-node/purescript-node-fs-aff/releases/tag/v9.2.0) - 2023-03-24

New features:

- Added `access`, `copyFile`, and `mkdtemp` (#40 by @JordanMartinez)

## [v9.1.0](https://github.com/purescript-node/purescript-node-fs-aff/releases/tag/v9.1.0) - 2022-06-10

New features:
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"dependencies": {
"purescript-aff": "^7.0.0",
"purescript-either": "^6.0.0",
"purescript-node-fs": "^8.1.0",
"purescript-node-fs": "^8.2.0",
"purescript-node-path": "^5.0.0"
},
"devDependencies": {
Expand Down
145 changes: 91 additions & 54 deletions src/Node/FS/Aff.purs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module Node.FS.Aff
( rename
( access
, access'
, copyFile
, copyFile'
, mkdtemp
, mkdtemp'
, rename
, truncate
, chown
, chmod
Expand Down Expand Up @@ -35,53 +41,82 @@ module Node.FS.Aff
import Prelude

import Data.DateTime (DateTime)
import Data.Either (Either(..))
import Data.Maybe (Maybe)
import Effect (Effect)
import Effect.Aff (Aff, makeAff, nonCanceler)
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
:: 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
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
:: 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.
-- |
Expand Down Expand Up @@ -121,10 +156,11 @@ link = toAff2 A.link
-- |
-- | Creates a symlink.
-- |
symlink :: FilePath
-> FilePath
-> F.SymlinkType
-> Aff Unit
symlink
:: FilePath
-> FilePath
-> F.SymlinkType
-> Aff Unit
symlink = toAff3 A.symlink

-- |
Expand Down Expand Up @@ -164,7 +200,6 @@ rmdir = toAff1 A.rmdir
rmdir' :: FilePath -> { maxRetries :: Int, retryDelay :: Int } -> Aff Unit
rmdir' = toAff2 A.rmdir'


-- |
-- | Deletes a file or directory.
-- |
Expand Down Expand Up @@ -237,23 +272,24 @@ appendFile = toAff2 A.appendFile
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
:: 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
:: 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
Expand All @@ -263,12 +299,13 @@ 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
:: 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
Expand Down