diff options
author | eric thul <thul.eric@gmail.com> | 2015-08-11 20:27:04 -0400 |
---|---|---|
committer | eric thul <thul.eric@gmail.com> | 2015-08-11 20:27:04 -0400 |
commit | fa01c5a4cb42d80ac147dc5ab512a0795dbe14da (patch) | |
tree | 8bdb1f4c24a31c7fd5c69fcd804e8428ae6c49b6 /src/PursLoader/FS.purs | |
parent | eae2e182cec2d521166e5775294b52300c42f069 (diff) | |
download | purs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.tar.gz purs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.tar.zst purs-loader-fa01c5a4cb42d80ac147dc5ab512a0795dbe14da.zip |
Moving files to match module
Diffstat (limited to 'src/PursLoader/FS.purs')
-rw-r--r-- | src/PursLoader/FS.purs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/PursLoader/FS.purs b/src/PursLoader/FS.purs new file mode 100644 index 0000000..6955a63 --- /dev/null +++ b/src/PursLoader/FS.purs | |||
@@ -0,0 +1,67 @@ | |||
1 | module PursLoader.FS | ||
2 | ( FS() | ||
3 | , writeFileUtf8 | ||
4 | , findFileUtf8 | ||
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.Maybe (Maybe(..)) | ||
12 | import Data.String.Regex (Regex()) | ||
13 | |||
14 | import Data.Function | ||
15 | |||
16 | foreign import data FS :: ! | ||
17 | |||
18 | writeFileUtf8 :: forall eff. String -> String -> Aff (fs :: FS | eff) Unit | ||
19 | writeFileUtf8 filepath contents = makeAff $ runFn4 writeFileUtf8Fn filepath contents | ||
20 | |||
21 | foreign import writeFileUtf8Fn """ | ||
22 | function writeFileUtf8Fn(filepath, contents, errback, callback) { | ||
23 | return function(){ | ||
24 | var fs = require('fs'); | ||
25 | |||
26 | fs.writeFile(filepath, contents, function(error){ | ||
27 | if (error) errback(error)(); | ||
28 | else callback()(); | ||
29 | }); | ||
30 | }; | ||
31 | } | ||
32 | """ :: forall eff. Fn4 String | ||
33 | String | ||
34 | (Error -> Eff (fs :: FS | eff) Unit) | ||
35 | (Unit -> 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) | ||