@@ -4,13 +4,11 @@ module Wasp.Project.WaspFile
4
4
)
5
5
where
6
6
7
- import Data.List (find , isSuffixOf )
8
7
import StrongPath
9
8
( Abs ,
10
9
Dir ,
11
10
Path' ,
12
11
castFile ,
13
- fromAbsDir ,
14
12
fromRelFile ,
15
13
(</>) ,
16
14
)
@@ -25,27 +23,33 @@ import Wasp.Project.WaspFile.TypeScript (analyzeWaspTsFile)
25
23
import Wasp.Project.WaspFile.WaspLang (analyzeWaspLangFile )
26
24
import qualified Wasp.Psl.Ast.Schema as Psl.Schema
27
25
import qualified Wasp.Util.IO as IOUtil
26
+ import Wasp.Util.StrongPath (findAllFilesWithSuffix )
28
27
29
28
findWaspFile :: Path' Abs (Dir WaspProjectDir ) -> IO (Either String WaspFilePath )
30
- findWaspFile waspDir = do
31
- files <- fst <$> IOUtil. listDirectory waspDir
32
- return $ case (findWaspTsFile files, findWaspLangFile files) of
33
- (Just _, Just _) -> Left bothFilesFoundMessage
34
- (Nothing , Nothing ) -> Left fileNotFoundMessage
35
- (Just waspTsFile, Nothing ) -> Right waspTsFile
36
- (Nothing , Just waspLangFile) -> Right waspLangFile
29
+ findWaspFile projectDir = do
30
+ filesInProjectDir <- fst <$> IOUtil. listDirectory projectDir
31
+ let filesEndingWithWasp = findAllFilesWithSuffix " .wasp" filesInProjectDir
32
+ filesEndingWithWaspTs = findAllFilesWithSuffix " .wasp.ts" filesInProjectDir
33
+ return $ case (filesEndingWithWasp, filesEndingWithWaspTs) of
34
+ ([] , [] ) -> Left fileNotFoundMessage
35
+ (_ : _, _ : _) -> Left bothFilesFoundMessage
36
+ ([] , waspTsFiles) -> case waspTsFiles of
37
+ [singleWaspTsFile]
38
+ | fromRelFile singleWaspTsFile == " .wasp.ts" -> Left (makeInvalidFileNameMessage " .wasp.ts" )
39
+ | otherwise -> Right . WaspTs $ castFile (projectDir </> singleWaspTsFile)
40
+ multipleWaspTsFiles -> Left (makeMultipleFilesMessage " *.wasp.ts" (map fromRelFile multipleWaspTsFiles))
41
+ (waspLangFiles, [] ) -> case waspLangFiles of
42
+ [singleWaspLangFile]
43
+ | fromRelFile singleWaspLangFile == " .wasp" -> Left (makeInvalidFileNameMessage " .wasp" )
44
+ | otherwise -> Right . WaspLang $ castFile (projectDir </> singleWaspLangFile)
45
+ multipleWaspFiles -> Left (makeMultipleFilesMessage " *.wasp" (map fromRelFile multipleWaspFiles))
37
46
where
38
- findWaspTsFile files = WaspTs <$> findFileThatEndsWith " .wasp.ts" files
39
- findWaspLangFile files = WaspLang <$> findFileThatEndsWith " .wasp" files
40
- findFileThatEndsWith suffix files =
41
- castFile
42
- . (waspDir </> )
43
- <$> find ((suffix `isSuffixOf` ) . fromRelFile) files
44
-
45
- fileNotFoundMessage = " Couldn't find a *.wasp or a *.wasp.ts file in directory " ++ fromAbsDir waspDir ++ " ."
47
+ fileNotFoundMessage = " Couldn't find the *.wasp or a *.wasp.ts file in the project directory."
46
48
bothFilesFoundMessage =
47
49
" Found both *.wasp and *.wasp.ts files in the project directory. "
48
50
++ " You must choose how you want to define your app (using Wasp or TypeScript) and only keep one of them."
51
+ makeMultipleFilesMessage suffix files = " Found multiple " ++ suffix ++ " files in the project directory: " ++ show files ++ " . Please keep only one."
52
+ makeInvalidFileNameMessage suffix = " Your Wasp file can't be called '" ++ suffix ++ " '. Please rename it to something like [name]" ++ suffix ++ " ."
49
53
50
54
analyzeWaspFile ::
51
55
Path' Abs (Dir WaspProjectDir ) ->
0 commit comments