Skip to content

Commit 2f50d11

Browse files
committed
Merge pull request #7 from purescript-contrib/async-fixes
Async fixes, add async `exists`
2 parents aa0f4a6 + 76cafc1 commit 2f50d11

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

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

39+
exists :: forall eff. FilePath -> (Boolean -> Eff eff Unit) -> Eff (fs :: FS | eff) Unit
40+
3941
link :: forall eff. FilePath -> FilePath -> Callback eff Unit -> Eff (fs :: FS | eff) Unit
4042

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

examples/Test.purs

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import Node.Encoding
1111

1212
main = do
1313

14+
A.exists "examples\\Test.purs" $ \e ->
15+
trace $ "Test.purs exists? " ++ show e
16+
1417
file <- S.readTextFile UTF8 "examples\\Test.purs"
1518
trace "\n\nreadTextFile sync result:"
1619
trace $ file

src/Node/FS/Async.purs

+38-21
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ module Node.FS.Async
2222
, writeTextFile
2323
, appendFile
2424
, appendTextFile
25+
, exists
2526
) where
2627

2728
import Control.Monad.Eff
29+
import Control.Monad.Eff.Unsafe (unsafeInterleaveEff)
2830
import Control.Monad.Eff.Exception
2931
import Data.Date
3032
import Data.Either
@@ -72,7 +74,13 @@ foreign import fs "var fs = require('fs');" ::
7274
, readFile :: forall a opts. Fn3 FilePath { | opts } (JSCallback a) Unit
7375
, writeFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
7476
, appendFile :: forall a opts. Fn4 FilePath a { | opts } (JSCallback Unit) Unit
77+
, exists :: forall a. Fn2 FilePath (Boolean -> a) Unit
7578
}
79+
80+
foreign import mkEff
81+
"function mkEff(action) {\
82+
\ return action;\
83+
\}" :: forall eff a. (Unit -> a) -> Eff eff a
7684

7785
-- |
7886
-- Type synonym for callback functions.
@@ -87,7 +95,7 @@ rename :: forall eff. FilePath
8795
-> Callback eff Unit
8896
-> Eff (fs :: FS | eff) Unit
8997

90-
rename oldFile newFile cb = return $ runFn3
98+
rename oldFile newFile cb = mkEff $ \_ -> runFn3
9199
fs.rename oldFile newFile (handleCallback cb)
92100

93101
-- |
@@ -98,7 +106,7 @@ truncate :: forall eff. FilePath
98106
-> Callback eff Unit
99107
-> Eff (fs :: FS | eff) Unit
100108

101-
truncate file len cb = return $ runFn3
109+
truncate file len cb = mkEff $ \_ -> runFn3
102110
fs.truncate file len (handleCallback cb)
103111

104112
-- |
@@ -110,7 +118,7 @@ chown :: forall eff. FilePath
110118
-> Callback eff Unit
111119
-> Eff (fs :: FS | eff) Unit
112120

113-
chown file uid gid cb = return $ runFn4
121+
chown file uid gid cb = mkEff $ \_ -> runFn4
114122
fs.chown file uid gid (handleCallback cb)
115123

116124
-- |
@@ -121,7 +129,7 @@ chmod :: forall eff. FilePath
121129
-> Callback eff Unit
122130
-> Eff (fs :: FS | eff) Unit
123131

124-
chmod file mode cb = return $ runFn3
132+
chmod file mode cb = mkEff $ \_ -> runFn3
125133
fs.chmod file mode (handleCallback cb)
126134

127135
-- |
@@ -131,7 +139,7 @@ stat :: forall eff. FilePath
131139
-> Callback eff Stats
132140
-> Eff (fs :: FS | eff) Unit
133141

134-
stat file cb = return $ runFn2
142+
stat file cb = mkEff $ \_ -> runFn2
135143
fs.stat file (handleCallback $ cb <<< (<$>) Stats)
136144

137145
-- |
@@ -142,7 +150,7 @@ link :: forall eff. FilePath
142150
-> Callback eff Unit
143151
-> Eff (fs :: FS | eff) Unit
144152

145-
link src dst cb = return $ runFn3
153+
link src dst cb = mkEff $ \_ -> runFn3
146154
fs.link src dst (handleCallback cb)
147155

148156
-- |
@@ -154,7 +162,7 @@ symlink :: forall eff. FilePath
154162
-> Callback eff Unit
155163
-> Eff (fs :: FS | eff) Unit
156164

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

160168
-- |
@@ -164,7 +172,7 @@ readlink :: forall eff. FilePath
164172
-> Callback eff FilePath
165173
-> Eff (fs :: FS | eff) Unit
166174

167-
readlink path cb = return $ runFn2
175+
readlink path cb = mkEff $ \_ -> runFn2
168176
fs.readlink path (handleCallback cb)
169177

170178
-- |
@@ -174,7 +182,7 @@ realpath :: forall eff. FilePath
174182
-> Callback eff FilePath
175183
-> Eff (fs :: FS | eff) Unit
176184

