aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
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
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')
-rw-r--r--src/FS.purs32
-rw-r--r--src/Glob.purs33
-rw-r--r--src/Loader.purs52
-rw-r--r--src/LoaderUtil.purs9
-rw-r--r--src/Options.purs4
5 files changed, 114 insertions, 16 deletions
diff --git a/src/FS.purs b/src/FS.purs
new file mode 100644
index 0000000..a56fe26
--- /dev/null
+++ b/src/FS.purs
@@ -0,0 +1,32 @@
1module PursLoader.FS
2 ( FS()
3 , writeFileUtf8
4 ) where
5
6import Control.Monad.Aff (Aff(), makeAff)
7import Control.Monad.Eff (Eff())
8import Control.Monad.Eff.Exception (Error())
9
10import Data.Function
11
12foreign import data FS :: !
13
14writeFileUtf8 :: forall eff. String -> String -> Aff (fs :: FS | eff) Unit
15writeFileUtf8 filepath contents = makeAff $ runFn4 writeFileUtf8Fn filepath contents
16
17foreign import writeFileUtf8Fn """
18function writeFileUtf8Fn(filepath, contents, errback, callback) {
19 return function(){
20 var fs = require('fs');
21
22 fs.writeFile(filepath, contents, function(error){
23 if (error) errback(error)();
24 else callback()();
25 });
26 };
27}
28""" :: forall eff. Fn4 String
29 String
30 (Error -> Eff (fs :: FS | eff) Unit)
31 (Unit -> Eff (fs :: FS | eff) Unit)
32 (Eff (fs :: FS | eff) Unit)
diff --git a/src/Glob.purs b/src/Glob.purs
new file mode 100644
index 0000000..392d9e4
--- /dev/null
+++ b/src/Glob.purs
@@ -0,0 +1,33 @@
1module PursLoader.Glob
2 ( Glob()
3 , globAll
4 ) where
5
6import Control.Monad.Aff (Aff(), makeAff)
7import Control.Monad.Eff (Eff())
8import Control.Monad.Eff.Exception (Error())
9
10import Data.Function
11
12foreign import data Glob :: !
13
14globAll :: forall eff. [String] -> Aff (glob :: Glob | eff) [[String]]
15globAll patterns = makeAff $ runFn3 globAllFn patterns
16
17foreign import globAllFn """
18function globAllFn(patterns, errback, callback) {
19 return function(){
20 var glob = require('glob');
21
22 var async = require('async');
23
24 async.map(patterns, glob, function(error, result){
25 if (error) errback(new Error(error))();
26 else callback(result)();
27 });
28 };
29}
30""" :: forall eff. Fn3 [String]
31 (Error -> Eff (glob :: Glob | eff) Unit)
32 ([[String]] -> Eff (glob :: Glob | eff) Unit)
33 (Eff (glob :: Glob | eff) Unit)
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
diff --git a/src/LoaderUtil.purs b/src/LoaderUtil.purs
index f22be44..86be124 100644
--- a/src/LoaderUtil.purs
+++ b/src/LoaderUtil.purs
@@ -1,18 +1,11 @@
1module PursLoader.LoaderUtil 1module PursLoader.LoaderUtil
2 ( getRemainingRequest 2 ( parseQuery
3 , parseQuery
4 ) where 3 ) where
5 4
6import Data.Foreign (Foreign()) 5import Data.Foreign (Foreign())
7 6
8import PursLoader.LoaderRef (LoaderRef()) 7import PursLoader.LoaderRef (LoaderRef())
9 8
10foreign import getRemainingRequest """
11function getRemainingRequest(ref){
12 var loaderUtils = require('loader-utils');
13 return loaderUtils.getRemainingRequest(ref);
14}""" :: LoaderRef -> String
15
16foreign import parseQuery """ 9foreign import parseQuery """
17function parseQuery(query){ 10function parseQuery(query){
18 var loaderUtils = require('loader-utils'); 11 var loaderUtils = require('loader-utils');
diff --git a/src/Options.purs b/src/Options.purs
index be21877..51e9be5 100644
--- a/src/Options.purs
+++ b/src/Options.purs
@@ -1,6 +1,7 @@
1module PursLoader.Options 1module PursLoader.Options
2 ( pscOptions 2 ( pscOptions
3 , loaderSrcOption 3 , loaderSrcOption
4 , loaderFFIOption
4 ) where 5 ) where
5 6
6import Data.Array (concat) 7import Data.Array (concat)
@@ -101,3 +102,6 @@ pscOptions query = either (const []) fold parsed
101 102
102loaderSrcOption :: Foreign -> Maybe [String] 103loaderSrcOption :: Foreign -> Maybe [String]
103loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) 104loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query)
105
106loaderFFIOption :: Foreign -> Maybe [String]
107loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query)