diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/FS.purs | 32 | ||||
-rw-r--r-- | src/Glob.purs | 33 | ||||
-rw-r--r-- | src/Loader.purs | 52 | ||||
-rw-r--r-- | src/LoaderUtil.purs | 9 | ||||
-rw-r--r-- | src/Options.purs | 4 |
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 @@ | |||
1 | module PursLoader.FS | ||
2 | ( FS() | ||
3 | , writeFileUtf8 | ||
4 | ) where | ||
5 | |||
6 | import Control.Monad.Aff (Aff(), makeAff) | ||
7 | import Control.Monad.Eff (Eff()) | ||
8 | import Control.Monad.Eff.Exception (Error()) | ||
9 | |||
10 | import Data.Function | ||
11 | |||
12 | foreign import data FS :: ! | ||
13 | |||
14 | writeFileUtf8 :: forall eff. String -> String -> Aff (fs :: FS | eff) Unit | ||
15 | writeFileUtf8 filepath contents = makeAff $ runFn4 writeFileUtf8Fn filepath contents | ||
16 | |||
17 | foreign import writeFileUtf8Fn """ | ||
18 | function 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 @@ | |||
1 | module PursLoader.Glob | ||
2 | ( Glob() | ||
3 | , globAll | ||
4 | ) where | ||
5 | |||
6 | import Control.Monad.Aff (Aff(), makeAff) | ||
7 | import Control.Monad.Eff (Eff()) | ||
8 | import Control.Monad.Eff.Exception (Error()) | ||
9 | |||
10 | import Data.Function | ||
11 | |||
12 | foreign import data Glob :: ! | ||
13 | |||
14 | globAll :: forall eff. [String] -> Aff (glob :: Glob | eff) [[String]] | ||
15 | globAll patterns = makeAff $ runFn3 globAllFn patterns | ||
16 | |||
17 | foreign import globAllFn """ | ||
18 | function 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()) | |||
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 |
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 @@ | |||
1 | module PursLoader.LoaderUtil | 1 | module PursLoader.LoaderUtil |
2 | ( getRemainingRequest | 2 | ( parseQuery |
3 | , parseQuery | ||
4 | ) where | 3 | ) where |
5 | 4 | ||
6 | import Data.Foreign (Foreign()) | 5 | import Data.Foreign (Foreign()) |
7 | 6 | ||
8 | import PursLoader.LoaderRef (LoaderRef()) | 7 | import PursLoader.LoaderRef (LoaderRef()) |
9 | 8 | ||
10 | foreign import getRemainingRequest """ | ||
11 | function getRemainingRequest(ref){ | ||
12 | var loaderUtils = require('loader-utils'); | ||
13 | return loaderUtils.getRemainingRequest(ref); | ||
14 | }""" :: LoaderRef -> String | ||
15 | |||
16 | foreign import parseQuery """ | 9 | foreign import parseQuery """ |
17 | function parseQuery(query){ | 10 | function 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 @@ | |||
1 | module PursLoader.Options | 1 | module PursLoader.Options |
2 | ( pscOptions | 2 | ( pscOptions |
3 | , loaderSrcOption | 3 | , loaderSrcOption |
4 | , loaderFFIOption | ||
4 | ) where | 5 | ) where |
5 | 6 | ||
6 | import Data.Array (concat) | 7 | import Data.Array (concat) |
@@ -101,3 +102,6 @@ pscOptions query = either (const []) fold parsed | |||
101 | 102 | ||
102 | loaderSrcOption :: Foreign -> Maybe [String] | 103 | loaderSrcOption :: Foreign -> Maybe [String] |
103 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) | 104 | loaderSrcOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.src) (read query) |
105 | |||
106 | loaderFFIOption :: Foreign -> Maybe [String] | ||
107 | loaderFFIOption query = either (const Nothing) (\(Options a) -> runNullOrUndefined a.ffi) (read query) | ||