2020-09-07 22:55:13 +03:00
|
|
|
module Command.CreateNewProject
|
2021-02-12 05:28:15 +03:00
|
|
|
( createNewProject,
|
|
|
|
)
|
|
|
|
where
|
2020-09-07 22:55:13 +03:00
|
|
|
|
2021-02-12 05:28:15 +03:00
|
|
|
import qualified Cli.Common as Common
|
|
|
|
import Command (Command, CommandError (..))
|
2021-02-09 19:29:08 +03:00
|
|
|
import qualified Command.Common
|
2021-02-12 05:28:15 +03:00
|
|
|
import Control.Monad.Except (throwError)
|
|
|
|
import Control.Monad.IO.Class (liftIO)
|
2021-02-09 15:13:51 +03:00
|
|
|
import qualified Data
|
2021-02-12 05:28:15 +03:00
|
|
|
import Data.Char (isLetter)
|
|
|
|
import ExternalCode (SourceExternalCodeDir)
|
2021-07-03 12:00:01 +03:00
|
|
|
import StrongPath (Abs, Dir, File', Path', Rel, reldir, relfile, (</>))
|
2021-02-12 05:28:15 +03:00
|
|
|
import qualified StrongPath as SP
|
|
|
|
import System.Directory (createDirectory, getCurrentDirectory)
|
|
|
|
import qualified System.Directory
|
|
|
|
import qualified System.FilePath as FP
|
|
|
|
import Text.Printf (printf)
|
|
|
|
import qualified Util.Terminal as Term
|
2020-09-07 22:55:13 +03:00
|
|
|
|
2021-02-12 05:28:15 +03:00
|
|
|
newtype ProjectName = ProjectName {_projectName :: String}
|
2020-09-07 22:55:13 +03:00
|
|
|
|
|
|
|
createNewProject :: String -> Command ()
|
2021-02-12 05:28:15 +03:00
|
|
|
createNewProject projectNameStr = do
|
|
|
|
case parseProjectName projectNameStr of
|
|
|
|
Left err -> throwError $ CommandError err
|
|
|
|
Right projectName -> createNewProject' projectName
|
|
|
|
where
|
|
|
|
parseProjectName name =
|
|
|
|
if all isLetter name
|
|
|
|
then Right $ ProjectName name
|
|
|
|
else Left "Please use only letters for project name."
|
|
|
|
|
|
|
|
createNewProject' :: ProjectName -> Command ()
|
|
|
|
createNewProject' (ProjectName projectName) = do
|
|
|
|
absCwd <- liftIO getCurrentDirectory
|
|
|
|
waspProjectDir <- case SP.parseAbsDir $ absCwd FP.</> projectName of
|
|
|
|
Left err ->
|
|
|
|
throwError $
|
|
|
|
CommandError
|
|
|
|
( "Failed to parse absolute path to wasp project dir: "
|
|
|
|
++ show err
|
|
|
|
)
|
|
|
|
Right sp -> return sp
|
|
|
|
liftIO $ do
|
|
|
|
createDirectorySP waspProjectDir
|
|
|
|
writeFileSP (waspProjectDir </> mainWaspFileInWaspProjectDir) mainWaspFileContent
|
|
|
|
writeFileSP (waspProjectDir </> gitignoreFileInWaspProjectDir) gitignoreFileContent
|
|
|
|
writeFileSP
|
|
|
|
(waspProjectDir </> Common.dotWaspRootFileInWaspProjectDir)
|
|
|
|
"File marking the root of Wasp project."
|
|
|
|
|
|
|
|
let extCodeDir = waspProjectDir </> Common.extCodeDirInWaspProjectDir
|
|
|
|
liftIO $ do
|
|
|
|
createDirectorySP extCodeDir
|
|
|
|
dataDir <- Data.getAbsDataDirPath
|
|
|
|
|
|
|
|
let copyTemplateFile' = copyTemplateFile dataDir extCodeDir
|
|
|
|
|
|
|
|
writeFileSP (extCodeDir </> waspignoreFileInExtCodeDir) waspignoreFileContent
|
|
|
|
|
|
|
|
copyTemplateFile'
|
2021-07-03 12:00:01 +03:00
|
|
|
[relfile|new/ext/MainPage.js|]
|
2021-02-12 05:28:15 +03:00
|
|
|
mainPageJsFileInExtCodeDir
|
|
|
|
|
|
|
|
copyTemplateFile'
|
2021-07-03 12:00:01 +03:00
|
|
|
[relfile|new/ext/Main.css|]
|
2021-02-12 05:28:15 +03:00
|
|
|
mainCssFileInExtCodeDir
|
|
|
|
|
|
|
|
copyTemplateFile'
|
2021-07-03 12:00:01 +03:00
|
|
|
[relfile|new/ext/waspLogo.png|]
|
2021-02-12 05:28:15 +03:00
|
|
|
waspLogoFileInExtCodeDir
|
|
|
|
|
|
|
|
liftIO $ do
|
|
|
|
putStrLn $ Term.applyStyles [Term.Green] ("Created new Wasp app in ./" ++ projectName ++ " directory!")
|
|
|
|
putStrLn "To run it, do:"
|
|
|
|
putStrLn ""
|
|
|
|
putStrLn $ Term.applyStyles [Term.Bold] (" cd " ++ projectName)
|
|
|
|
putStrLn $ Term.applyStyles [Term.Bold] " wasp start"
|
|
|
|
putStrLn ""
|
|
|
|
putStrLn Command.Common.alphaWarningMessage
|
2020-09-07 22:55:13 +03:00
|
|
|
where
|
2021-02-12 05:28:15 +03:00
|
|
|
copyTemplateFile ::
|
2021-07-03 12:00:01 +03:00
|
|
|
Path' Abs (Dir Data.DataDir) ->
|
|
|
|
Path' Abs (Dir SourceExternalCodeDir) ->
|
|
|
|
Path' (Rel Common.CliTemplatesDir) File' ->
|
|
|
|
Path' (Rel SourceExternalCodeDir) File' ->
|
2021-02-12 05:28:15 +03:00
|
|
|
IO ()
|
|
|
|
copyTemplateFile dataDir extCodeDir srcTmplFile dstExtDirFile =
|
|
|
|
System.Directory.copyFile
|
2021-07-03 12:00:01 +03:00
|
|
|
(SP.fromAbsFile (dataDir </> cliTemplatesDirInDataDir </> srcTmplFile))
|
|
|
|
(SP.fromAbsFile (extCodeDir </> dstExtDirFile))
|
2021-02-12 05:28:15 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
cliTemplatesDirInDataDir :: Path' (Rel Data.DataDir) (Dir Common.CliTemplatesDir)
|
|
|
|
cliTemplatesDirInDataDir = [reldir|Cli/templates|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
mainWaspFileInWaspProjectDir :: Path' (Rel Common.WaspProjectDir) File'
|
|
|
|
mainWaspFileInWaspProjectDir = [relfile|main.wasp|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
|
|
|
mainWaspFileContent =
|
|
|
|
unlines
|
|
|
|
[ "app %s {" `printf` projectName,
|
|
|
|
" title: \"%s\"" `printf` projectName,
|
|
|
|
"}",
|
|
|
|
"",
|
|
|
|
"route \"/\" -> page Main",
|
|
|
|
"page Main {",
|
|
|
|
" component: import Main from \"@ext/MainPage.js\"",
|
|
|
|
"}"
|
|
|
|
]
|
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
gitignoreFileInWaspProjectDir :: Path' (Rel Common.WaspProjectDir) File'
|
|
|
|
gitignoreFileInWaspProjectDir = [relfile|.gitignore|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
|
|
|
gitignoreFileContent =
|
|
|
|
unlines
|
|
|
|
[ "/.wasp/",
|
|
|
|
"/.env"
|
|
|
|
]
|
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
waspignoreFileInExtCodeDir :: Path' (Rel SourceExternalCodeDir) File'
|
|
|
|
waspignoreFileInExtCodeDir = [relfile|.waspignore|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
|
|
|
waspignoreFileContent =
|
|
|
|
unlines
|
|
|
|
[ "# Ignore editor tmp files",
|
|
|
|
"**/*~",
|
|
|
|
"**/#*#"
|
|
|
|
]
|
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
mainPageJsFileInExtCodeDir :: Path' (Rel SourceExternalCodeDir) File'
|
|
|
|
mainPageJsFileInExtCodeDir = [relfile|MainPage.js|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
mainCssFileInExtCodeDir :: Path' (Rel SourceExternalCodeDir) File'
|
|
|
|
mainCssFileInExtCodeDir = [relfile|Main.css|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
waspLogoFileInExtCodeDir :: Path' (Rel SourceExternalCodeDir) File'
|
|
|
|
waspLogoFileInExtCodeDir = [relfile|waspLogo.png|]
|
2021-02-12 05:28:15 +03:00
|
|
|
|
2021-07-03 12:00:01 +03:00
|
|
|
writeFileSP = writeFile . SP.fromAbsFile
|
|
|
|
createDirectorySP = createDirectory . SP.fromAbsDir
|