]> git.immae.eu Git - github/fretlink/purs-loader.git/commitdiff
Writes PureScript output to stderr
authoreric thul <thul.eric@gmail.com>
Sun, 6 Mar 2016 16:47:39 +0000 (11:47 -0500)
committereric thul <thul.eric@gmail.com>
Sun, 6 Mar 2016 16:47:39 +0000 (11:47 -0500)
Resolves #40

bower.json
docs/PursLoader/Loader.md
docs/PursLoader/Plugin.md
src/PursLoader/Loader.purs
src/PursLoader/Plugin.purs

index 761c24c8d47dad1928582c42f00f131fbd3d36d9..681612d7d09d1cc75178275105136abb1d3f5712 100644 (file)
@@ -5,6 +5,7 @@
     "purescript-aff": "^0.13.0",
     "purescript-foreign": "^0.7.0",
     "purescript-unsafe-coerce": "~0.1.0",
-    "purescript-nullable": "~0.2.1"
+    "purescript-nullable": "~0.2.1",
+    "purescript-node-process": "~0.4.1"
   }
 }
index bb0247077441b036b2ff3aa1ebed28fa20c49332..d05e3b726eec6a43a95d892942c33ed901b65931 100644 (file)
@@ -3,19 +3,25 @@
 #### `Effects`
 
 ``` purescript
-type Effects eff = (loader :: Loader | eff)
+type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
+```
+
+#### `Effects_`
+
+``` purescript
+type Effects_ eff = Effects (loader :: Loader | eff)
 ```
 
 #### `loader`
 
 ``` purescript
-loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
+loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
 ```
 
 #### `loaderFn`
 
 ``` purescript
-loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
+loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
 ```
 
 
index 9abec4df29eee5739123baf406c0ff36cd9215c8..645c41bcc64bfac91f73b6d86365af5b980124aa 100644 (file)
@@ -3,7 +3,7 @@
 #### `Result`
 
 ``` purescript
-type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph }
+type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph, output :: String }
 ```
 
 #### `Compile`
index d1068b6b91b896939b9cf491cfd694ab168fe91e..e1b9e0f36fa8dede7c11121cb0a5232b1bd1501b 100644 (file)
@@ -1,26 +1,31 @@
 module PursLoader.Loader
   ( Effects()
+  , Effects_()
   , loader
   , loaderFn
   ) where
 
-import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, unit)
+import Prelude (Unit(), ($), (>>=), (<$>), (<*>), (++), bind, const, id, pure, void, unit)
 
 import Control.Apply ((*>))
-import Control.Alt ((<|>))
 import Control.Bind (join)
 import Control.Monad.Eff (Eff(), foreachE)
-import Control.Monad.Eff.Exception (Error(), error)
+import Control.Monad.Eff.Console (CONSOLE())
+import Control.Monad.Eff.Exception (EXCEPTION(), Error(), error, message)
 
 import Data.Array ((!!))
 import Data.Bifunctor (lmap)
 import Data.Either (Either(..), either)
 import Data.Foreign.Class (read)
 import Data.Function (Fn2(), mkFn2)
-import Data.Maybe (Maybe(..), maybe)
+import Data.Maybe (Maybe(), maybe)
 import Data.Nullable (toMaybe)
 import Data.String.Regex (Regex(), match, noFlags, regex)
 
+import Node.Encoding (Encoding(UTF8))
+import Node.Process (stderr)
+import Node.Stream (writeString)
+
 import Unsafe.Coerce (unsafeCoerce)
 
 import PursLoader.LoaderRef
@@ -41,9 +46,11 @@ import PursLoader.Options (Options(..))
 import PursLoader.Path (dirname, relative)
 import PursLoader.Plugin as Plugin
 
-type Effects eff = (loader :: Loader | eff)
+type Effects eff = (console :: CONSOLE, err :: EXCEPTION | eff)
+
+type Effects_ eff = Effects (loader :: Loader | eff)
 
-loader :: forall eff. LoaderRef -> String -> Eff (Effects eff) Unit
+loader :: forall eff. LoaderRef -> String -> Eff (Effects_ eff) Unit
 loader ref source = do
   callback <- async ref
 
@@ -53,28 +60,35 @@ loader ref source = do
 
   pluginContext.compile (compile callback)
   where
-  pluginContext :: Plugin.Context (Effects eff)
+  pluginContext :: Plugin.Context (Effects_ eff)
   pluginContext = (unsafeCoerce ref).purescriptWebpackPluginContext
 
-  compile :: AsyncCallback eff -> Plugin.Compile (Effects eff)
-  compile callback error' { srcMap, ffiMap, graph } = do
+  compile :: AsyncCallback (Effects eff) -> Plugin.Compile (Effects_ eff)
+  compile callback error' { srcMap, ffiMap, graph, output } = do
     clearDependencies ref
 
     either (const $ pure unit) (\a -> debug ("Adding PureScript dependency " ++ a)) name
 
     addDependency ref (resourcePath ref)
 
-    either (\err -> callback (toMaybe error' <|> Just err) "") id
+    void $ writeString stderr UTF8 output (pure unit)
+
+    maybe (pure unit) (\a -> void $ writeString stderr UTF8 (message a) (pure unit)) (toMaybe error')
+
+    either (const $ callback (pure fixedError) "") id
            (handle <$> name <*> dependencies <*> exports)
     where
-    handle :: String -> Array String -> String -> Eff (Effects eff) Unit
+    fixedError :: Error
+    fixedError = error "PureScript compilation has failed."
+
+    handle :: String -> Array String -> String -> Eff (Effects_ eff) Unit
     handle name' deps res = do
       debug ("Adding PureScript transitive dependencies for " ++ name')
       addTransitive name'
       foreachE deps addTransitive
       debug "Generated loader result"
       debug res
-      callback (toMaybe error') res
+      callback (const fixedError <$> toMaybe error') res
 
     exports :: Either Error String
     exports = (\a b -> "module.exports = require('" ++ a ++ "')['" ++ b ++ "'];") <$> path <*> name
@@ -82,10 +96,10 @@ loader ref source = do
     dependencies :: Either Error (Array String)
     dependencies = name >>= Plugin.dependenciesOf graph
 
-    addTransitive :: String -> Eff (Effects eff) Unit
+    addTransitive :: String -> Eff (Effects_ eff) Unit
     addTransitive dep = addDep (Plugin.get srcMap dep) *> addDep (Plugin.get ffiMap dep)
       where
-      addDep :: Maybe String -> Eff (Effects eff) Unit
+      addDep :: Maybe String -> Eff (Effects_ eff) Unit
       addDep = maybe (pure unit) (addDependency ref)
 
     name :: Either Error String
@@ -107,5 +121,5 @@ loader ref source = do
       resourceDir :: String
       resourceDir = dirname (resourcePath ref)
 
-loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects eff) Unit)
+loaderFn :: forall eff. Fn2 LoaderRef String (Eff (Effects_ eff) Unit)
 loaderFn = mkFn2 loader
index 23f8600436478fdd0ed1c585d59f2b3718149046..c9f01333e2683b271a9a19dae3a8ef2079dbf011 100644 (file)
@@ -18,7 +18,7 @@ import Data.Function (Fn4(), runFn4)
 import Data.Maybe (Maybe(..))
 import Data.Nullable (Nullable())
 
-type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph }
+type Result = { srcMap :: ImmutableMap String String, ffiMap :: ImmutableMap String String, graph :: DependencyGraph, output :: String }
 
 type Compile eff = Nullable Error -> Result -> Eff eff Unit