@@ -31,6 +31,7 @@ module Language.PureScript.Ide.Integration
3131 , addImplicitImport
3232 , loadModule
3333 , loadModuleWithDeps
34+ , getCwd
3435 , getFlexCompletions
3536 , getType
3637 , rebuildModule
@@ -48,13 +49,14 @@ import Data.Aeson
4849import Data.Aeson.Types
4950import qualified Data.ByteString.Lazy.UTF8 as BSL
5051import Data.Either (isRight)
51−import Data.Maybe (fromJust)
52+import Data.Maybe (fromJust, isNothing)
5253import qualified Data.Text as T
5354import qualified Data.Vector as V
5455import Language.PureScript.Ide.Util
5556import System.Directory
5657import System.Exit
5758import System.FilePath
59+import System.IO.Error (mkIOError, userErrorType)
5860import System.Process
5961
6062projectDirectory :: IO FilePath
@@ -77,9 +79,15 @@ stopServer = terminateProcess
7779withServer :: IO a -> IO a
7880withServer s = do
7981 _ <- startServer
82+ started <- tryNTimes 5 (shush <$> (try getCwd :: IO (Either SomeException String)))
83+ when (isNothing started) $
84+ throwIO (mkIOError userErrorType "psc-ide-server didn't start in time" Nothing Nothing)
8085 r <- s
8186 quitServer
82− return r
87+ pure r
88+
89+shush :: Either a b -> Maybe b
90+shush = either (const Nothing) Just
8391
8492-- project management utils
8593
@@ -93,6 +101,16 @@ compileTestProject = do
93101 }
94102 isSuccess <$> waitForProcess procHandle
95103
104+tryNTimes :: Int -> IO (Maybe a) -> IO (Maybe a)
105+tryNTimes 0 _ = pure Nothing
106+tryNTimes n action = do
107+ r <- action
108+ case r of
109+ Nothing -> do
110+ threadDelay 500000
111+ tryNTimes (n - 1) action
112+ Just a -> pure (Just a)
113+
96114deleteOutputFolder :: IO ()
97115deleteOutputFolder = do
98116 odir <- fmap (</> "output") projectDirectory
@@ -137,6 +155,11 @@ reset = do
137155 _ <- try $ sendCommand resetCommand :: IO (Either SomeException String)
138156 return ()
139157
158+getCwd :: IO String
159+getCwd = do
160+ let cwdCommand = object ["command" .= ("cwd" :: String)]
161+ sendCommand cwdCommand
162+
140163loadModuleWithDeps :: String -> IO String
141164loadModuleWithDeps m = sendCommand $ load [] [m]
142165