diff --git a/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala b/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala index bba1ef8beb..68c4851db6 100644 --- a/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala +++ b/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala @@ -84,6 +84,10 @@ final case class SharedOptions( @Name("resourceDir") resourceDirs: List[String] = Nil, + @HelpMessage("Specify platform") + @ValueDescription("scala-js|scala-native|jvm") + platform: Option[String] = None, + @Group("Scala") @Hidden scalaLibrary: Option[Boolean] = None, diff --git a/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala b/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala index 38321dd188..9556434aad 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala @@ -156,10 +156,16 @@ object SharedOptionsUtil extends CommandHelpers { jmhVersion: Option[String] = None, ignoreErrors: Boolean = false ): bo.BuildOptions = { - val platformOpt = - if (js.js) Some(Platform.JS) - else if (native.native) Some(Platform.Native) - else None + val platformStr = Platform.normalize(platform.getOrElse("None")) + val platformOpt = Platform.parse(platformStr) match { + case Some(p) => Some(p) + case None => + if (native.native && js.js) Some(Platform.Native) + else if (js.js) Some(Platform.JS) + else if (native.native) Some(Platform.Native) + else None + } + bo.BuildOptions( scalaOptions = bo.ScalaOptions( scalaVersion = scalaVersion diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala index 44073a3e62..cfc5d902fc 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -206,6 +206,23 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String]) } } + test("simple script JS via platform option") { + val message = "Hello" + val inputs = TestInputs( + os.rel / "simple.sc" -> + s"""import scala.scalajs.js + |val console = js.Dynamic.global.console + |val msg = "$message" + |console.log(msg) + |""".stripMargin + ) + inputs.fromRoot { root => + val output = + os.proc(TestUtil.cli, extraOptions, ".", "--platform", "js").call(cwd = root).out.trim() + expect(output == message) + } + } + def platformNl: String = if (Properties.isWin) "\\r\\n" else "\\n" def simpleNativeTests(): Unit = { diff --git a/website/docs/commands/run.md b/website/docs/commands/run.md index 21b33fc6c3..b1f0f97eac 100644 --- a/website/docs/commands/run.md +++ b/website/docs/commands/run.md @@ -135,6 +135,12 @@ Note that this requires `node` to be [installed](/install#scala-js) on your syst scala-cli Hello.scala --js ``` +It is also possible to achieve it using `--platform` option: + +```bash +scala-cli Hello.scala --platform js +``` + See our dedicated [Scala.js guide](../guides/scala-js.md) for more information. ## Scala Native @@ -146,8 +152,27 @@ Note that the [Scala Native requirements](https://scala-native.readthedocs.io/en scala-cli Hello.scala --native -S 2.13.6 ``` +It is also possible to achieve it using `--platform` option: + +```bash +scala-cli Hello.scala --platform native +``` + We have a dedicated [Scala Native guide](../guides/scala-native.md) as well. +## Platform + +The `--platform` option can be used to choose the platform, which should be used to compile and run application. Available platforms are: +* JVM (`jvm`) +* Scala.js (`scala.js` | `scala-js` | `scalajs` | `js`) +* Scala Native (`scala-native` | `scalanative` | `native`) + +Please note that `--platform` option is prioritized over `--js` and `--native` options. For example, the following command will run using Scala Native: + +```bash +scala-cli Hello.scala --js --platform scala-native +``` + ## Scala Scripts Scala CLI can also compile and run Scala scripts: diff --git a/website/docs/reference/cli-options.md b/website/docs/reference/cli-options.md index a987493b16..f9d4688475 100644 --- a/website/docs/reference/cli-options.md +++ b/website/docs/reference/cli-options.md @@ -1260,6 +1260,10 @@ Aliases: `--resource-dir` Add a resource directory +### `--platform` + +Specify platform + ### `--scala-library` [Internal] diff --git a/website/docs/reference/scala-command/cli-options.md b/website/docs/reference/scala-command/cli-options.md index 4799915c3b..f506bf2aa3 100644 --- a/website/docs/reference/scala-command/cli-options.md +++ b/website/docs/reference/scala-command/cli-options.md @@ -670,6 +670,10 @@ Aliases: `--resource-dir` Add a resource directory +### `--platform` + +Specify platform + ### `--scala-library` [Internal]