Factor out this function into its own module

We also need to reuse it during watch
This commit is contained in:
Martijn Faassen 2022-02-07 11:56:07 +01:00
parent ae5f498b65
commit 988929fbdc
2 changed files with 41 additions and 28 deletions

View File

@ -0,0 +1,38 @@
module Wasp.Cli.Command.CompileAndSetup (compileAndSetup) where
import Control.Monad.Except (throwError)
import Control.Monad.IO.Class (liftIO)
import StrongPath (Abs, Dir, Path')
import Wasp.Cli.Command (Command, CommandError (..))
import Wasp.Cli.Command.Common
( waspSaysC,
)
import Wasp.Cli.Command.Compile (compileIO)
import qualified Wasp.Cli.Common as Common
import Wasp.Cli.Terminal (asWaspFailureMessage, asWaspStartMessage, asWaspSuccessMessage)
import qualified Wasp.Generator.Common
import qualified Wasp.Lib
compileAndSetup ::
Path' Abs (Dir Common.WaspProjectDir) ->
Path' Abs (Dir Wasp.Generator.Common.ProjectRootDir) ->
Command ()
compileAndSetup waspRoot outDir = do
waspSaysC $ asWaspStartMessage "Compiling wasp code..."
compilationResult <- liftIO $ compileIO waspRoot outDir
case compilationResult of
Left compileError -> throwError $ CommandError $ asWaspFailureMessage "Compilation failed:" ++ compileError
Right () -> waspSaysC $ asWaspSuccessMessage "Code has been successfully compiled, project has been generated."
-- TODO: Do smart npm install -> if we need to install stuff, install it, otherwise don't.
-- This should be responsibility of Generator, it should tell us how to install stuff.
-- But who checks out if stuff needs to be installed at all? That should probably be
-- Generator again. After installation, it should return some kind of data that describes that installation.
-- Then, next time, we give it data we have about last installation, and it uses that
-- to decide if installation needs to happen or not. If it happens, it returnes new data again.
-- Right now we have setup/installation being called, but it has not support for being "smart" yet.
waspSaysC $ asWaspStartMessage "Setting up generated project..."
setupResult <- liftIO $ Wasp.Lib.setup outDir
case setupResult of
Left setupError -> throwError $ CommandError $ asWaspFailureMessage "Setup failed:" ++ setupError
Right () -> waspSaysC $ asWaspSuccessMessage "Setup successful."

View File

@ -6,17 +6,16 @@ where
import Control.Concurrent.Async (race) import Control.Concurrent.Async (race)
import Control.Monad.Except (throwError) import Control.Monad.Except (throwError)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import StrongPath (Abs, Dir, Path', (</>)) import StrongPath ((</>))
import Wasp.Cli.Command (Command, CommandError (..)) import Wasp.Cli.Command (Command, CommandError (..))
import Wasp.Cli.Command.Common import Wasp.Cli.Command.Common
( findWaspProjectRootDirFromCwd, ( findWaspProjectRootDirFromCwd,
waspSaysC, waspSaysC,
) )
import Wasp.Cli.Command.Compile (compileIO) import Wasp.Cli.Command.CompileAndSetup (compileAndSetup)
import Wasp.Cli.Command.Watch (watch) import Wasp.Cli.Command.Watch (watch)
import qualified Wasp.Cli.Common as Common import qualified Wasp.Cli.Common as Common
import Wasp.Cli.Terminal (asWaspFailureMessage, asWaspStartMessage, asWaspSuccessMessage) import Wasp.Cli.Terminal (asWaspFailureMessage, asWaspStartMessage)
import qualified Wasp.Generator.Common
import qualified Wasp.Lib import qualified Wasp.Lib
-- | Does initial compile of wasp code and then runs the generated project. -- | Does initial compile of wasp code and then runs the generated project.
@ -36,27 +35,3 @@ start = do
Right startResult -> case startResult of Right startResult -> case startResult of
Left startError -> throwError $ CommandError $ asWaspFailureMessage "Start failed:" ++ startError Left startError -> throwError $ CommandError $ asWaspFailureMessage "Start failed:" ++ startError
Right () -> error "This should never happen, start should never end but it did." Right () -> error "This should never happen, start should never end but it did."
compileAndSetup ::
Path' Abs (Dir Common.WaspProjectDir) ->
Path' Abs (Dir Wasp.Generator.Common.ProjectRootDir) ->
Command ()
compileAndSetup waspRoot outDir = do
waspSaysC $ asWaspStartMessage "Compiling wasp code..."
compilationResult <- liftIO $ compileIO waspRoot outDir
case compilationResult of
Left compileError -> throwError $ CommandError $ asWaspFailureMessage "Compilation failed:" ++ compileError
Right () -> waspSaysC $ asWaspSuccessMessage "Code has been successfully compiled, project has been generated."
-- TODO: Do smart install -> if we need to install stuff, install it, otherwise don't.
-- This should be responsibility of Generator, it should tell us how to install stuff.
-- But who checks out if stuff needs to be installed at all? That should probably be
-- Generator again. After installation, it should return some kind of data that describes that installation.
-- Then, next time, we give it data we have about last installation, and it uses that
-- to decide if installation needs to happen or not. If it happens, it returnes new data again.
-- Right now we have setup/installation being called, but it has not support for being "smart" yet.
waspSaysC $ asWaspStartMessage "Setting up generated project..."
setupResult <- liftIO $ Wasp.Lib.setup outDir
case setupResult of
Left setupError -> throwError $ CommandError $ asWaspFailureMessage "Setup failed:" ++ setupError
Right () -> waspSaysC $ asWaspSuccessMessage "Setup successful."