From 67c6816cb813e9dd7be8bec36a10834dc0585d7c Mon Sep 17 00:00:00 2001 From: "M. Ian Graham" Date: Sun, 16 Jun 2019 15:12:38 +0200 Subject: [PATCH] Add option to pass arguments to node on test/run (#267) --- CHANGELOG.md | 1 + README.md | 3 +++ app/Spago.hs | 13 +++++++------ src/Spago/Build.hs | 10 ++++++---- 4 files changed, 17 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05b0587ba..f360470ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ Bugfixes: New features: - Add option to clear the screen to spago build/run (#209) +- Add option to pass args to node when doing spago test/run (#267) Bugfixes: - Produce an error message when asserting directory permissions (#250) diff --git a/README.md b/README.md index 8bb0bc8f0..29d975519 100644 --- a/README.md +++ b/README.md @@ -301,6 +301,9 @@ $ spago run --main ModulePath.To.Main # And pass arguments through to `purs compile` $ spago run --main ModulePath.To.Main -- --verbose-errors + +# Or pass arguments to node +$ spago run --node-args "arg1 arg2" ``` diff --git a/app/Spago.hs b/app/Spago.hs index 8099e3e89..5dfa19bce 100644 --- a/app/Spago.hs +++ b/app/Spago.hs @@ -55,10 +55,10 @@ data Command | VerifySet (Maybe Int) (Maybe CacheFlag) -- | Test the project with some module, default Test.Main - | Test (Maybe ModuleName) BuildOptions + | Test (Maybe ModuleName) BuildOptions [ExtraArg] -- | Run the project with some module, default Main - | Run (Maybe ModuleName) BuildOptions + | Run (Maybe ModuleName) BuildOptions [ExtraArg] -- | Bundle the project into an executable -- Builds the project before bundling @@ -116,6 +116,7 @@ parser = do watchBool = CLI.switch "watch" 'w' "Watch for changes in local files and automatically rebuild" clearScreenBool = CLI.switch "clear-screen" 'l' "Clear the screen on rebuild (watch mode only)" noBuildBool = CLI.switch "no-build" 's' "Skip build step" + nodeArgs = CLI.many $ CLI.opt (Just . ExtraArg) "node-args" 'n' "Argument to pass to node (run/test only)" watch = do res <- watchBool pure $ case res of @@ -191,13 +192,13 @@ parser = do test = ( "test" , "Test the project with some module, default Test.Main" - , Test <$> mainModule <*> buildOptions + , Test <$> mainModule <*> buildOptions <*> nodeArgs ) run = ( "run" , "Runs the project with some module, default Main" - , Run <$> mainModule <*> buildOptions + , Run <$> mainModule <*> buildOptions <*> nodeArgs ) bundleApp = @@ -342,8 +343,8 @@ main = do PackageSetUpgrade -> Spago.Packages.upgradePackageSet Freeze -> Spago.Packages.freeze Build buildOptions -> Spago.Build.build buildOptions Nothing - Test modName buildOptions -> Spago.Build.test modName buildOptions - Run modName buildOptions -> Spago.Build.run modName buildOptions + Test modName buildOptions nodeArgs -> Spago.Build.test modName buildOptions nodeArgs + Run modName buildOptions nodeArgs -> Spago.Build.run modName buildOptions nodeArgs Repl paths pursArgs -> Spago.Build.repl paths pursArgs BundleApp modName tPath shouldBuild buildOptions -> Spago.Build.bundleApp WithMain modName tPath shouldBuild buildOptions diff --git a/src/Spago/Build.hs b/src/Spago/Build.hs index 14fa3fdbf..191f23bcf 100644 --- a/src/Spago/Build.hs +++ b/src/Spago/Build.hs @@ -87,12 +87,12 @@ repl sourcePaths passthroughArgs = do -- | Test the project: compile and run "Test.Main" -- (or the provided module name) with node -test :: Spago m => Maybe Purs.ModuleName -> BuildOptions -> m () +test :: Spago m => Maybe Purs.ModuleName -> BuildOptions -> [Purs.ExtraArg] -> m () test = runWithNode (Purs.ModuleName "Test.Main") (Just "Tests succeeded.") "Tests failed: " -- | Run the project: compile and run "Main" -- (or the provided module name) with node -run :: Spago m => Maybe Purs.ModuleName -> BuildOptions -> m () +run :: Spago m => Maybe Purs.ModuleName -> BuildOptions -> [Purs.ExtraArg] -> m () run = runWithNode (Purs.ModuleName "Main") Nothing "Running failed, exit code: " -- | Run the project with node: compile and run with the provided ModuleName @@ -104,13 +104,15 @@ runWithNode -> Text -> Maybe Purs.ModuleName -> BuildOptions + -> [Purs.ExtraArg] -> m () -runWithNode defaultModuleName maybeSuccessMessage failureMessage maybeModuleName buildOpts = do +runWithNode defaultModuleName maybeSuccessMessage failureMessage maybeModuleName buildOpts nodeArgs = do echoDebug "Running NodeJS" build buildOpts (Just nodeAction) where moduleName = fromMaybe defaultModuleName maybeModuleName - cmd = "node -e \"require('./output/" <> Purs.unModuleName moduleName <> "').main()\"" + args = Text.intercalate " " $ map Purs.unExtraArg nodeArgs + cmd = "node -e \"require('./output/" <> Purs.unModuleName moduleName <> "').main()\" " <> args nodeAction = do shell cmd empty >>= \case ExitSuccess -> fromMaybe (pure ()) (echo <$> maybeSuccessMessage)