diff options
Diffstat (limited to 'src/Loader.purs')
-rw-r--r-- | src/Loader.purs | 52 |
1 files changed, 44 insertions, 8 deletions
diff --git a/src/Loader.purs b/src/Loader.purs index 0235da9..872a51c 100644 --- a/src/Loader.purs +++ b/src/Loader.purs | |||
@@ -9,36 +9,72 @@ import Control.Monad.Eff (Eff()) | |||
9 | import Control.Monad.Eff.Class (liftEff) | 9 | import Control.Monad.Eff.Class (liftEff) |
10 | import Control.Monad.Eff.Exception (error) | 10 | import Control.Monad.Eff.Exception (error) |
11 | 11 | ||
12 | import Data.Array ((!!)) | 12 | import Data.Array ((!!), concat) |
13 | import Data.Function (Fn2(), mkFn2) | 13 | import Data.Function (Fn2(), mkFn2) |
14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) | 14 | import Data.Maybe (Maybe(..), fromMaybe, maybe) |
15 | import Data.String (joinWith) | ||
15 | import Data.String.Regex (match, noFlags, regex) | 16 | import Data.String.Regex (match, noFlags, regex) |
16 | 17 | ||
17 | import PursLoader.ChildProcess (ChildProcess(), spawn) | 18 | import PursLoader.ChildProcess (ChildProcess(), spawn) |
19 | import PursLoader.FS (FS(), writeFileUtf8) | ||
20 | import PursLoader.Glob (Glob(), globAll) | ||
18 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) | 21 | import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) |
19 | import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) | 22 | import PursLoader.LoaderUtil (parseQuery) |
20 | import PursLoader.Options (loaderSrcOption, pscOptions) | 23 | import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions) |
21 | 24 | ||
22 | type Effects eff = (loader :: Loader, cp :: ChildProcess | eff) | 25 | type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff) |
23 | 26 | ||
24 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 27 | moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
25 | 28 | ||
26 | pscCommand = "psc" | 29 | pscCommand = "psc" |
27 | 30 | ||
31 | psciCommand = "psci" | ||
32 | |||
33 | psciFilename = ".psci" | ||
34 | |||
28 | (!!!) = flip (!!) | 35 | (!!!) = flip (!!) |
29 | 36 | ||
37 | foreign import cwd "var cwd = process.cwd();" :: String | ||
38 | |||
39 | foreign import relative """ | ||
40 | function relative(from) { | ||
41 | return function(to){ | ||
42 | var path = require('path'); | ||
43 | return path.relative(from, to); | ||
44 | }; | ||
45 | } | ||
46 | """ :: String -> String -> String | ||
47 | |||
48 | mkPsci :: [[String]] -> [[String]] -> String | ||
49 | mkPsci srcs ffis = joinWith "\n" ((loadModule <$> concat srcs) <> (loadForeign <$> concat ffis)) | ||
50 | where | ||
51 | loadModule :: String -> String | ||
52 | loadModule a = ":m " ++ relative cwd a | ||
53 | |||
54 | loadForeign :: String -> String | ||
55 | loadForeign a = ":f " ++ relative cwd a | ||
56 | |||
30 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) | 57 | loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) |
31 | loader' ref source = do | 58 | loader' ref source = do |
32 | liftEff $ cacheable ref | 59 | liftEff $ cacheable ref |
33 | 60 | ||
34 | let request = getRemainingRequest ref | 61 | let parsed = parseQuery $ query ref |
35 | parsed = parseQuery $ query ref | ||
36 | srcs = fromMaybe [] (loaderSrcOption parsed) | 62 | srcs = fromMaybe [] (loaderSrcOption parsed) |
63 | ffis = fromMaybe [] (loaderFFIOption parsed) | ||
37 | opts = pscOptions parsed | 64 | opts = pscOptions parsed |
38 | moduleName = match moduleRegex source >>= (!!!) 1 | ||
39 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName | ||
40 | 65 | ||
41 | spawn pscCommand (srcs <> opts) | 66 | spawn pscCommand (srcs <> opts) |
67 | |||
68 | srcss <- globAll srcs | ||
69 | ffiss <- globAll ffis | ||
70 | |||
71 | let psciFile = mkPsci srcss ffiss | ||
72 | |||
73 | writeFileUtf8 psciFilename psciFile | ||
74 | |||
75 | let moduleName = match moduleRegex source >>= (!!!) 1 | ||
76 | result = (\a -> "module.exports = require('" ++ a ++ "');") <$> moduleName | ||
77 | |||
42 | return result | 78 | return result |
43 | 79 | ||
44 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 80 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit |