From c194f84cab66fa6e18b78c32f9cdf2bddf8d1e68 Mon Sep 17 00:00:00 2001 From: eric thul Date: Wed, 8 Apr 2015 19:49:24 -0400 Subject: Rewrite using purescript for the implementation --- src/FS.purs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/FS.purs (limited to 'src/FS.purs') diff --git a/src/FS.purs b/src/FS.purs new file mode 100644 index 0000000..68fe2f9 --- /dev/null +++ b/src/FS.purs @@ -0,0 +1,45 @@ +module PursLoader.FS + ( FS() + , readFileUtf8 + , readFileUtf8Sync + ) where + +import Control.Monad.Aff (Aff(), makeAff) +import Control.Monad.Eff (Eff()) +import Control.Monad.Eff.Exception (Error()) + +import Data.Function + +foreign import data FS :: ! + +readFileUtf8 :: forall eff. String -> Aff (fs :: FS | eff) String +readFileUtf8 filepath = makeAff $ runFn3 readFileUtf8Fn filepath + +readFileUtf8Sync :: forall eff. String -> Eff (fs :: FS | eff) String +readFileUtf8Sync filepath = readFileUtf8SyncFn filepath + +foreign import readFileUtf8Fn """ +function readFileUtf8Fn(filepath, errback, callback) { + return function(){ + var fs = require('fs'); + + fs.readFile(filepath, 'utf-8', function(e, data){ + if (e) errback(e)(); + else callback(data)(); + }); + }; +} +""" :: forall eff. Fn3 String + (Error -> Eff (fs :: FS | eff) Unit) + (String -> Eff (fs :: FS | eff) Unit) + (Eff (fs :: FS | eff) Unit) + +foreign import readFileUtf8SyncFn """ +function readFileUtf8SyncFn(filepath) { + return function(){ + var fs = require('fs'); + + return fs.readFileSync(filepath, {encoding: 'utf-8'}); + }; +} +""" :: forall eff. String -> (Eff (fs :: FS | eff) String) -- cgit v1.2.3