177-
realpath path cb = return $ runFn3
185+
realpath path cb = mkEff $ \_ -> runFn3
178186
fs.realpath path {} (handleCallback cb)
179187

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

189-
realpath' path cache cb = return $ runFn3
197+
realpath' path cache cb = mkEff $ \_ -> runFn3
190198
fs.realpath path cache (handleCallback cb)
191199

192200
-- |
@@ -196,7 +204,7 @@ unlink :: forall eff. FilePath
196204
-> Callback eff Unit
197205
-> Eff (fs :: FS | eff) Unit
198206

199-
unlink file cb = return $ runFn2
207+
unlink file cb = mkEff $ \_ -> runFn2
200208
fs.unlink file (handleCallback cb)
201209

202210
-- |
@@ -206,7 +214,7 @@ rmdir :: forall eff. FilePath
206214
-> Callback eff Unit
207215
-> Eff (fs :: FS | eff) Unit
208216

209-
rmdir file cb = return $ runFn2
217+
rmdir file cb = mkEff $ \_ -> runFn2
210218
fs.rmdir file (handleCallback cb)
211219

212220
-- |
@@ -226,7 +234,7 @@ mkdir' :: forall eff. FilePath
226234
-> Callback eff Unit
227235
-> Eff (fs :: FS | eff) Unit
228236

229-
mkdir' file mode cb = return $ runFn3
237+
mkdir' file mode cb = mkEff $ \_ -> runFn3
230238
fs.mkdir file mode (handleCallback cb)
231239

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

239-
readdir file cb = return $ runFn2
247+
readdir file cb = mkEff $ \_ -> runFn2
240248
fs.readdir file (handleCallback cb)
241249

242250
-- |
@@ -248,7 +256,7 @@ utimes :: forall eff. FilePath
248256
-> Callback eff Unit
249257
-> Eff (fs :: FS | eff) Unit
250258

251-
utimes file atime mtime cb = return $ runFn4
259+
utimes file atime mtime cb = mkEff $ \_ -> runFn4
252260
fs.utimes file
253261
((toEpochMilliseconds atime) / 1000)
254262
((toEpochMilliseconds mtime) / 1000)
@@ -261,7 +269,7 @@ readFile :: forall eff. FilePath
261269
-> Callback eff Buffer
262270
-> Eff (fs :: FS | eff) Unit
263271

264-
readFile file cb = return $ runFn3
272+
readFile file cb = mkEff $ \_ -> runFn3
265273
fs.readFile file {} (handleCallback cb)
266274

267275
-- |
@@ -272,7 +280,7 @@ readTextFile :: forall eff. Encoding
272280
-> Callback eff String
273281
-> Eff (fs :: FS | eff) Unit
274282

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

278286
-- |
@@ -283,7 +291,7 @@ writeFile :: forall eff. FilePath
283291
-> Callback eff Unit
284292
-> Eff (fs :: FS | eff) Unit
285293

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

289297
-- |
@@ -295,7 +303,7 @@ writeTextFile :: forall eff. Encoding
295303
-> Callback eff Unit
296304
-> Eff (fs :: FS | eff) Unit
297305

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

301309
-- |
@@ -306,7 +314,7 @@ appendFile :: forall eff. FilePath
306314
-> Callback eff Unit
307315
-> Eff (fs :: FS | eff) Unit
308316

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

312320
-- |
@@ -318,5 +326,14 @@ appendTextFile :: forall eff. Encoding
318326
-> Callback eff Unit
319327
-> Eff (fs :: FS | eff) Unit
320328

321-
appendTextFile encoding file buff cb = return $ runFn4
329+
appendTextFile encoding file buff cb = mkEff $ \_ -> runFn4
322330
fs.appendFile file buff { encoding: show encoding } (handleCallback cb)
331+
332+
-- |
333+
-- Check if the path exists.
334+
--
335+
exists :: forall eff. FilePath
336+
-> (Boolean -> Eff eff Unit)
337+
-> Eff (fs :: FS | eff) Unit
338+
exists file cb = mkEff $ \_ -> runFn2
339+
fs.exists file $ \b -> runPure (unsafeInterleaveEff (cb b))

src/Node/FS/Sync.purs

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ foreign import fs "var fs = require('fs');" ::
5353
, readFileSync :: forall a opts. Fn2 FilePath { | opts } a
5454
, writeFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
5555
, appendFileSync :: forall a opts. Fn3 FilePath a { | opts } Unit
56-
, existsSync :: Fn1 FilePath Boolean
56+
, existsSync :: FilePath -> Boolean
5757
}
5858

5959
foreign import mkEff
@@ -286,5 +286,4 @@ appendTextFile encoding file buff = mkEff $ \_ -> runFn3
286286
--
287287
exists :: forall eff. FilePath
288288
-> Eff (fs :: FS | eff) Boolean
289-
exists file = mkEff $ \_ -> runFn1
290-
fs.existsSync file
289+
exists file = mkEff $ \_ -> fs.existsSync file

0 commit comments

Comments
 (0)