@@ -10,7 +10,7 @@ module Compiler
1010 )
1111 where
1212
13−import Prelude (bind, pure, show, ($), (<<<))
13+import Prelude (bind, pure, show, ($), (<<<), const)
1414import Control.Monad.Error.Class (liftEither)
1515import Control.Monad.Reader.Trans (ReaderT, runReaderT, ask)
1616import Control.Monad.Trans.Class (lift)
@@ -18,6 +18,7 @@ import Data.Argonaut.Core (Json)
1818import Data.Argonaut.Decode (printJsonDecodeError)
1919import Data.Argonaut.Decode.Class (decodeJson)
2020import Data.Argonaut.Parser (jsonParser)
21+import Data.Functor ((<$>))
2122import Data.Bifunctor (lmap)
2223import Data.Either (Either)
2324import Data.Maybe (Maybe(Just))
@@ -27,6 +28,8 @@ import Effect.Exception (Error)
2728import Affjax.ResponseFormat (string)
2829import Affjax.Node (post, printError)
2930import Affjax.RequestBody (RequestBody (String))
31+import Parsing.String as PS
32+import Parsing.String.Replace as PS.Replace
3033
3134type Settings = { protocol :: String
3235 , hostname :: String
@@ -71,16 +74,22 @@ askSettings = lift ask
7174liftAff :: forall a. Aff a -> Compiler a
7275liftAff h = lift $ lift h
7376
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
80+
7481compile :: Compiler Code
7582compile = do
7683 code <- askCode
7784 s <- askSettings
78− let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port <> "/compile"
79− mRes <- liftAff $ post string url $ Just $ String code
85+ let url = s.protocol <> "://" <> s.hostname <> ":" <> show s.port
86+ endpoint = "/compile"
87+ output = "/output"
88+ mRes <- liftAff $ post string (url <> endpoint) $ Just $ String code
8089 res <- liftEither $ (error <<< printError) `lmap` mRes
8190 body <- liftEither $ error `lmap` (jsonParser res.body)
8291 succ <- liftEither $ s.parser body
83− pure $ succ.js
92+ pure $ rename url output succ.js
8493
8594runCompiler :: Settings -> Code -> Aff Code
8695runCompiler s code = runReaderT (runReaderT compile code) s