2020-09-07 22:55:13 +03:00
|
|
|
module Main where
|
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
import Control.Concurrent (threadDelay)
|
|
|
|
import qualified Control.Concurrent.Async as Async
|
2022-03-17 14:36:36 +03:00
|
|
|
import qualified Control.Exception as E
|
2021-04-28 18:36:00 +03:00
|
|
|
import Control.Monad (void)
|
|
|
|
import Data.Char (isSpace)
|
2023-03-21 18:37:20 +03:00
|
|
|
import Data.List (intercalate)
|
2022-06-28 20:48:02 +03:00
|
|
|
import Main.Utf8 (withUtf8)
|
2022-03-23 15:04:09 +03:00
|
|
|
import System.Environment (getArgs)
|
2023-05-26 15:47:58 +03:00
|
|
|
import System.Exit (exitFailure)
|
2021-11-11 15:26:20 +03:00
|
|
|
import Wasp.Cli.Command (runCommand)
|
|
|
|
import Wasp.Cli.Command.BashCompletion (bashCompletion, generateBashCompletionScript, printBashCompletionInstruction)
|
|
|
|
import Wasp.Cli.Command.Build (build)
|
|
|
|
import qualified Wasp.Cli.Command.Call as Command.Call
|
|
|
|
import Wasp.Cli.Command.Clean (clean)
|
|
|
|
import Wasp.Cli.Command.Compile (compile)
|
|
|
|
import Wasp.Cli.Command.CreateNewProject (createNewProject)
|
2023-06-20 00:06:38 +03:00
|
|
|
import qualified Wasp.Cli.Command.CreateNewProject.AI as Command.CreateNewProject.AI
|
2023-03-21 18:37:20 +03:00
|
|
|
import Wasp.Cli.Command.Db (runDbCommand)
|
2021-11-11 15:26:20 +03:00
|
|
|
import qualified Wasp.Cli.Command.Db.Migrate as Command.Db.Migrate
|
2023-03-21 18:37:20 +03:00
|
|
|
import qualified Wasp.Cli.Command.Db.Reset as Command.Db.Reset
|
2023-04-05 12:33:52 +03:00
|
|
|
import qualified Wasp.Cli.Command.Db.Seed as Command.Db.Seed
|
2023-03-21 18:37:20 +03:00
|
|
|
import qualified Wasp.Cli.Command.Db.Studio as Command.Db.Studio
|
2023-02-07 18:29:25 +03:00
|
|
|
import Wasp.Cli.Command.Deploy (deploy)
|
2021-11-11 15:26:20 +03:00
|
|
|
import Wasp.Cli.Command.Deps (deps)
|
2022-10-13 19:46:07 +03:00
|
|
|
import Wasp.Cli.Command.Dockerfile (printDockerfile)
|
2021-11-11 15:26:20 +03:00
|
|
|
import Wasp.Cli.Command.Info (info)
|
|
|
|
import Wasp.Cli.Command.Start (start)
|
2023-03-21 18:37:20 +03:00
|
|
|
import qualified Wasp.Cli.Command.Start.Db as Command.Start.Db
|
2021-11-11 15:26:20 +03:00
|
|
|
import qualified Wasp.Cli.Command.Telemetry as Telemetry
|
2023-03-30 19:22:40 +03:00
|
|
|
import Wasp.Cli.Command.Test (test)
|
2023-02-01 13:18:24 +03:00
|
|
|
import Wasp.Cli.Command.Uninstall (uninstall)
|
2022-09-08 14:56:37 +03:00
|
|
|
import Wasp.Cli.Command.WaspLS (runWaspLS)
|
2023-05-26 15:47:58 +03:00
|
|
|
import Wasp.Cli.Message (cliSendMessage)
|
2021-11-11 15:26:20 +03:00
|
|
|
import Wasp.Cli.Terminal (title)
|
2023-05-26 15:47:58 +03:00
|
|
|
import qualified Wasp.Message as Message
|
2023-06-05 14:52:51 +03:00
|
|
|
import qualified Wasp.Node.Version as NodeVersion
|
2022-03-17 14:36:36 +03:00
|
|
|
import Wasp.Util (indent)
|
2021-11-11 15:26:20 +03:00
|
|
|
import qualified Wasp.Util.Terminal as Term
|
2022-11-23 18:55:12 +03:00
|
|
|
import Wasp.Version (waspVersion)
|
2020-09-07 22:55:13 +03:00
|
|
|
|
|
|
|
main :: IO ()
|
2022-06-28 20:48:02 +03:00
|
|
|
main = withUtf8 . (`E.catch` handleInternalErrors) $ do
|
2021-04-28 18:36:00 +03:00
|
|
|
args <- getArgs
|
|
|
|
let commandCall = case args of
|
2023-05-03 13:57:17 +03:00
|
|
|
("new" : newArgs) -> Command.Call.New newArgs
|
2023-06-28 12:25:09 +03:00
|
|
|
-- new-ai / new-ai:stdout is meant to be called and consumed programatically (e.g. by our Wasp AI
|
|
|
|
-- web app), while new-ai:disk is useful for us for testing.
|
2023-06-29 22:25:23 +03:00
|
|
|
[newAiCmd, projectName, appDescription, projectConfigJson]
|
2023-06-28 12:25:09 +03:00
|
|
|
| newAiCmd `elem` ["new-ai", "new-ai:stdout"] ->
|
2023-06-29 22:25:23 +03:00
|
|
|
Command.Call.NewAiToStdout projectName appDescription projectConfigJson
|
2023-06-28 12:25:09 +03:00
|
|
|
| newAiCmd == "new-ai:disk" ->
|
2023-06-29 22:25:23 +03:00
|
|
|
Command.Call.NewAiToDisk projectName appDescription projectConfigJson
|
2021-04-28 18:36:00 +03:00
|
|
|
["start"] -> Command.Call.Start
|
2023-03-21 18:37:20 +03:00
|
|
|
["start", "db"] -> Command.Call.StartDb
|
2021-04-28 18:36:00 +03:00
|
|
|
["clean"] -> Command.Call.Clean
|
|
|
|
["compile"] -> Command.Call.Compile
|
|
|
|
("db" : dbArgs) -> Command.Call.Db dbArgs
|
2023-02-01 13:18:24 +03:00
|
|
|
["uninstall"] -> Command.Call.Uninstall
|
2021-04-28 18:36:00 +03:00
|
|
|
["version"] -> Command.Call.Version
|
|
|
|
["build"] -> Command.Call.Build
|
|
|
|
["telemetry"] -> Command.Call.Telemetry
|
2021-05-20 15:05:42 +03:00
|
|
|
["deps"] -> Command.Call.Deps
|
2022-10-13 19:46:07 +03:00
|
|
|
["dockerfile"] -> Command.Call.Dockerfile
|
2021-08-23 19:13:04 +03:00
|
|
|
["info"] -> Command.Call.Info
|
2021-06-16 14:40:31 +03:00
|
|
|
["completion"] -> Command.Call.PrintBashCompletionInstruction
|
|
|
|
["completion:generate"] -> Command.Call.GenerateBashCompletionScript
|
2021-09-06 14:09:03 +03:00
|
|
|
["completion:list"] -> Command.Call.BashCompletionListCommands
|
2022-09-08 14:56:37 +03:00
|
|
|
("waspls" : _) -> Command.Call.WaspLS
|
2023-02-07 18:29:25 +03:00
|
|
|
("deploy" : deployArgs) -> Command.Call.Deploy deployArgs
|
2023-03-30 19:22:40 +03:00
|
|
|
("test" : testArgs) -> Command.Call.Test testArgs
|
2021-04-28 18:36:00 +03:00
|
|
|
_ -> Command.Call.Unknown args
|
2021-02-05 16:23:22 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
telemetryThread <- Async.async $ runCommand $ Telemetry.considerSendingData commandCall
|
2021-02-05 16:23:22 +03:00
|
|
|
|
2023-05-26 15:47:58 +03:00
|
|
|
-- Before calling any command, check that the node requirement is met. Node is
|
|
|
|
-- not needed for every command, but checking for every command was decided
|
|
|
|
-- to be more robust than trying to only check for commands that require it.
|
|
|
|
-- See https://github.com/wasp-lang/wasp/issues/1134#issuecomment-1554065668
|
|
|
|
NodeVersion.getAndCheckNodeVersion >>= \case
|
|
|
|
Left errorMsg -> do
|
|
|
|
cliSendMessage $ Message.Failure "Node requirement not met" errorMsg
|
|
|
|
exitFailure
|
|
|
|
Right _ -> pure ()
|
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
case commandCall of
|
2023-05-03 13:57:17 +03:00
|
|
|
Command.Call.New newArgs -> runCommand $ createNewProject newArgs
|
2023-06-29 22:25:23 +03:00
|
|
|
Command.Call.NewAiToStdout projectName appDescription projectConfigJson ->
|
|
|
|
runCommand $ Command.CreateNewProject.AI.createNewProjectNonInteractiveToStdout projectName appDescription projectConfigJson
|
|
|
|
Command.Call.NewAiToDisk projectName appDescription projectConfigJson ->
|
|
|
|
runCommand $ Command.CreateNewProject.AI.createNewProjectNonInteractiveOnDisk projectName appDescription projectConfigJson
|
2021-04-28 18:36:00 +03:00
|
|
|
Command.Call.Start -> runCommand start
|
2023-03-21 18:37:20 +03:00
|
|
|
Command.Call.StartDb -> runCommand Command.Start.Db.start
|
2021-04-28 18:36:00 +03:00
|
|
|
Command.Call.Clean -> runCommand clean
|
|
|
|
Command.Call.Compile -> runCommand compile
|
|
|
|
Command.Call.Db dbArgs -> dbCli dbArgs
|
|
|
|
Command.Call.Version -> printVersion
|
2023-02-01 13:18:24 +03:00
|
|
|
Command.Call.Uninstall -> runCommand uninstall
|
2021-04-28 18:36:00 +03:00
|
|
|
Command.Call.Build -> runCommand build
|
|
|
|
Command.Call.Telemetry -> runCommand Telemetry.telemetry
|
2021-05-20 15:05:42 +03:00
|
|
|
Command.Call.Deps -> runCommand deps
|
2022-10-13 19:46:07 +03:00
|
|
|
Command.Call.Dockerfile -> runCommand printDockerfile
|
2021-08-23 19:13:04 +03:00
|
|
|
Command.Call.Info -> runCommand info
|
2021-11-17 02:36:46 +03:00
|
|
|
Command.Call.PrintBashCompletionInstruction -> runCommand printBashCompletionInstruction
|
|
|
|
Command.Call.GenerateBashCompletionScript -> runCommand generateBashCompletionScript
|
|
|
|
Command.Call.BashCompletionListCommands -> runCommand bashCompletion
|
2021-04-28 18:36:00 +03:00
|
|
|
Command.Call.Unknown _ -> printUsage
|
2022-09-08 14:56:37 +03:00
|
|
|
Command.Call.WaspLS -> runWaspLS
|
2023-02-07 18:29:25 +03:00
|
|
|
Command.Call.Deploy deployArgs -> runCommand $ deploy deployArgs
|
2023-03-30 19:22:40 +03:00
|
|
|
Command.Call.Test testArgs -> runCommand $ test testArgs
|
2021-01-23 00:53:36 +03:00
|
|
|
|
2021-04-28 18:36:00 +03:00
|
|
|
-- If sending of telemetry data is still not done 1 second since commmand finished, abort it.
|
|
|
|
-- We also make sure here to catch all errors that might get thrown and silence them.
|
|
|
|
void $ Async.race (threadDelaySeconds 1) (Async.waitCatch telemetryThread)
|
2021-01-23 00:53:36 +03:00
|
|
|
where
|
2021-04-28 18:36:00 +03:00
|
|
|
threadDelaySeconds =
|
|
|
|
let microsecondsInASecond = 1000000
|
|
|
|
in threadDelay . (* microsecondsInASecond)
|
2020-09-07 22:55:13 +03:00
|
|
|
|
2022-03-17 14:36:36 +03:00
|
|
|
handleInternalErrors :: E.ErrorCall -> IO ()
|
2023-06-23 13:44:43 +03:00
|
|
|
handleInternalErrors e = do
|
|
|
|
putStrLn $ "\nInternal Wasp error (bug in compiler):\n" ++ indent 2 (show e)
|
|
|
|
exitFailure
|
2022-03-17 14:36:36 +03:00
|
|
|
|
2023-05-26 15:47:58 +03:00
|
|
|
{- ORMOLU_DISABLE -}
|
2020-09-07 22:55:13 +03:00
|
|
|
printUsage :: IO ()
|
2021-04-28 18:36:00 +03:00
|
|
|
printUsage =
|
|
|
|
putStrLn $
|
|
|
|
unlines
|
|
|
|
[ title "USAGE",
|
2023-03-21 18:37:20 +03:00
|
|
|
" wasp <command> [command-args]",
|
|
|
|
"",
|
2021-04-28 18:36:00 +03:00
|
|
|
title "COMMANDS",
|
|
|
|
title " GENERAL",
|
2023-05-03 13:57:17 +03:00
|
|
|
cmd " new [<name>] [args] Creates a new Wasp project. Run it without arguments for interactive mode.",
|
2023-04-06 17:15:34 +03:00
|
|
|
" OPTIONS:",
|
|
|
|
" -t|--template <template-name>",
|
|
|
|
" Check out the templates list here: https://github.com/wasp-lang/starters",
|
|
|
|
"",
|
2023-03-21 18:37:20 +03:00
|
|
|
cmd " version Prints current version of CLI.",
|
|
|
|
cmd " waspls Run Wasp Language Server. Add --help to get more info.",
|
|
|
|
cmd " completion Prints help on bash completion.",
|
|
|
|
cmd " uninstall Removes Wasp from your system.",
|
2021-04-28 18:36:00 +03:00
|
|
|
title " IN PROJECT",
|
2023-03-21 18:37:20 +03:00
|
|
|
cmd " start Runs Wasp app in development mode, watching for file changes.",
|
|
|
|
cmd " start db Starts managed development database for you.",
|
|
|
|
cmd " db <db-cmd> [args] Executes a database command. Run 'wasp db' for more info.",
|
2023-03-30 19:22:40 +03:00
|
|
|
cmd " clean Deletes all generated code and other cached artifacts.",
|
2023-03-21 18:37:20 +03:00
|
|
|
" Wasp equivalent of 'have you tried closing and opening it again?'.",
|
|
|
|
cmd " build Generates full web app code, ready for deployment. Use when deploying or ejecting.",
|
|
|
|
cmd " deploy Deploys your Wasp app to cloud hosting providers.",
|
|
|
|
cmd " telemetry Prints telemetry status.",
|
|
|
|
cmd " deps Prints the dependencies that Wasp uses in your project.",
|
|
|
|
cmd " dockerfile Prints the contents of the Wasp generated Dockerfile.",
|
|
|
|
cmd " info Prints basic information about current Wasp project.",
|
2023-03-30 19:22:40 +03:00
|
|
|
cmd " test Executes tests in your project.",
|
2023-03-21 18:37:20 +03:00
|
|
|
"",
|
2021-04-28 18:36:00 +03:00
|
|
|
title "EXAMPLES",
|
2023-03-21 18:37:20 +03:00
|
|
|
" wasp new MyApp",
|
|
|
|
" wasp start",
|
|
|
|
" wasp db migrate-dev",
|
|
|
|
"",
|
|
|
|
Term.applyStyles [Term.Green] "Docs:" ++ " https://wasp-lang.dev/docs",
|
2022-11-26 21:04:49 +03:00
|
|
|
Term.applyStyles [Term.Magenta] "Discord (chat):" ++ " https://discord.gg/rzdnErX",
|
2023-03-21 18:37:20 +03:00
|
|
|
Term.applyStyles [Term.Cyan] "Newsletter:" ++ " https://wasp-lang.dev/#signup"
|
2021-04-28 18:36:00 +03:00
|
|
|
]
|
2023-03-21 18:37:20 +03:00
|
|
|
{- ORMOLU_ENABLE -}
|
2020-09-07 22:55:13 +03:00
|
|
|
|
2020-10-29 15:15:13 +03:00
|
|
|
printVersion :: IO ()
|
2022-11-23 18:55:12 +03:00
|
|
|
printVersion = do
|
|
|
|
putStrLn $
|
|
|
|
unlines
|
|
|
|
[ show waspVersion,
|
|
|
|
"",
|
2022-12-14 13:51:05 +03:00
|
|
|
"If you wish to install/switch to the latest version of Wasp, do:",
|
|
|
|
" curl -sSL https://get.wasp-lang.dev/installer.sh | sh -s",
|
2023-03-21 18:37:20 +03:00
|
|
|
"",
|
|
|
|
"If you want specific x.y.z version of Wasp, do:",
|
2022-11-23 18:55:12 +03:00
|
|
|
" curl -sSL https://get.wasp-lang.dev/installer.sh | sh -s -- -v x.y.z",
|
2022-12-14 13:51:05 +03:00
|
|
|
"",
|
2023-03-21 18:37:20 +03:00
|
|
|
"Check https://github.com/wasp-lang/wasp/releases for the list of valid versions, including the latest one."
|
2022-11-23 18:55:12 +03:00
|
|
|
]
|
2020-10-29 15:15:13 +03:00
|
|
|
|
2020-09-15 17:39:56 +03:00
|
|
|
-- TODO(matija): maybe extract to a separate module, e.g. DbCli.hs?
|
|
|
|
dbCli :: [String] -> IO ()
|
|
|
|
dbCli args = case args of
|
2023-04-12 15:01:54 +03:00
|
|
|
["start"] -> runCommand Command.Start.Db.start
|
2022-12-13 21:55:01 +03:00
|
|
|
"migrate-dev" : optionalMigrateArgs -> runDbCommand $ Command.Db.Migrate.migrateDev optionalMigrateArgs
|
2023-03-21 18:37:20 +03:00
|
|
|
["reset"] -> runDbCommand Command.Db.Reset.reset
|
2023-04-05 12:33:52 +03:00
|
|
|
["seed"] -> runDbCommand $ Command.Db.Seed.seed Nothing
|
|
|
|
["seed", seedName] -> runDbCommand $ Command.Db.Seed.seed $ Just seedName
|
2023-03-21 18:37:20 +03:00
|
|
|
["studio"] -> runDbCommand Command.Db.Studio.studio
|
2021-04-28 18:36:00 +03:00
|
|
|
_ -> printDbUsage
|
2020-09-07 22:55:13 +03:00
|
|
|
|
2023-05-26 15:47:58 +03:00
|
|
|
{- ORMOLU_DISABLE -}
|
2020-09-15 17:39:56 +03:00
|
|
|
printDbUsage :: IO ()
|
2021-04-28 18:36:00 +03:00
|
|
|
printDbUsage =
|
|
|
|
putStrLn $
|
|
|
|
unlines
|
|
|
|
[ title "USAGE",
|
2023-03-21 18:37:20 +03:00
|
|
|
" wasp db <command> [command-args]",
|
|
|
|
"",
|
2021-04-28 18:36:00 +03:00
|
|
|
title "COMMANDS",
|
2023-04-12 15:01:54 +03:00
|
|
|
cmd " start Alias for `wasp start db`.",
|
2023-03-21 18:37:20 +03:00
|
|
|
cmd " reset Drops all data and tables from development database and re-applies all migrations.",
|
2023-04-05 12:33:52 +03:00
|
|
|
cmd " seed [name] Executes a db seed function (specified via app.db.seeds).",
|
2023-04-19 02:14:06 +03:00
|
|
|
" If there are multiple seeds, you can specify a seed to execute by providing its name,",
|
|
|
|
" or if not then you will be asked to provide the name interactively.",
|
2023-03-21 18:37:20 +03:00
|
|
|
cmd $ intercalate "\n" [
|
|
|
|
" migrate-dev Ensures dev database corresponds to the current state of schema(entities):",
|
|
|
|
" - Generates a new migration if there are changes in the schema.",
|
|
|
|
" - Applies any pending migrations to the database either using the",
|
|
|
|
" supplied migration name or asking for one.",
|
|
|
|
" OPTIONS:",
|
|
|
|
" --name [migration-name]",
|
|
|
|
" --create-only"
|
|
|
|
],
|
|
|
|
cmd " studio GUI for inspecting your database.",
|
|
|
|
"",
|
2021-04-28 18:36:00 +03:00
|
|
|
title "EXAMPLES",
|
2023-03-21 18:37:20 +03:00
|
|
|
" wasp db migrate-dev",
|
|
|
|
" wasp db migrate-dev --name \"Added User entity\"",
|
|
|
|
" wasp db migrate-dev --create-only",
|
|
|
|
" wasp db studio"
|
2021-04-28 18:36:00 +03:00
|
|
|
]
|
2023-03-21 18:37:20 +03:00
|
|
|
{- ORMOLU_ENABLE -}
|
2021-02-09 13:19:29 +03:00
|
|
|
|
|
|
|
cmd :: String -> String
|
|
|
|
cmd = mapFirstWord (Term.applyStyles [Term.Yellow, Term.Bold])
|
|
|
|
|
|
|
|
mapFirstWord :: (String -> String) -> String -> String
|
|
|
|
mapFirstWord f s = beforeFirstWord ++ f firstWord ++ afterFirstWord
|
|
|
|
where
|
|
|
|
(beforeFirstWord, firstWordAndAfter) = span isSpace s
|
2021-04-28 18:36:00 +03:00
|
|
|
(firstWord, afterFirstWord) = break isSpace firstWordAndAfter
|