Skip to content

Async fixes, add async exists #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 10, 2014
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

chown :: forall eff. FilePath -> Number -> Number -> Callback eff Unit -> Eff (fs :: FS | eff) Unit

exists :: forall eff. FilePath -> (Boolean -> Eff eff Unit) -> Eff (fs :: FS | eff) Unit

link :: forall eff. FilePath -> FilePath -> Callback eff Unit -> Eff (fs :: FS | eff) Unit

mkdir :: forall eff. FilePath -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
Expand Down
3 changes: 3 additions & 0 deletions examples/Test.purs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import Node.Encoding

main = do

A.exists "examples\\Test.purs" $ \e ->
trace $ "Test.purs exists? " ++ show e

file <- S.readTextFile UTF8 "examples\\Test.purs"
trace "\n\nreadTextFile sync result:"
trace $ file
Expand Down
59 changes: 38 additions & 21 deletions src/Node/FS/Async.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ module Node.FS.Async
, writeTextFile
, appendFile
, appendTextFile
, exists
) where

import Control.Monad.Eff
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
import Control.Monad.Eff.Exception
import Data.Date
import Data.Either
Expand Down Expand Up @@ -72,7 +74,13 @@ foreign import fs "var fs = require('fs');" ::
, readFile :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit
, writeFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
, appendFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
, exists :: forall a. Fn2 FilePath (Boolean -> a) Unit
}

foreign import mkEff
"function mkEff(action) {\
\ return action;\
\}" :: forall eff a. (Unit -> a) -> Eff eff a

-- |
-- Type synonym for callback functions.
Expand All @@ -87,7 +95,7 @@ rename :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

rename oldFile newFile cb = return $ runFn3
rename oldFile newFile cb = mkEff $ \_ -> runFn3
fs.rename oldFile newFile (handleCallback cb)

-- |
Expand All @@ -98,7 +106,7 @@ truncate :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

truncate file len cb = return $ runFn3
truncate file len cb = mkEff $ \_ -> runFn3
fs.truncate file len (handleCallback cb)

-- |
Expand All @@ -110,7 +118,7 @@ chown :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

chown file uid gid cb = return $ runFn4
chown file uid gid cb = mkEff $ \_ -> runFn4
fs.chown file uid gid (handleCallback cb)

-- |
Expand All @@ -121,7 +129,7 @@ chmod :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

chmod file mode cb = return $ runFn3
chmod file mode cb = mkEff $ \_ -> runFn3
fs.chmod file mode (handleCallback cb)

-- |
Expand All @@ -131,7 +139,7 @@ stat :: forall eff. FilePath
-> Callback eff Stats
-> Eff (fs :: FS | eff) Unit

stat file cb = return $ runFn2
stat file cb = mkEff $ \_ -> runFn2
fs.stat file (handleCallback $ cb <<< (<$>) Stats)

-- |
Expand All @@ -142,7 +150,7 @@ link :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

link src dst cb = return $ runFn3
link src dst cb = mkEff $ \_ -> runFn3
fs.link src dst (handleCallback cb)

-- |
Expand All @@ -154,7 +162,7 @@ symlink :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

symlink src dest ty cb = return $ runFn4
symlink src dest ty cb = mkEff $ \_ -> runFn4
fs.symlink src dest (show ty) (handleCallback cb)

-- |
Expand All @@ -164,7 +172,7 @@ readlink :: forall eff. FilePath
-> Callback eff FilePath
-> Eff (fs :: FS | eff) Unit

readlink path cb = return $ runFn2
readlink path cb = mkEff $ \_ -> runFn2
fs.readlink path (handleCallback cb)

-- |
Expand All @@ -174,7 +182,7 @@ realpath :: forall eff. FilePath
-> Callback eff FilePath
-> Eff (fs :: FS | eff) Unit

realpath path cb = return $ runFn3
realpath path cb = mkEff $ \_ -> runFn3
fs.realpath path {} (handleCallback cb)

-- |
Expand All @@ -186,7 +194,7 @@ realpath' :: forall eff cache. FilePath
-> Callback eff FilePath
-> Eff (fs :: FS | eff) Unit

realpath' path cache cb = return $ runFn3
realpath' path cache cb = mkEff $ \_ -> runFn3
fs.realpath path cache (handleCallback cb)

