diff options
author | eric <thul.eric@gmail.com> | 2015-04-12 11:35:25 -0400 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2015-04-12 11:35:25 -0400 |
commit | 7d58cf78c770ef95858f90c6e89bd18653657a82 (patch) | |
tree | ebae7549b1a1b14e11fd5e2ad4dd66d901af81c8 /src/FS.purs | |
parent | 18ced1e0f031df444a5d85d72cd6843d826fef38 (diff) | |
parent | 94a23e744896b4440794de5d6cffedff1a1a2d56 (diff) | |
download | purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.tar.gz purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.tar.zst purs-loader-7d58cf78c770ef95858f90c6e89bd18653657a82.zip |
Merge pull request #8 from ethul/topic/purescript-rewrite
Topic/purescript rewrite
Diffstat (limited to 'src/FS.purs')
-rw-r--r-- | src/FS.purs | 45 |
1 files changed, 45 insertions, 0 deletions
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 @@ | |||
1 | module PursLoader.FS | ||
2 | ( FS() | ||
3 | , readFileUtf8 | ||
4 | , readFileUtf8Sync | ||
5 | ) where | ||
6 | |||
7 | import Control.Monad.Aff (Aff(), makeAff) | ||
8 | import Control.Monad.Eff (Eff()) | ||
9 | import Control.Monad.Eff.Exception (Error()) | ||
10 | |||
11 | import Data.Function | ||
12 | |||
13 | foreign import data FS :: ! | ||
14 | |||
15 | readFileUtf8 :: forall eff. String -> Aff (fs :: FS | eff) String | ||
16 | readFileUtf8 filepath = makeAff $ runFn3 readFileUtf8Fn filepath | ||
17 | |||
18 | readFileUtf8Sync :: forall eff. String -> Eff (fs :: FS | eff) String | ||
19 | readFileUtf8Sync filepath = readFileUtf8SyncFn filepath | ||
20 | |||
21 | foreign import readFileUtf8Fn """ | ||
22 | function readFileUtf8Fn(filepath, errback, callback) { | ||
23 | return function(){ | ||
24 | var fs = require('fs'); | ||
25 | |||
26 | fs.readFile(filepath, 'utf-8', function(e, data){ | ||
27 | if (e) errback(e)(); | ||
28 | else callback(data)(); | ||
29 | }); | ||
30 | }; | ||
31 | } | ||
32 | """ :: forall eff. Fn3 String | ||
33 | (Error -> Eff (fs :: FS | eff) Unit) | ||
34 | (String -> Eff (fs :: FS | eff) Unit) | ||
35 | (Eff (fs :: FS | eff) Unit) | ||
36 | |||
37 | foreign import readFileUtf8SyncFn """ | ||
38 | function readFileUtf8SyncFn(filepath) { | ||
39 | return function(){ | ||
40 | var fs = require('fs'); | ||
41 | |||
42 | return fs.readFileSync(filepath, {encoding: 'utf-8'}); | ||
43 | }; | ||
44 | } | ||
45 | """ :: forall eff. String -> (Eff (fs :: FS | eff) String) | ||