@@ -33,20 +33,23 @@ serve portNumber libLoc = do
3333pageTitle :: String -> String
3434pageTitle = dropExtension . takeBaseName
3535
36−defaultCacheDirectory = "elm-server-cache"
37−
3836serveElm :: FilePath -> ServerPartT IO Response
39−serveElm fp = do
40− fileExists <- liftIO $ doesFileExist file
41− guard $ fileExists && takeExtension fp == ".elm"
37+serveElm fp =
38+ do fileExists <- liftIO $ doesFileExist file
39+ guard (fileExists && takeExtension fp == ".elm")
40+ onSuccess compile serve
41+ where
42+ file = tail fp
4243
43− (_, stdout, _, handle) <- liftIO $ createProcess $
44− (proc "elm" args) { std_out = CreatePipe }
45− exitCode <- liftIO $ waitForProcess handle
44+ compile = liftIO $ createProcess $ (proc "elm" args) { std_out = CreatePipe }
45+ where args = [ "--make", "--runtime=" ++ runtime, file ]
4646
47− dirExists <- liftIO $ doesDirectoryExist defaultCacheDirectory
48− when dirExists $ liftIO $ removeDirectoryRecursive defaultCacheDirectory
47+ serve = serveFile (asContentType "text/html")
48+ ("build" </> replaceExtension file "html")
4949
50+onSuccess action success = do
51+ (_, stdout, _, handle) <- action
52+ exitCode <- liftIO $ waitForProcess handle
5053 case (exitCode, stdout) of
5154 (ExitFailure 127, _) ->
5255 badRequest $ toResponse "Error: elm binary not found in your path."
@@ -55,13 +58,8 @@ serveElm fp = do
5558 badRequest $ toResponse str
5659 (ExitFailure _, Nothing) ->
5760 badRequest $ toResponse "See command line for error message."
58− (ExitSuccess, _) ->
59− serveFile
60− (asContentType "text/html") ("build" </> replaceExtension file "html")
61+ (ExitSuccess, _) -> success
6162
62− where file = tail fp
63− args = [ "--make" ,"--runtime=" ++ runtime,
64− "--cache-dir=" ++ defaultCacheDirectory, file ]
6563
6664serveLib :: FilePath -> String -> ServerPartT IO Response
6765serveLib libLoc fp = do