Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure users don't create a .wasp file #2418

Merged
merged 13 commits into from
Feb 12, 2025
2 changes: 2 additions & 0 deletions waspc/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Read more about breaking changes in the migration guide: https://wasp-lang.dev/d

Big thanks to our community members who contributed to this release! @Bojun-Feng @dabrorius @komyg @NathanaelA @vblazenka @genyus

- Improved the error message when the user has a top level *.wasp* file.

## 0.15.2

### 🐞 Bug fixes
Expand Down
38 changes: 21 additions & 17 deletions waspc/src/Wasp/Project/WaspFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ module Wasp.Project.WaspFile
)
where

import Data.List (find, isSuffixOf)
import StrongPath
( Abs,
Dir,
Path',
castFile,
fromAbsDir,
fromRelFile,
(</>),
)
Expand All @@ -25,27 +23,33 @@ import Wasp.Project.WaspFile.TypeScript (analyzeWaspTsFile)
import Wasp.Project.WaspFile.WaspLang (analyzeWaspLangFile)
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
import qualified Wasp.Util.IO as IOUtil
import Wasp.Util.StrongPath (findAllFilesWithSuffix)

findWaspFile :: Path' Abs (Dir WaspProjectDir) -> IO (Either String WaspFilePath)
findWaspFile waspDir = do
files <- fst <$> IOUtil.listDirectory waspDir
return $ case (findWaspTsFile files, findWaspLangFile files) of
(Just _, Just _) -> Left bothFilesFoundMessage
(Nothing, Nothing) -> Left fileNotFoundMessage
(Just waspTsFile, Nothing) -> Right waspTsFile
(Nothing, Just waspLangFile) -> Right waspLangFile
findWaspFile projectDir = do
filesInProjectDir <- fst <$> IOUtil.listDirectory projectDir
let filesEndingWithWasp = findAllFilesWithSuffix ".wasp" filesInProjectDir
filesEndingWithWaspTs = findAllFilesWithSuffix ".wasp.ts" filesInProjectDir
return $ case (filesEndingWithWasp, filesEndingWithWaspTs) of
([], []) -> Left fileNotFoundMessage
(_ : _, _ : _) -> Left bothFilesFoundMessage
([], waspTsFiles) -> case waspTsFiles of
[singleWaspTsFile]
| fromRelFile singleWaspTsFile == ".wasp.ts" -> Left (makeInvalidFileNameMessage ".wasp.ts")
| otherwise -> Right . WaspTs $ castFile (projectDir </> singleWaspTsFile)
multipleWaspTsFiles -> Left (makeMultipleFilesMessage "*.wasp.ts" (map fromRelFile multipleWaspTsFiles))
(waspLangFiles, []) -> case waspLangFiles of
[singleWaspLangFile]
| fromRelFile singleWaspLangFile == ".wasp" -> Left (makeInvalidFileNameMessage ".wasp")
| otherwise -> Right . WaspLang $ castFile (projectDir </> singleWaspLangFile)
multipleWaspFiles -> Left (makeMultipleFilesMessage "*.wasp" (map fromRelFile multipleWaspFiles))
where
findWaspTsFile files = WaspTs <$> findFileThatEndsWith ".wasp.ts" files
findWaspLangFile files = WaspLang <$> findFileThatEndsWith ".wasp" files
findFileThatEndsWith suffix files =
castFile
. (waspDir </>)
<$> find ((suffix `isSuffixOf`) . fromRelFile) files

fileNotFoundMessage = "Couldn't find a *.wasp or a *.wasp.ts file in directory " ++ fromAbsDir waspDir ++ " ."
fileNotFoundMessage = "Couldn't find the *.wasp or a *.wasp.ts file in the project directory."
bothFilesFoundMessage =
"Found both *.wasp and *.wasp.ts files in the project directory. "
++ "You must choose how you want to define your app (using Wasp or TypeScript) and only keep one of them."
makeMultipleFilesMessage suffix files = "Found multiple " ++ suffix ++ " files in the project directory: " ++ show files ++ ". Please keep only one."
makeInvalidFileNameMessage suffix = "Your Wasp file can't be called '" ++ suffix ++ "'. Please rename it to something like [name]" ++ suffix ++ "."

analyzeWaspFile ::
Path' Abs (Dir WaspProjectDir) ->
Expand Down
5 changes: 5 additions & 0 deletions waspc/src/Wasp/Util/StrongPath.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ module Wasp.Util.StrongPath
stripProperPrefix,
splitAbsExtension,
splitRelExtension,
findAllFilesWithSuffix,
)
where

import Control.Arrow (first)
import Control.Monad.Catch (MonadThrow)
import Data.List (isSuffixOf)
import qualified Path as P
import qualified StrongPath as SP
import qualified StrongPath.Path as SP
Expand All @@ -33,3 +35,6 @@ splitAbsExtension path =
splitRelExtension :: MonadThrow m => SP.Path' (SP.Rel b) (SP.File a) -> m (SP.Path' (SP.Rel b) (SP.File c), String)
splitRelExtension path =
first SP.fromPathRelFile <$> P.splitExtension (SP.toPathRelFile path)

findAllFilesWithSuffix :: String -> [SP.Path p r (SP.File f)] -> [SP.Path p r (SP.File f)]
findAllFilesWithSuffix extension = filter ((extension `isSuffixOf`) . SP.toFilePath)
Loading