diff options
author | eric <thul.eric@gmail.com> | 2016-03-12 14:56:34 -0500 |
---|---|---|
committer | eric <thul.eric@gmail.com> | 2016-03-12 14:56:34 -0500 |
commit | 7c6f8623b2448e9a371d6c7964493e365843004c (patch) | |
tree | 5079dcf661e96520e9b37562b47ab4d83bc7496a /src/PursLoader/Loader.purs | |
parent | 55120f4502cb76e768f60654f3937db809df8ade (diff) | |
parent | cb2f4c886fd1057fbf0511b46cab3489123ced23 (diff) | |
download | purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.tar.gz purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.tar.zst purs-loader-7c6f8623b2448e9a371d6c7964493e365843004c.zip |
Merge pull request #42 from ethul/topic/stderr
Writes PureScript output to stderr
Diffstat (limited to 'src/PursLoader/Loader.purs')
-rw-r--r-- | src/PursLoader/Loader.purs | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/PursLoader/Loader.purs b/src/PursLoader/Loader.purs index 402e805..c50c63c 100644 --- a/src/PursLoader/Loader.purs +++ b/src/PursLoader/Loader.purs | |||
@@ -1,23 +1,28 @@ | |||
1 | module PursLoader.Loader | 1 | module PursLoader.Loader |
2 | ( Effects() | 2 | ( Effects() |
3 | , Effects_() | ||
3 | , loader | 4 | , loader |
4 | , loaderFn | 5 | , loaderFn |
5 | ) where | 6 | ) where |
6 | 7 | ||
7 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit) | 8 | import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), (<<<), bind, const, id, pure, unit, void) |
8 | 9 | ||
9 | import Control.Alt ((<|>)) | ||
10 | import Control.Bind (join) | 10 | import Control.Bind (join) |
11 | import Control.Monad.Eff (Eff(), foreachE) | 11 | import Control.Monad.Eff (Eff(), foreachE) |
12 | import Control.Monad.Eff.Exception (Error(), error) | 12 | import Control.Monad.Eff.Console (CONSOLE()) |
13 | import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message) | ||
13 | 14 | ||
14 | import Data.Array ((!!)) | 15 | import Data.Array ((!!)) |
15 | import Data.Either (Either(..), either) | 16 | import Data.Either (Either(..), either) |
16 | import Data.Function (Fn2(), mkFn2) | 17 | import Data.Function (Fn2(), mkFn2) |
17 | import Data.Maybe (Maybe(..), maybe) | 18 | import Data.Maybe (maybe) |
18 | import Data.Nullable (toMaybe) | 19 | import Data.Nullable (toMaybe) |
19 | import Data.String.Regex (Regex(), match, noFlags, regex) | 20 | import Data.String.Regex (Regex(), match, noFlags, regex) |
20 | 21 | ||
22 | import Node.Encoding (Encoding(UTF8)) | ||
23 | import Node.Process (stderr) | ||
24 | import Node.Stream (writeString) | ||
25 | |||
21 | import Unsafe.Coerce (unsafeCoerce) | 26 | import Unsafe.Coerce (unsafeCoerce) |
22 | 27 | ||
23 | import PursLoader.LoaderRef | 28 | import PursLoader.LoaderRef |
@@ -34,9 +39,11 @@ import PursLoader.Debug (debug) | |||
34 | import PursLoader.Path (dirname, joinPath, relative) | 39 | import PursLoader.Path (dirname, joinPath, relative) |
35 | import PursLoader.Plugin as Plugin | 40 | import PursLoader.Plugin as Plugin |
36 | 41 | ||
37 | type Effects eff = (loader :: Loader | eff) | 42 | type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff) |
43 | |||
44 | type Effects_ eff = Effects (loader :: Loader | eff) | ||
38 | 45 | ||
39 | loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit | 46 | loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit |
40 | loader ref source = do | 47 | loader ref source = do |
41 | callback <- async ref | 48 | callback <- async ref |
42 | 49 | ||
@@ -46,25 +53,32 @@ loader ref source = do | |||
46 | 53 | ||
47 | pluginContext.compile (compile callback) | 54 | pluginContext.compile (compile callback) |
48 | where | 55 | where |
49 | pluginContext :: Plugin.Context (Effects eff) | 56 | pluginContext :: Plugin.Context (Effects_ eff) |
50 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext | 57 | pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext |
51 | 58 | ||
52 | compile :: AsyncCallback eff -> Plugin.Compile (Effects eff) | 59 | compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff) |
53 | compile callback error' graph = do | 60 | compile callback error' graph output = do |
54 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name | 61 | either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name |
55 | 62 | ||
56 | addDependency ref (resourcePath ref) | 63 | addDependency ref (resourcePath ref) |
57 | 64 | ||
58 | either (\err -> callback (toMaybe error' <|> Just err) "") id | 65 | void $ writeString stderr UTF8 output (pure unit) |
66 | |||
67 | maybe (pure unit) (\a -> void $ writeString stderr UTF8 (message a) (pure unit)) (toMaybe error') | ||
68 | |||
69 | either (const $ callback (pure fixedError) "") id | ||
59 | (handle <$> name <*> dependencies <*> exports) | 70 | (handle <$> name <*> dependencies <*> exports) |
60 | where | 71 | where |
61 | handle :: String -> Array String -> String -> Eff (Effects eff) Unit | 72 | fixedError :: Error |
73 | fixedError = error "PureScript compilation has failed." | ||
74 | |||
75 | handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit | ||
62 | handle name' deps res = do | 76 | handle name' deps res = do |
63 | debug ("Adding PureScript dependencies for " ++ name') | 77 | debug ("Adding PureScript dependencies for " ++ name') |
64 | foreachE deps (addDependency ref) | 78 | foreachE deps (addDependency ref) |
65 | debug "Generated loader result" | 79 | debug "Generated loader result" |
66 | debug res | 80 | debug res |
67 | callback (toMaybe error') res | 81 | callback (const fixedError <$> toMaybe error') res |
68 | 82 | ||
69 | exports :: Either Error String | 83 | exports :: Either Error String |
70 | exports = | 84 | exports = |
@@ -98,5 +112,5 @@ loader ref source = do | |||
98 | re :: Regex | 112 | re :: Regex |
99 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } | 113 | re = regex "(?:^|\\n)module\\s+([\\w\\.]+)" noFlags { ignoreCase = true } |
100 | 114 | ||
101 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit) | 115 | loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit) |
102 | loaderFn = mkFn2 loader | 116 | loaderFn = mkFn2 loader |