diff --git a/CHANGELOG.md b/CHANGELOG.md index 9632a1e..9a015d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Use `EffectFn` throughout instead of unsafe `mkEffect` utility (#70 by @colinwahl) ## [v8.1.0](https://github.com/purescript-node/purescript-node-fs/releases/tag/v8.1.0) - 2022-06-10 diff --git a/src/Node/FS/Async.js b/src/Node/FS/Async.js index 460c3cb..3ddc7a7 100644 --- a/src/Node/FS/Async.js +++ b/src/Node/FS/Async.js @@ -23,13 +23,3 @@ export { write as writeImpl, close as closeImpl } from "fs"; - -export function handleCallbackImpl(left, right, f) { - return function (err, value) { - if (err) { - f(left(err))(); - } else { - f(right(value))(); - } - }; -} diff --git a/src/Node/FS/Async.purs b/src/Node/FS/Async.purs index 8060d4a..998bde4 100644 --- a/src/Node/FS/Async.purs +++ b/src/Node/FS/Async.purs @@ -39,59 +39,53 @@ import Prelude import Data.DateTime (DateTime) import Data.DateTime.Instant (fromDateTime, unInstant) import Data.Either (Either(..)) -import Data.Function.Uncurried (Fn2, Fn6, Fn4, Fn3, runFn2, runFn6, runFn4, runFn3) import Data.Int (round) import Data.Maybe (Maybe(..)) -import Data.Nullable (Nullable, toNullable) +import Data.Nullable (Nullable, toMaybe, toNullable) import Data.Time.Duration (Milliseconds(..)) import Effect (Effect) import Effect.Exception (Error) +import Effect.Uncurried (EffectFn2, EffectFn3, EffectFn4, EffectFn6, mkEffectFn2, runEffectFn2, runEffectFn3, runEffectFn4, runEffectFn6) import Node.Buffer (Buffer, size) import Node.Encoding (Encoding) import Node.FS (FileDescriptor, ByteCount, FilePosition, BufferLength, BufferOffset, FileMode, FileFlags, SymlinkType, fileFlagsToNode, symlinkTypeToNode) -import Node.FS.Internal (mkEffect) import Node.FS.Perms (Perms, permsToString, all, mkPerms) import Node.FS.Stats (StatsObj, Stats(..)) import Node.Path (FilePath) -type JSCallback a = Fn2 (Nullable Error) a Unit - -foreign import handleCallbackImpl :: - forall a. Fn3 (Error -> Either Error a) - (a -> Either Error a) - (Callback a) - (JSCallback a) - -handleCallback :: forall a. (Callback a) -> JSCallback a -handleCallback cb = runFn3 handleCallbackImpl Left Right cb +type JSCallback a = EffectFn2 (Nullable Error) a Unit +handleCallback :: forall a. Callback a -> JSCallback a +handleCallback cb = mkEffectFn2 \err a -> case toMaybe err of + Nothing -> cb (Right a) + Just err' -> cb (Left err') -- | Type synonym for callback functions. type Callback a = Either Error a -> Effect Unit -foreign import renameImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit -foreign import truncateImpl :: Fn3 FilePath Int (JSCallback Unit) Unit -foreign import chownImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit -foreign import chmodImpl :: Fn3 FilePath String (JSCallback Unit) Unit -foreign import statImpl :: Fn2 FilePath (JSCallback StatsObj) Unit -foreign import lstatImpl :: Fn2 FilePath (JSCallback StatsObj) Unit -foreign import linkImpl :: Fn3 FilePath FilePath (JSCallback Unit) Unit -foreign import symlinkImpl :: Fn4 FilePath FilePath String (JSCallback Unit) Unit -foreign import readlinkImpl :: Fn2 FilePath (JSCallback FilePath) Unit -foreign import realpathImpl :: forall cache. Fn3 FilePath { | cache } (JSCallback FilePath) Unit -foreign import unlinkImpl :: Fn2 FilePath (JSCallback Unit) Unit -foreign import rmdirImpl :: Fn3 FilePath { maxRetries :: Int, retryDelay :: Int } (JSCallback Unit) Unit -foreign import rmImpl :: Fn3 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } (JSCallback Unit) Unit -foreign import mkdirImpl :: Fn3 FilePath { recursive :: Boolean, mode :: String } (JSCallback Unit) Unit -foreign import readdirImpl :: Fn2 FilePath (JSCallback (Array FilePath)) Unit -foreign import utimesImpl :: Fn4 FilePath Int Int (JSCallback Unit) Unit -foreign import readFileImpl :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit -foreign import writeFileImpl :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit -foreign import appendFileImpl :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit -foreign import openImpl :: Fn4 FilePath String (Nullable FileMode) (JSCallback FileDescriptor) Unit -foreign import readImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit -foreign import writeImpl :: Fn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit -foreign import closeImpl :: Fn2 FileDescriptor (JSCallback Unit) Unit +foreign import renameImpl :: EffectFn3 FilePath FilePath (JSCallback Unit) Unit +foreign import truncateImpl :: EffectFn3 FilePath Int (JSCallback Unit) Unit +foreign import chownImpl :: EffectFn4 FilePath Int Int (JSCallback Unit) Unit +foreign import chmodImpl :: EffectFn3 FilePath String (JSCallback Unit) Unit +foreign import statImpl :: EffectFn2 FilePath (JSCallback StatsObj) Unit +foreign import lstatImpl :: EffectFn2 FilePath (JSCallback StatsObj) Unit +foreign import linkImpl :: EffectFn3 FilePath FilePath (JSCallback Unit) Unit +foreign import symlinkImpl :: EffectFn4 FilePath FilePath String (JSCallback Unit) Unit +foreign import readlinkImpl :: EffectFn2 FilePath (JSCallback FilePath) Unit +foreign import realpathImpl :: forall cache. EffectFn3 FilePath { | cache } (JSCallback FilePath) Unit +foreign import unlinkImpl :: EffectFn2 FilePath (JSCallback Unit) Unit +foreign import rmdirImpl :: EffectFn3 FilePath { maxRetries :: Int, retryDelay :: Int } (JSCallback Unit) Unit +foreign import rmImpl :: EffectFn3 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } (JSCallback Unit) Unit +foreign import mkdirImpl :: EffectFn3 FilePath { recursive :: Boolean, mode :: String } (JSCallback Unit) Unit +foreign import readdirImpl :: EffectFn2 FilePath (JSCallback (Array FilePath)) Unit +foreign import utimesImpl :: EffectFn4 FilePath Int Int (JSCallback Unit) Unit +foreign import readFileImpl :: forall a opts. EffectFn3 FilePath { | opts } (JSCallback a) Unit +foreign import writeFileImpl :: forall a opts. EffectFn4 FilePath a { | opts } (JSCallback Unit) Unit +foreign import appendFileImpl :: forall a opts. EffectFn4 FilePath a { | opts } (JSCallback Unit) Unit +foreign import openImpl :: EffectFn4 FilePath String (Nullable FileMode) (JSCallback FileDescriptor) Unit +foreign import readImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit +foreign import writeImpl :: EffectFn6 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) (JSCallback ByteCount) Unit +foreign import closeImpl :: EffectFn2 FileDescriptor (JSCallback Unit) Unit -- | Renames a file. @@ -99,7 +93,7 @@ rename :: FilePath -> FilePath -> Callback Unit -> Effect Unit -rename oldFile newFile cb = mkEffect $ \_ -> runFn3 +rename oldFile newFile cb = runEffectFn3 renameImpl oldFile newFile (handleCallback cb) -- | Truncates a file to the specified length. @@ -108,7 +102,7 @@ truncate :: FilePath -> Callback Unit -> Effect Unit -truncate file len cb = mkEffect $ \_ -> runFn3 +truncate file len cb = runEffectFn3 truncateImpl file len (handleCallback cb) -- | Changes the ownership of a file. @@ -118,7 +112,7 @@ chown :: FilePath -> Callback Unit -> Effect Unit -chown file uid gid cb = mkEffect $ \_ -> runFn4 +chown file uid gid cb = runEffectFn4 chownImpl file uid gid (handleCallback cb) -- | Changes the permissions of a file. @@ -127,7 +121,7 @@ chmod :: FilePath -> Callback Unit -> Effect Unit -chmod file perms cb = mkEffect $ \_ -> runFn3 +chmod file perms cb = runEffectFn3 chmodImpl file (permsToString perms) (handleCallback cb) -- | Gets file statistics. @@ -135,7 +129,7 @@ stat :: FilePath -> Callback Stats -> Effect Unit -stat file cb = mkEffect $ \_ -> runFn2 +stat file cb = runEffectFn2 statImpl file (handleCallback $ cb <<< map Stats) -- | Gets file or symlink statistics. `lstat` is identical to `stat`, except @@ -144,7 +138,7 @@ stat file cb = mkEffect $ \_ -> runFn2 lstat :: FilePath -> Callback Stats -> Effect Unit -lstat file cb = mkEffect $ \_ -> runFn2 +lstat file cb = runEffectFn2 lstatImpl file (handleCallback $ cb <<< map Stats) -- | Creates a link to an existing file. @@ -153,7 +147,7 @@ link :: FilePath -> Callback Unit -> Effect Unit -link src dst cb = mkEffect $ \_ -> runFn3 +link src dst cb = runEffectFn3 linkImpl src dst (handleCallback cb) -- | Creates a symlink. @@ -163,7 +157,7 @@ symlink :: FilePath -> Callback Unit -> Effect Unit -symlink src dest ty cb = mkEffect $ \_ -> runFn4 +symlink src dest ty cb = runEffectFn4 symlinkImpl src dest (symlinkTypeToNode ty) (handleCallback cb) -- | Reads the value of a symlink. @@ -171,7 +165,7 @@ readlink :: FilePath -> Callback FilePath -> Effect Unit -readlink path cb = mkEffect $ \_ -> runFn2 +readlink path cb = runEffectFn2 readlinkImpl path (handleCallback cb) -- | Find the canonicalized absolute location for a path. @@ -179,7 +173,7 @@ realpath :: FilePath -> Callback FilePath -> Effect Unit -realpath path cb = mkEffect $ \_ -> runFn3 +realpath path cb = runEffectFn3 realpathImpl path {} (handleCallback cb) -- | Find the canonicalized absolute location for a path using a cache object @@ -189,7 +183,7 @@ realpath' :: forall cache. FilePath -> Callback FilePath -> Effect Unit -realpath' path cache cb = mkEffect $ \_ -> runFn3 +realpath' path cache cb = runEffectFn3 realpathImpl path cache (handleCallback cb) -- | Deletes a file. @@ -197,7 +191,7 @@ unlink :: FilePath -> Callback Unit -> Effect Unit -unlink file cb = mkEffect $ \_ -> runFn2 +unlink file cb = runEffectFn2 unlinkImpl file (handleCallback cb) -- | Deletes a directory. @@ -211,7 +205,7 @@ rmdir' :: FilePath -> { maxRetries :: Int, retryDelay :: Int } -> Callback Unit -> Effect Unit -rmdir' path opts cb = mkEffect $ \_ -> runFn3 +rmdir' path opts cb = runEffectFn3 rmdirImpl path opts (handleCallback cb) -- | Deletes a file or directory. @@ -225,7 +219,7 @@ rm' :: FilePath -> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } -> Callback Unit -> Effect Unit -rm' path opts cb = mkEffect $ \_ -> runFn3 +rm' path opts cb = runEffectFn3 rmImpl path opts (handleCallback cb) @@ -241,7 +235,7 @@ mkdir' -> { recursive :: Boolean, mode :: Perms } -> Callback Unit -> Effect Unit -mkdir' file { recursive, mode: perms } cb = mkEffect $ \_ -> runFn3 +mkdir' file { recursive, mode: perms } cb = runEffectFn3 mkdirImpl file { recursive, mode: permsToString perms } (handleCallback cb) -- | Reads the contents of a directory. @@ -249,7 +243,7 @@ readdir :: FilePath -> Callback (Array FilePath) -> Effect Unit -readdir file cb = mkEffect $ \_ -> runFn2 +readdir file cb = runEffectFn2 readdirImpl file (handleCallback cb) -- | Sets the accessed and modified times for the specified file. @@ -259,7 +253,7 @@ utimes :: FilePath -> Callback Unit -> Effect Unit -utimes file atime mtime cb = mkEffect $ \_ -> runFn4 +utimes file atime mtime cb = runEffectFn4 utimesImpl file (fromDate atime) (fromDate mtime) @@ -274,7 +268,7 @@ readFile :: FilePath -> Callback Buffer -> Effect Unit -readFile file cb = mkEffect $ \_ -> runFn3 +readFile file cb = runEffectFn3 readFileImpl file {} (handleCallback cb) -- | Reads the entire contents of a text file with the specified encoding. @@ -283,7 +277,7 @@ readTextFile :: Encoding -> Callback String -> Effect Unit -readTextFile encoding file cb = mkEffect $ \_ -> runFn3 +readTextFile encoding file cb = runEffectFn3 readFileImpl file { encoding: show encoding } (handleCallback cb) -- | Writes a buffer to a file. @@ -292,7 +286,7 @@ writeFile :: FilePath -> Callback Unit -> Effect Unit -writeFile file buff cb = mkEffect $ \_ -> runFn4 +writeFile file buff cb = runEffectFn4 writeFileImpl file buff {} (handleCallback cb) -- | Writes text to a file using the specified encoding. @@ -302,7 +296,7 @@ writeTextFile :: Encoding -> Callback Unit -> Effect Unit -writeTextFile encoding file buff cb = mkEffect $ \_ -> runFn4 +writeTextFile encoding file buff cb = runEffectFn4 writeFileImpl file buff { encoding: show encoding } (handleCallback cb) -- | Appends the contents of a buffer to a file. @@ -311,7 +305,7 @@ appendFile :: FilePath -> Callback Unit -> Effect Unit -appendFile file buff cb = mkEffect $ \_ -> runFn4 +appendFile file buff cb = runEffectFn4 appendFileImpl file buff {} (handleCallback cb) -- | Appends text to a file using the specified encoding. @@ -321,7 +315,7 @@ appendTextFile :: Encoding -> Callback Unit -> Effect Unit -appendTextFile encoding file buff cb = mkEffect $ \_ -> runFn4 +appendTextFile encoding file buff cb = runEffectFn4 appendFileImpl file buff { encoding: show encoding } (handleCallback cb) @@ -332,7 +326,7 @@ fdOpen :: FilePath -> Maybe FileMode -> Callback FileDescriptor -> Effect Unit -fdOpen file flags mode cb = mkEffect $ \_ -> runFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb) +fdOpen file flags mode cb = runEffectFn4 openImpl file (fileFlagsToNode flags) (toNullable mode) (handleCallback cb) -- | 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. @@ -343,7 +337,7 @@ fdRead :: FileDescriptor -> Maybe FilePosition -> Callback ByteCount -> Effect Unit -fdRead fd buff off len pos cb = mkEffect $ \_ -> runFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb) +fdRead fd buff off len pos cb = runEffectFn6 readImpl fd buff off len (toNullable pos) (handleCallback cb) -- | Convenience function to fill the whole buffer from the current -- | file position. @@ -364,7 +358,7 @@ fdWrite :: FileDescriptor -> Maybe FilePosition -> Callback ByteCount -> Effect Unit -fdWrite fd buff off len pos cb = mkEffect $ \_ -> runFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb) +fdWrite fd buff off len pos cb = runEffectFn6 writeImpl fd buff off len (toNullable pos) (handleCallback cb) -- | Convenience function to append the whole buffer to the current -- | file position. @@ -381,4 +375,4 @@ fdAppend fd buff cb = do fdClose :: FileDescriptor -> Callback Unit -> Effect Unit -fdClose fd cb = mkEffect $ \_ -> runFn2 closeImpl fd (handleCallback cb) +fdClose fd cb = runEffectFn2 closeImpl fd (handleCallback cb) diff --git a/src/Node/FS/Internal.purs b/src/Node/FS/Internal.purs deleted file mode 100644 index ea21e6b..0000000 --- a/src/Node/FS/Internal.purs +++ /dev/null @@ -1,10 +0,0 @@ - -module Node.FS.Internal where - -import Prelude - -import Effect (Effect) -import Unsafe.Coerce (unsafeCoerce) - -mkEffect :: forall a. (Unit -> a) -> Effect a -mkEffect = unsafeCoerce diff --git a/src/Node/FS/Stream.purs b/src/Node/FS/Stream.purs index 2c503a0..f795445 100644 --- a/src/Node/FS/Stream.purs +++ b/src/Node/FS/Stream.purs @@ -14,20 +14,19 @@ module Node.FS.Stream ) where import Prelude + import Data.Maybe (Maybe(..)) -import Data.Function.Uncurried (Fn2, runFn2) import Data.Nullable (Nullable, toNullable) import Effect (Effect) -import Node.Stream (Readable(), Writable()) -import Node.Path (FilePath()) - +import Effect.Uncurried (EffectFn2, runEffectFn2) import Node.FS (FileDescriptor, FileFlags(..), fileFlagsToNode) -import Node.FS.Perms (Perms()) +import Node.FS.Perms (Perms) import Node.FS.Perms as Perms -import Node.FS.Internal (mkEffect) +import Node.Path (FilePath) +import Node.Stream (Readable, Writable) -foreign import createReadStreamImpl :: forall opts. Fn2 (Nullable FilePath) { | opts } (Readable ()) -foreign import createWriteStreamImpl :: forall opts. Fn2 (Nullable FilePath) { | opts } (Writable ()) +foreign import createReadStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Readable ()) +foreign import createWriteStreamImpl :: forall opts. EffectFn2 (Nullable FilePath) { | opts } (Writable ()) readWrite :: Perms readWrite = Perms.mkPerms rw rw rw @@ -67,7 +66,7 @@ defaultWriteStreamOptions = createWriteStreamWith :: WriteStreamOptions -> FilePath -> Effect (Writable ()) -createWriteStreamWith opts file = mkEffect $ \_ -> runFn2 +createWriteStreamWith opts file = runEffectFn2 createWriteStreamImpl (nonnull file) { mode: Perms.permsToInt opts.perms , flags: fileFlagsToNode opts.flags @@ -77,7 +76,7 @@ createWriteStreamWith opts file = mkEffect $ \_ -> runFn2 fdCreateWriteStreamWith :: WriteStreamOptions -> FileDescriptor -> Effect (Writable ()) -fdCreateWriteStreamWith opts fd = mkEffect $ \_ -> runFn2 +fdCreateWriteStreamWith opts fd = runEffectFn2 createWriteStreamImpl null { fd , mode: Perms.permsToInt opts.perms @@ -113,7 +112,7 @@ defaultReadStreamOptions = createReadStreamWith :: ReadStreamOptions -> FilePath -> Effect (Readable ()) -createReadStreamWith opts file = mkEffect $ \_ -> runFn2 +createReadStreamWith opts file = runEffectFn2 createReadStreamImpl (nonnull file) { mode: Perms.permsToInt opts.perms , flags: fileFlagsToNode opts.flags @@ -124,7 +123,7 @@ createReadStreamWith opts file = mkEffect $ \_ -> runFn2 fdCreateReadStreamWith :: ReadStreamOptions -> FileDescriptor -> Effect (Readable ()) -fdCreateReadStreamWith opts fd = mkEffect $ \_ -> runFn2 +fdCreateReadStreamWith opts fd = runEffectFn2 createReadStreamImpl null { fd , mode: Perms.permsToInt opts.perms diff --git a/src/Node/FS/Sync.purs b/src/Node/FS/Sync.purs index 7594495..c89c2ec 100644 --- a/src/Node/FS/Sync.purs +++ b/src/Node/FS/Sync.purs @@ -40,11 +40,10 @@ import Effect (Effect) import Data.DateTime (DateTime) import Data.Time.Duration (Milliseconds(..)) import Data.DateTime.Instant (fromDateTime, unInstant) -import Data.Function.Uncurried (Fn1, Fn5, Fn3, Fn2, - runFn1, runFn5, runFn3, runFn2) import Data.Nullable (Nullable(), toNullable) import Data.Int (round) import Data.Maybe (Maybe(..)) +import Effect.Uncurried (EffectFn1, EffectFn2, EffectFn3, EffectFn5, runEffectFn1, runEffectFn2, runEffectFn3, runEffectFn5) import Node.Buffer (Buffer(), size) import Node.Encoding (Encoding) @@ -54,40 +53,39 @@ import Node.FS (FileDescriptor, ByteCount, FilePosition, BufferLength, import Node.FS.Stats (StatsObj, Stats(..)) import Node.Path (FilePath()) import Node.FS.Perms (Perms, permsToString, all, mkPerms) -import Node.FS.Internal (mkEffect) - -foreign import renameSyncImpl :: Fn2 FilePath FilePath Unit -foreign import truncateSyncImpl :: Fn2 FilePath Int Unit -foreign import chownSyncImpl :: Fn3 FilePath Int Int Unit -foreign import chmodSyncImpl :: Fn2 FilePath String Unit -foreign import statSyncImpl :: Fn1 FilePath StatsObj -foreign import lstatSyncImpl :: Fn1 FilePath StatsObj -foreign import linkSyncImpl :: Fn2 FilePath FilePath Unit -foreign import symlinkSyncImpl :: Fn3 FilePath FilePath String Unit -foreign import readlinkSyncImpl :: Fn1 FilePath FilePath -foreign import realpathSyncImpl :: forall cache. Fn2 FilePath { | cache } FilePath -foreign import unlinkSyncImpl :: Fn1 FilePath Unit -foreign import rmdirSyncImpl :: Fn2 FilePath { maxRetries :: Int, retryDelay :: Int } Unit -foreign import rmSyncImpl :: Fn2 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } Unit -foreign import mkdirSyncImpl :: Fn2 FilePath { recursive :: Boolean, mode :: String } Unit -foreign import readdirSyncImpl :: Fn1 FilePath (Array FilePath) -foreign import utimesSyncImpl :: Fn3 FilePath Int Int Unit -foreign import readFileSyncImpl :: forall a opts. Fn2 FilePath { | opts } a -foreign import writeFileSyncImpl :: forall a opts. Fn3 FilePath a { | opts } Unit -foreign import appendFileSyncImpl :: forall a opts. Fn3 FilePath a { | opts } Unit -foreign import existsSyncImpl :: FilePath -> Boolean -foreign import openSyncImpl :: Fn3 FilePath String (Nullable FileMode) FileDescriptor -foreign import readSyncImpl :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount -foreign import writeSyncImpl :: Fn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount -foreign import fsyncSyncImpl :: Fn1 FileDescriptor Unit -foreign import closeSyncImpl :: Fn1 FileDescriptor Unit + +foreign import renameSyncImpl :: EffectFn2 FilePath FilePath Unit +foreign import truncateSyncImpl :: EffectFn2 FilePath Int Unit +foreign import chownSyncImpl :: EffectFn3 FilePath Int Int Unit +foreign import chmodSyncImpl :: EffectFn2 FilePath String Unit +foreign import statSyncImpl :: EffectFn1 FilePath StatsObj +foreign import lstatSyncImpl :: EffectFn1 FilePath StatsObj +foreign import linkSyncImpl :: EffectFn2 FilePath FilePath Unit +foreign import symlinkSyncImpl :: EffectFn3 FilePath FilePath String Unit +foreign import readlinkSyncImpl :: EffectFn1 FilePath FilePath +foreign import realpathSyncImpl :: forall cache. EffectFn2 FilePath { | cache } FilePath +foreign import unlinkSyncImpl :: EffectFn1 FilePath Unit +foreign import rmdirSyncImpl :: EffectFn2 FilePath { maxRetries :: Int, retryDelay :: Int } Unit +foreign import rmSyncImpl :: EffectFn2 FilePath { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } Unit +foreign import mkdirSyncImpl :: EffectFn2 FilePath { recursive :: Boolean, mode :: String } Unit +foreign import readdirSyncImpl :: EffectFn1 FilePath (Array FilePath) +foreign import utimesSyncImpl :: EffectFn3 FilePath Int Int Unit +foreign import readFileSyncImpl :: forall a opts. EffectFn2 FilePath { | opts } a +foreign import writeFileSyncImpl :: forall a opts. EffectFn3 FilePath a { | opts } Unit +foreign import appendFileSyncImpl :: forall a opts. EffectFn3 FilePath a { | opts } Unit +foreign import existsSyncImpl :: EffectFn1 FilePath Boolean +foreign import openSyncImpl :: EffectFn3 FilePath String (Nullable FileMode) FileDescriptor +foreign import readSyncImpl :: EffectFn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount +foreign import writeSyncImpl :: EffectFn5 FileDescriptor Buffer BufferOffset BufferLength (Nullable FilePosition) ByteCount +foreign import fsyncSyncImpl :: EffectFn1 FileDescriptor Unit +foreign import closeSyncImpl :: EffectFn1 FileDescriptor Unit -- | Renames a file. rename :: FilePath -> FilePath -> Effect Unit -rename oldFile newFile = mkEffect $ \_ -> runFn2 +rename oldFile newFile = runEffectFn2 renameSyncImpl oldFile newFile -- | Truncates a file to the specified length. @@ -95,7 +93,7 @@ truncate :: FilePath -> Int -> Effect Unit -truncate file len = mkEffect $ \_ -> runFn2 +truncate file len = runEffectFn2 truncateSyncImpl file len -- | Changes the ownership of a file. @@ -104,7 +102,7 @@ chown :: FilePath -> Int -> Effect Unit -chown file uid gid = mkEffect $ \_ -> runFn3 +chown file uid gid = runEffectFn3 chownSyncImpl file uid gid -- | Changes the permissions of a file. @@ -112,14 +110,14 @@ chmod :: FilePath -> Perms -> Effect Unit -chmod file perms = mkEffect $ \_ -> runFn2 +chmod file perms = runEffectFn2 chmodSyncImpl file (permsToString perms) -- | Gets file statistics. stat :: FilePath -> Effect Stats -stat file = map Stats $ mkEffect $ \_ -> runFn1 +stat file = map Stats $ runEffectFn1 statSyncImpl file -- | Gets file or symlink statistics. `lstat` is identical to `stat`, except @@ -128,7 +126,7 @@ stat file = map Stats $ mkEffect $ \_ -> runFn1 lstat :: FilePath -> Effect Stats -lstat file = map Stats $ mkEffect $ \_ -> runFn1 +lstat file = map Stats $ runEffectFn1 lstatSyncImpl file -- | Creates a link to an existing file. @@ -136,7 +134,7 @@ link :: FilePath -> FilePath -> Effect Unit -link src dst = mkEffect $ \_ -> runFn2 +link src dst = runEffectFn2 linkSyncImpl src dst -- | Creates a symlink. @@ -145,21 +143,21 @@ symlink :: FilePath -> SymlinkType -> Effect Unit -symlink src dst ty = mkEffect $ \_ -> runFn3 +symlink src dst ty = runEffectFn3 symlinkSyncImpl src dst (symlinkTypeToNode ty) -- | Reads the value of a symlink. readlink :: FilePath -> Effect FilePath -readlink path = mkEffect $ \_ -> runFn1 +readlink path = runEffectFn1 readlinkSyncImpl path -- | Find the canonicalized absolute location for a path. realpath :: FilePath -> Effect FilePath -realpath path = mkEffect $ \_ -> runFn2 +realpath path = runEffectFn2 realpathSyncImpl path {} -- | Find the canonicalized absolute location for a path using a cache object for @@ -168,14 +166,14 @@ realpath' :: forall cache. FilePath -> { | cache } -> Effect FilePath -realpath' path cache = mkEffect $ \_ -> runFn2 +realpath' path cache = runEffectFn2 realpathSyncImpl path cache -- | Deletes a file. unlink :: FilePath -> Effect Unit -unlink file = mkEffect $ \_ -> runFn1 +unlink file = runEffectFn1 unlinkSyncImpl file -- | Deletes a directory. @@ -187,7 +185,7 @@ rmdir path = rmdir' path { maxRetries: 0, retryDelay: 100 } rmdir' :: FilePath -> { maxRetries :: Int, retryDelay :: Int } -> Effect Unit -rmdir' path opts = mkEffect $ \_ -> runFn2 +rmdir' path opts = runEffectFn2 rmdirSyncImpl path opts -- | Deletes a file or directory. @@ -199,7 +197,7 @@ rm path = rm' path { force: false, maxRetries: 100, recursive: false, retryDelay rm' :: FilePath -> { force :: Boolean, maxRetries :: Int, recursive :: Boolean, retryDelay :: Int } -> Effect Unit -rm' path opts = mkEffect $ \_ -> runFn2 +rm' path opts = runEffectFn2 rmSyncImpl path opts @@ -213,14 +211,14 @@ mkdir' :: FilePath -> { recursive :: Boolean, mode :: Perms } -> Effect Unit -mkdir' file { recursive, mode: perms } = mkEffect $ \_ -> runFn2 +mkdir' file { recursive, mode: perms } = runEffectFn2 mkdirSyncImpl file { recursive, mode: permsToString perms } -- | Reads the contents of a directory. readdir :: FilePath -> Effect (Array FilePath) -readdir file = mkEffect $ \_ -> runFn1 +readdir file = runEffectFn1 readdirSyncImpl file -- | Sets the accessed and modified times for the specified file. @@ -229,7 +227,7 @@ utimes :: FilePath -> DateTime -> Effect Unit -utimes file atime mtime = mkEffect $ \_ -> runFn3 +utimes file atime mtime = runEffectFn3 utimesSyncImpl file (fromDate atime) (fromDate mtime) @@ -242,7 +240,7 @@ utimes file atime mtime = mkEffect $ \_ -> runFn3 readFile :: FilePath -> Effect Buffer -readFile file = mkEffect $ \_ -> runFn2 +readFile file = runEffectFn2 readFileSyncImpl file {} -- | Reads the entire contents of a text file with the specified encoding. @@ -250,7 +248,7 @@ readTextFile :: Encoding -> FilePath -> Effect String -readTextFile encoding file = mkEffect $ \_ -> runFn2 +readTextFile encoding file = runEffectFn2 readFileSyncImpl file { encoding: show encoding } -- | Writes a buffer to a file. @@ -258,7 +256,7 @@ writeFile :: FilePath -> Buffer -> Effect Unit -writeFile file buff = mkEffect $ \_ -> runFn3 +writeFile file buff = runEffectFn3 writeFileSyncImpl file buff {} -- | Writes text to a file using the specified encoding. @@ -267,7 +265,7 @@ writeTextFile :: Encoding -> String -> Effect Unit -writeTextFile encoding file text = mkEffect $ \_ -> runFn3 +writeTextFile encoding file text = runEffectFn3 writeFileSyncImpl file text { encoding: show encoding } -- | Appends the contents of a buffer to a file. @@ -275,7 +273,7 @@ appendFile :: FilePath -> Buffer -> Effect Unit -appendFile file buff = mkEffect $ \_ -> runFn3 +appendFile file buff = runEffectFn3 appendFileSyncImpl file buff {} -- | Appends text to a file using the specified encoding. @@ -284,13 +282,13 @@ appendTextFile :: Encoding -> String -> Effect Unit -appendTextFile encoding file buff = mkEffect $ \_ -> runFn3 +appendTextFile encoding file buff = runEffectFn3 appendFileSyncImpl file buff { encoding: show encoding } -- | Check if the path exists. exists :: FilePath -> Effect Boolean -exists file = mkEffect $ \_ -> existsSyncImpl file +exists file = runEffectFn1 existsSyncImpl file -- | Open a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_opensync_path_flags_mode) -- | for details. @@ -298,8 +296,8 @@ fdOpen :: FilePath -> FileFlags -> Maybe FileMode -> Effect FileDescriptor -fdOpen file flags mode = mkEffect $ \_ -> - runFn3 openSyncImpl file (fileFlagsToNode flags) (toNullable mode) +fdOpen file flags mode = runEffectFn3 + openSyncImpl file (fileFlagsToNode flags) (toNullable mode) -- | Read from a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_readsync_fd_buffer_offset_length_position) -- | for details. @@ -310,7 +308,7 @@ fdRead :: FileDescriptor -> Maybe FilePosition -> Effect ByteCount fdRead fd buff off len pos = - mkEffect $ \_ -> runFn5 readSyncImpl fd buff off len (toNullable pos) + runEffectFn5 readSyncImpl fd buff off len (toNullable pos) -- | Convenience function to fill the whole buffer from the current -- | file position. @@ -330,7 +328,7 @@ fdWrite :: FileDescriptor -> Maybe FilePosition -> Effect ByteCount fdWrite fd buff off len pos = - mkEffect $ \_ -> runFn5 writeSyncImpl fd buff off len (toNullable pos) + runEffectFn5 writeSyncImpl fd buff off len (toNullable pos) -- | Convenience function to append the whole buffer to the current -- | file position. @@ -345,10 +343,10 @@ fdAppend fd buff = do -- | for details. fdFlush :: FileDescriptor -> Effect Unit -fdFlush fd = mkEffect $ \_ -> runFn1 fsyncSyncImpl fd +fdFlush fd = runEffectFn1 fsyncSyncImpl fd -- | Close a file synchronously. See the [Node documentation](http://nodejs.org/api/fs.html#fs_fs_closesync_fd) -- | for details. fdClose :: FileDescriptor -> Effect Unit -fdClose fd = mkEffect $ \_ -> runFn1 closeSyncImpl fd +fdClose fd = runEffectFn1 closeSyncImpl fd