-- |
Expand All @@ -196,7 +204,7 @@ unlink :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

unlink file cb = return $ runFn2
unlink file cb = mkEff $ \_ -> runFn2
fs.unlink file (handleCallback cb)

-- |
Expand All @@ -206,7 +214,7 @@ rmdir :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

rmdir file cb = return $ runFn2
rmdir file cb = mkEff $ \_ -> runFn2
fs.rmdir file (handleCallback cb)

-- |
Expand All @@ -226,7 +234,7 @@ mkdir' :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

mkdir' file mode cb = return $ runFn3
mkdir' file mode cb = mkEff $ \_ -> runFn3
fs.mkdir file mode (handleCallback cb)

-- |
Expand All @@ -236,7 +244,7 @@ readdir :: forall eff. FilePath
-> Callback eff [FilePath]
-> Eff (fs :: FS | eff) Unit

readdir file cb = return $ runFn2
readdir file cb = mkEff $ \_ -> runFn2
fs.readdir file (handleCallback cb)

-- |
Expand All @@ -248,7 +256,7 @@ utimes :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

utimes file atime mtime cb = return $ runFn4
utimes file atime mtime cb = mkEff $ \_ -> runFn4
fs.utimes file
((toEpochMilliseconds atime) / 1000)
((toEpochMilliseconds mtime) / 1000)
Expand All @@ -261,7 +269,7 @@ readFile :: forall eff. FilePath
-> Callback eff Buffer
-> Eff (fs :: FS | eff) Unit

readFile file cb = return $ runFn3
readFile file cb = mkEff $ \_ -> runFn3
fs.readFile file {} (handleCallback cb)

-- |
Expand All @@ -272,7 +280,7 @@ readTextFile :: forall eff. Encoding
-> Callback eff String
-> Eff (fs :: FS | eff) Unit

readTextFile encoding file cb = return $ runFn3
readTextFile encoding file cb = mkEff $ \_ -> runFn3
fs.readFile file { encoding: show encoding } (handleCallback cb)

-- |
Expand All @@ -283,7 +291,7 @@ writeFile :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

writeFile file buff cb = return $ runFn4
writeFile file buff cb = mkEff $ \_ -> runFn4
fs.writeFile file buff {} (handleCallback cb)

-- |
Expand All @@ -295,7 +303,7 @@ writeTextFile :: forall eff. Encoding
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

writeTextFile encoding file buff cb = return $ runFn4
writeTextFile encoding file buff cb = mkEff $ \_ -> runFn4
fs.writeFile file buff { encoding: show encoding } (handleCallback cb)

-- |
Expand All @@ -306,7 +314,7 @@ appendFile :: forall eff. FilePath
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

appendFile file buff cb = return $ runFn4
appendFile file buff cb = mkEff $ \_ -> runFn4
fs.appendFile file buff {} (handleCallback cb)

-- |
Expand All @@ -318,5 +326,14 @@ appendTextFile :: forall eff. Encoding
-> Callback eff Unit
-> Eff (fs :: FS | eff) Unit

appendTextFile encoding file buff cb = return $ runFn4
appendTextFile encoding file buff cb = mkEff $ \_ -> runFn4
fs.appendFile file buff { encoding: show encoding } (handleCallback cb)

-- |
-- Check if the path exists.
--
exists :: forall eff. FilePath
-> (Boolean -> Eff eff Unit)
-> Eff (fs :: FS | eff) Unit
exists file cb = mkEff $ \_ -> runFn2
fs.exists file $ \b -> runPure (unsafeInterleaveEff (cb b))
5 changes: 2 additions & 3 deletions src/Node/FS/Sync.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ foreign import fs "var fs = require('fs');" ::
, readFileSync :: forall a opts. Fn2 FilePath { | opts } a
, writeFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
, appendFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
, existsSync :: Fn1 FilePath Boolean
, existsSync :: FilePath -> Boolean
}

foreign import mkEff
Expand Down Expand Up @@ -286,5 +286,4 @@ appendTextFile encoding file buff = mkEff $ \_ -> runFn3
--
exists :: forall eff. FilePath
-> Eff (fs :: FS | eff) Boolean
exists file = mkEff $ \_ -> runFn1
fs.existsSync file
exists file = mkEff $ \_ -> fs.existsSync file