]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/Loader.purs
Ensure output directory exists
[github/fretlink/purs-loader.git] / src / Loader.purs
CommitLineData
c194f84c 1module PursLoader.Loader
1983893b 2 ( Effects()
c194f84c 3 , loader
4 , loaderFn
5 ) where
6
7import Control.Monad.Aff (Aff(), runAff)
8import Control.Monad.Eff (Eff())
9import Control.Monad.Eff.Class (liftEff)
10import Control.Monad.Eff.Exception (error)
11
1983893b 12import Data.Array ((!!))
c194f84c 13import Data.Function (Fn2(), mkFn2)
14import Data.Maybe (Maybe(..), fromMaybe, maybe)
1983893b 15import Data.String.Regex (match, noFlags, regex)
c194f84c 16
17import PursLoader.ChildProcess (ChildProcess(), spawn)
1983893b 18import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query)
c194f84c 19import PursLoader.LoaderUtil (getRemainingRequest, parseQuery)
1983893b 20import PursLoader.Options (loaderSrcOption, pscOptions)
c194f84c 21
1983893b 22type Effects eff = (loader :: Loader, cp :: ChildProcess | eff)
c194f84c 23
24moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
25
1983893b 26pscCommand = "psc"
c194f84c 27
28(!!!) = flip (!!)
29
1983893b 30loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String)
c194f84c 31loader' ref source = do
32 liftEff $ cacheable ref
33
34 let request = getRemainingRequest ref
c194f84c 35 parsed = parseQuery $ query ref
1983893b 36 srcs = fromMaybe [] (loaderSrcOption parsed)
37 opts = pscOptions parsed
38 moduleName = match moduleRegex source >>= (!!!) 1
39 result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName
c194f84c 40
1983893b 41 spawn pscCommand (srcs <> opts)
42 return result
c194f84c 43
1983893b 44loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
c194f84c 45loader ref source = do
46 callback <- async ref
47 runAff (\e -> callback (Just e) "")
1983893b 48 (maybe (callback (Just (error "Loader has failed to run")) "")
c194f84c 49 (callback Nothing))
50 (loader' ref source)
51
1983893b 52loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
c194f84c 53loaderFn = mkFn2 loader