diff options
author | eric thul <thul.eric@gmail.com> | 2015-07-18 17:07:38 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-07-18 17:07:38 -0400 |
commit | 3610dff1b8308a810d827f0595832b326deff37b (patch) | |
tree | a4858f5e16e3df01aee249ad53b5b76f90fb97de /src/FS.purs | |
parent | 418c9429609c6d80ddaf7df41e0ee64192e2ec7b (diff) | |
download | purs-loader-3610dff1b8308a810d827f0595832b326deff37b.tar.gz purs-loader-3610dff1b8308a810d827f0595832b326deff37b.tar.zst purs-loader-3610dff1b8308a810d827f0595832b326deff37b.zip |
Add FFI JavaScript as a webpack dependency
Resolves #18
Diffstat (limited to 'src/FS.purs')
-rw-r--r-- | src/FS.purs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/FS.purs b/src/FS.purs index a56fe26..6955a63 100644 --- a/src/FS.purs +++ b/src/FS.purs | |||
@@ -1,12 +1,16 @@ | |||
1 | module PursLoader.FS | 1 | module PursLoader.FS |
2 | ( FS() | 2 | ( FS() |
3 | , writeFileUtf8 | 3 | , writeFileUtf8 |
4 | , findFileUtf8 | ||
4 | ) where | 5 | ) where |
5 | 6 | ||
6 | import Control.Monad.Aff (Aff(), makeAff) | 7 | import Control.Monad.Aff (Aff(), makeAff) |
7 | import Control.Monad.Eff (Eff()) | 8 | import Control.Monad.Eff (Eff()) |
8 | import Control.Monad.Eff.Exception (Error()) | 9 | import Control.Monad.Eff.Exception (Error()) |
9 | 10 | ||
11 | import Data.Maybe (Maybe(..)) | ||
12 | import Data.String.Regex (Regex()) | ||
13 | |||
10 | import Data.Function | 14 | import Data.Function |
11 | 15 | ||
12 | foreign import data FS :: ! | 16 | foreign import data FS :: ! |
@@ -30,3 +34,34 @@ function writeFileUtf8Fn(filepath, contents, errback, callback) { | |||
30 | (Error -> Eff (fs :: FS | eff) Unit) | 34 | (Error -> Eff (fs :: FS | eff) Unit) |
31 | (Unit -> Eff (fs :: FS | eff) Unit) | 35 | (Unit -> Eff (fs :: FS | eff) Unit) |
32 | (Eff (fs :: FS | eff) Unit) | 36 | (Eff (fs :: FS | eff) Unit) |
37 | |||
38 | findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String) | ||
39 | findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths | ||
40 | |||
41 | foreign import findFileUtf8Fn """ | ||
42 | function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) { | ||
43 | return function(){ | ||
44 | var fs = require('fs'); | ||
45 | |||
46 | var async = require('async'); | ||
47 | |||
48 | function findFile(filepath, callback) { | ||
49 | fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){ | ||
50 | if (error) callback(false); | ||
51 | else callback(regex.test(result)); | ||
52 | }); | ||
53 | } | ||
54 | |||
55 | async.detect(filepaths, findFile, function(result){ | ||
56 | if (!result) callback(nothing)(); | ||
57 | else callback(just(result))(); | ||
58 | }); | ||
59 | }; | ||
60 | } | ||
61 | """ :: forall eff. Fn6 (Maybe String) | ||
62 | (String -> Maybe String) | ||
63 | Regex | ||
64 | [String] | ||
65 | (Error -> Eff (fs :: FS | eff) Unit) | ||
66 | (Maybe String -> Eff (fs :: FS | eff) Unit) | ||
67 | (Eff (fs :: FS | eff) Unit) | ||