From 3610dff1b8308a810d827f0595832b326deff37b Mon Sep 17 00:00:00 2001 From: eric thul Date: Sat, 18 Jul 2015 17:07:38 -0400 Subject: Add FFI JavaScript as a webpack dependency Resolves #18 --- src/FS.purs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/FS.purs') 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 @@ module PursLoader.FS ( FS() , writeFileUtf8 + , findFileUtf8 ) where import Control.Monad.Aff (Aff(), makeAff) import Control.Monad.Eff (Eff()) import Control.Monad.Eff.Exception (Error()) +import Data.Maybe (Maybe(..)) +import Data.String.Regex (Regex()) + import Data.Function foreign import data FS :: ! @@ -30,3 +34,34 @@ function writeFileUtf8Fn(filepath, contents, errback, callback) { (Error -> Eff (fs :: FS | eff) Unit) (Unit -> Eff (fs :: FS | eff) Unit) (Eff (fs :: FS | eff) Unit) + +findFileUtf8 :: forall eff. Regex -> [String] -> Aff (fs :: FS | eff) (Maybe String) +findFileUtf8 regexp filepaths = makeAff $ runFn6 findFileUtf8Fn Nothing Just regexp filepaths + +foreign import findFileUtf8Fn """ +function findFileUtf8Fn(nothing, just, regex, filepaths, errback, callback) { + return function(){ + var fs = require('fs'); + + var async = require('async'); + + function findFile(filepath, callback) { + fs.readFile(filepath, {encoding: 'utf-8'}, function(error, result){ + if (error) callback(false); + else callback(regex.test(result)); + }); + } + + async.detect(filepaths, findFile, function(result){ + if (!result) callback(nothing)(); + else callback(just(result))(); + }); + }; +} +""" :: forall eff. Fn6 (Maybe String) + (String -> Maybe String) + Regex + [String] + (Error -> Eff (fs :: FS | eff) Unit) + (Maybe String -> Eff (fs :: FS | eff) Unit) + (Eff (fs :: FS | eff) Unit) -- cgit v1.2.3