@@ -10,7 +10,7 @@ module Compiler
13−import Prelude (bind, pure, show, ($), (<<<))
13+import Prelude (bind, pure, show, ($), (<<<), const)
14import Control.Monad.Error.Class (liftEither)
14import Control.Monad.Error.Class (liftEither)
15import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
15import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
16import Control.Monad.Trans.Class (lift)
16import Control.Monad.Trans.Class (lift)
@@ -18,6 +18,7 @@ import Data.Argonaut.Core (Json)
18import Data.Argonaut.Decode (printJsonDecodeError)
18import Data.Argonaut.Decode (printJsonDecodeError)
19import Data.Argonaut.Decode.Class (decodeJson)
19import Data.Argonaut.Decode.Class (decodeJson)
20import Data.Argonaut.Parser (jsonParser)
20import Data.Argonaut.Parser (jsonParser)
21+import Data.Functor ((<$>))
21import Data.Bifunctor (lmap)
22import Data.Bifunctor (lmap)
22import Data.Either (Either)
23import Data.Either (Either)
23import Data.Maybe (Maybe(Just))
24import Data.Maybe (Maybe(Just))
@@ -27,6 +28,8 @@ import Effect.Exception (Error)
27import Affjax.ResponseFormat (string)
28import Affjax.ResponseFormat (string)
28import Affjax.Node (post, printError)
29import Affjax.Node (post, printError)
29import Affjax.RequestBody (RequestBody (String))
30import Affjax.RequestBody (RequestBody (String))
31+import Parsing.String as PS
32+import Parsing.String.Replace as PS.Replace
31type Settings = { protocol :: String
34type Settings = { protocol :: String
32 , hostname :: String
35 , hostname :: String
@@ -71,16 +74,22 @@ askSettings = lift ask
71liftAff :: forall a. Aff a -> Compiler a
74liftAff :: forall a. Aff a -> Compiler a
72liftAff h = lift $ lift h
75liftAff h = lift $ lift h
77+rename :: String -> String -> Code -> Code
78+rename url output code = PS.Replace.replace code (prefix <$> PS.string "from \"..") where
79+ prefix = const $ "from \"" <> url <> output
74compile :: Compiler Code
81compile :: Compiler Code
75compile = do
82compile = do
76 code <- askCode
83 code <- askCode
77 s <- askSettings
84 s <- askSettings
78− let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port <> "/compile"
85+ let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port
86+ endpoint = "/compile"
79− mRes <- liftAff $ post string url $ Just $ String code
88+ mRes <- liftAff $ post string (url <> endpoint) $ Just $ String code
80 res <- liftEither $ (error <<< printError) `lmap` mRes
89 res <- liftEither $ (error <<< printError) `lmap` mRes
81 body <- liftEither $ error `lmap` (jsonParser res.body)
90 body <- liftEither $ error `lmap` (jsonParser res.body)
82 succ <- liftEither $ s.parser body
91 succ <- liftEither $ s.parser body
83− pure $ succ.js
92+ pure $ rename url output succ.js
85runCompiler :: Settings -> Code -> Aff Code
94runCompiler :: Settings -> Code -> Aff Code
86runCompiler s code = runReaderT (runReaderT compile code) s
95runCompiler s code = runReaderT (runReaderT compile code) s