Skip to content

Commit

Permalink
Improve TS SDK setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sodic committed Oct 9, 2024
1 parent eb44164 commit a91b88a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions waspc/cli/src/Wasp/Cli/Command/TsConfigSetup.hs
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
module Wasp.Cli.Command.TsConfigSetup (tsConfigSetup) where

import Control.Concurrent (newChan)
import Control.Concurrent (Chan, newChan)
import Control.Concurrent.Async (concurrently)
import Control.Monad.Except (throwError)
import Control.Monad.IO.Class (liftIO)
import Wasp.Cli.Command (Command, require)
import StrongPath (Abs, Dir, Path')
import System.Exit (ExitCode (..))
import Wasp.Cli.Command (Command, CommandError (..), require)
import Wasp.Cli.Command.Require (InWaspProject (InWaspProject))
import Wasp.Generator.NpmInstall (installProjectNpmDependencies)
import qualified Wasp.Generator.Job as J
import Wasp.Generator.Job.IO (readJobMessagesAndPrintThemPrefixed)
import Wasp.Generator.Job.Process (runNodeCommandAsJob)
import Wasp.NodePackageFFI (InstallablePackage (WaspConfigPackage), getPackageInstallationPath)

-- | Prepares the project for using Wasp's TypeScript SDK.
tsConfigSetup :: Command ()
tsConfigSetup = do
InWaspProject waspProjectDir <- require
messageChan <- liftIO newChan
-- TODO: Both of these should be eaiser when Miho finishes the package.json
-- and tsconfig.json validation:
-- - Edit package.json to contain the SDK package
-- - Adapt TSconfigs to fit with the TS SDK project structure
liftIO $
-- NOTE: We're only installing the user's package.json dependencies here
-- This is to provide proper IDE support for users working with the TS SDK
-- (it needs the `wasp-config` package).
-- Calling this function here shouldn't break anything for later
-- installations.
-- TODO: What about doing this during Wasp start? Can we make Wasp start
-- pick up whether the user wants to use the TS SDK automatically?
installProjectNpmDependencies messageChan waspProjectDir >>= \case
Left e -> putStrLn $ "npm install failed: " ++ show e
Right _ -> return ()
-- NOTE: We're also installing the user's package.json dependencies here
-- This is to provide proper IDE support for users working with the TS SDK
-- (it needs the `wasp-config` package).
liftIO (installWaspConfigPackage messageChan waspProjectDir)
>>= onLeftThrowError
where
onLeftThrowError = either (throwError . CommandError "npm install failed") pure

installWaspConfigPackage :: Chan J.JobMessage -> Path' Abs (Dir a) -> IO (Either String ())
installWaspConfigPackage chan projectDir = do
installationPath <- getPackageInstallationPath WaspConfigPackage
(_, exitCode) <-
concurrently
(readJobMessagesAndPrintThemPrefixed chan)
(runNodeCommandAsJob projectDir "npm" ["install", "--save-dev", "file:" ++ installationPath] J.Wasp chan)
return $ case exitCode of
ExitSuccess -> Right ()
ExitFailure _ -> Left "Failed to install wasp-config package"

0 comments on commit a91b88a

Please sign in to comment.