Skip to content

Commit

Permalink
Properly parse decls.json
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Sep 26, 2024
1 parent 115dce5 commit 2617b6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
29 changes: 14 additions & 15 deletions waspc/src/Wasp/Project/Analyze.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import Control.Arrow (ArrowChoice (left))
import Control.Concurrent (newChan)
import Control.Concurrent.Async (concurrently)
import Control.Monad.Except (ExceptT (..), runExceptT)
import Control.Monad.IO.Class (liftIO)
import qualified Data.Aeson as Aeson
import Data.Conduit.Process.Typed (ExitCode (..))
import Data.List (find, isSuffixOf)
Expand All @@ -25,6 +24,7 @@ import qualified Wasp.Analyzer as Analyzer
import Wasp.Analyzer.AnalyzeError (getErrorMessageAndCtx)
import Wasp.Analyzer.Parser.Ctx (Ctx)
import qualified Wasp.AppSpec as AS
import Wasp.AppSpec.Core.Decl.JSON ()
import Wasp.AppSpec.PackageJson (PackageJson)
import qualified Wasp.AppSpec.Valid as ASV
import Wasp.CompileOptions (CompileOptions)
Expand Down Expand Up @@ -94,7 +94,7 @@ data WaspTsFile

data CompiledWaspJsFile

data SpecJsonFile
data DeclsJsonFile

analyzeWaspFile :: Path' Abs (Dir WaspProjectDir) -> Psl.Schema.Schema -> WaspFile -> IO (Either [CompileError] [AS.Decl])
analyzeWaspFile waspDir prismaSchemaAst = \case
Expand All @@ -106,11 +106,8 @@ analyzeWaspTsFile waspProjectDir prismaSchemaAst waspFilePath = runExceptT $ do
-- TODO: I'm not yet sure where tsconfig.node.json location should come from
-- because we also need that knowledge when generating a TS SDK project.
compiledWaspJsFile <- ExceptT $ compileWaspTsFile waspProjectDir [relfile|tsconfig.node.json|] waspFilePath
specJsonFile <- ExceptT $ executeMainWaspJsFile waspProjectDir prismaSchemaAst compiledWaspJsFile
contents <- ExceptT $ readDeclsJsonFile specJsonFile
liftIO $ putStrLn "Here are the contents of the spec file:"
liftIO $ print contents
return []
declsJsonFile <- ExceptT $ executeMainWaspJsFile waspProjectDir prismaSchemaAst compiledWaspJsFile
ExceptT $ readDeclsFromJsonFile declsJsonFile

compileWaspTsFile ::
Path' Abs (Dir WaspProjectDir) ->
Expand Down Expand Up @@ -150,7 +147,7 @@ executeMainWaspJsFile ::
Path' Abs (Dir WaspProjectDir) ->
Psl.Schema.Schema ->
Path' Abs (File CompiledWaspJsFile) ->
IO (Either [CompileError] (Path' Abs (File SpecJsonFile)))
IO (Either [CompileError] (Path' Abs (File DeclsJsonFile)))
executeMainWaspJsFile waspProjectDir prismaSchemaAst absCompiledMainWaspJsFile = do
chan <- newChan
(_, runExitCode) <- do
Expand All @@ -166,23 +163,25 @@ executeMainWaspJsFile waspProjectDir prismaSchemaAst absCompiledMainWaspJsFile =
-- too: waspProjectDir </> [relfile|node_modules/wasp-config/dist/run.js|]
[ "wasp-config",
SP.fromAbsFile absCompiledMainWaspJsFile,
SP.fromAbsFile absSpecOutputFile,
SP.fromAbsFile absDeclsOutputFile,
encodeToString allowedEntityNames
]
J.Wasp
chan
)
case runExitCode of
ExitFailure _status -> return $ Left ["Error while running the compiled *.wasp.mts file."]
ExitSuccess -> return $ Right absSpecOutputFile
ExitSuccess -> return $ Right absDeclsOutputFile
where
absSpecOutputFile = waspProjectDir </> dotWaspDirInWaspProjectDir </> [relfile|spec.json|]
absDeclsOutputFile = waspProjectDir </> dotWaspDirInWaspProjectDir </> [relfile|decls.json|]
allowedEntityNames = Psl.Schema.getModelNames prismaSchemaAst

readDeclsJsonFile :: Path' Abs (File SpecJsonFile) -> IO (Either [CompileError] Aeson.Value)
readDeclsJsonFile declsJsonFile = do
byteString <- IOUtil.readFile declsJsonFile
return $ Right $ Aeson.toJSON byteString
readDeclsFromJsonFile :: Path' Abs (File DeclsJsonFile) -> IO (Either [CompileError] [AS.Decl])
readDeclsFromJsonFile declsJsonFile = do
declsBytestring <- IOUtil.readFileBytes declsJsonFile
case Aeson.eitherDecode declsBytestring of
Right value -> return $ Right value
Left err -> return $ Left ["Error while parsing the declarations from JSON: " ++ err]

analyzeWaspLangFile :: Psl.Schema.Schema -> Path' Abs (File WaspLangFile) -> IO (Either [CompileError] [AS.Decl])
analyzeWaspLangFile prismaSchemaAst waspFilePath = do
Expand Down
10 changes: 0 additions & 10 deletions waspc/test/AppSpec/FromJSONTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ spec_AppSpecFromJSON = do
}
|]
`shouldDecodeTo` Just (Ref.Ref "foo" :: Ref Query)
it "fails to parse an invalid declType" $ do
[trimming|
{
"name": "foo",
"declType": "IMadeThisUp"
}
|]
-- NOTE: We are using `Ref Entity` here, because the Show instance in
-- shouldDecodeTo demands a proper type.
`shouldDecodeTo` (Nothing :: Maybe (Ref Entity))
describe "Query" $ do
it "parses a valid Query JSON with auth and entities" $ do
[trimming|
Expand Down

0 comments on commit 2617b6f

Please sign in to comment.