aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Loader.purs
diff options
context:
space:
mode:
authoreric thul <thul.eric@gmail.com>2015-07-06 23:49:47 -0400
committereric thul <thul.eric@gmail.com>2015-07-06 23:55:06 -0400
commit0e1221d7b15e578d5e9146b01e11a24007d4ba9b (patch)
treeaa70df70f6c36b7a85783c28d7eb256854b4232c /src/Loader.purs
parentbc4de0853bd1cbf63842b3b66bb30494c9ea778a (diff)
downloadpurs-loader-0e1221d7b15e578d5e9146b01e11a24007d4ba9b.tar.gz
purs-loader-0e1221d7b15e578d5e9146b01e11a24007d4ba9b.tar.zst
purs-loader-0e1221d7b15e578d5e9146b01e11a24007d4ba9b.zip
Generate .psci file
Resolves #11
Diffstat (limited to 'src/Loader.purs')
-rw-r--r--src/Loader.purs52
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())
9import Control.Monad.Eff.Class (liftEff) 9import Control.Monad.Eff.Class (liftEff)
10import Control.Monad.Eff.Exception (error) 10import Control.Monad.Eff.Exception (error)
11 11
12import Data.Array ((!!)) 12import Data.Array ((!!), concat)
13import Data.Function (Fn2(), mkFn2) 13import Data.Function (Fn2(), mkFn2)
14import Data.Maybe (Maybe(..), fromMaybe, maybe) 14import Data.Maybe (Maybe(..), fromMaybe, maybe)
15import Data.String (joinWith)
15import Data.String.Regex (match, noFlags, regex) 16import Data.String.Regex (match, noFlags, regex)
16 17
17import PursLoader.ChildProcess (ChildProcess(), spawn) 18import PursLoader.ChildProcess (ChildProcess(), spawn)
19import PursLoader.FS (FS(), writeFileUtf8)
20import PursLoader.Glob (Glob(), globAll)
18import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query) 21import PursLoader.LoaderRef (LoaderRef(), Loader(), async, cacheable, query)
19import PursLoader.LoaderUtil (getRemainingRequest, parseQuery) 22import PursLoader.LoaderUtil (parseQuery)
20import PursLoader.Options (loaderSrcOption, pscOptions) 23import PursLoader.Options (loaderFFIOption, loaderSrcOption, pscOptions)
21 24
22type Effects eff = (loader :: Loader, cp :: ChildProcess | eff) 25type Effects eff = (cp :: ChildProcess, fs :: FS, glob :: Glob, loader :: Loader | eff)
23 26
24moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } 27moduleRegex = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true }
25 28
26pscCommand = "psc" 29pscCommand = "psc"
27 30
31psciCommand = "psci"
32
33psciFilename = ".psci"
34
28(!!!) = flip (!!) 35(!!!) = flip (!!)
29 36
37foreign import cwd "var cwd = process.cwd();" :: String
38
39foreign import relative """
40function relative(from) {
41 return function(to){
42 var path = require('path');
43 return path.relative(from, to);
44 };
45}
46""" :: String -> String -> String
47
48mkPsci :: [[String]] -> [[String]] -> String
49mkPsci 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
30loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String) 57loader' :: forall eff. LoaderRef -> String -> Aff (Effects eff) (Maybe String)
31loader' ref source = do 58loader' 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
44loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit 80loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit