From 843f34292b684591370060ca6b4a0b38d91ade65 Mon Sep 17 00:00:00 2001 From: Domantas Petrauskas Date: Thu, 5 May 2022 20:27:50 +0300 Subject: [PATCH] Improve naming in new package manager abstraction, fix scripted test --- ...rnalCommand.scala => PackageManager.scala} | 27 ++++++++++++------- .../sbtplugin/ScalaJSBundlerPlugin.scala | 2 -- .../additonalNpmConfig/build.sbt | 3 ++- 3 files changed, 19 insertions(+), 13 deletions(-) rename sbt-scalajs-bundler/src/main/scala/scalajsbundler/{ExternalCommand.scala => PackageManager.scala} (76%) diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala similarity index 76% rename from sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala rename to sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala index 6abb86ce..2477db6b 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/PackageManager.scala @@ -11,7 +11,7 @@ import scalajsbundler.util.JSON * * @param name Name of the command to run */ -abstract class PackageManager(val name: String, val args: Seq[String], val installCommand: String) { +abstract class PackageManager(val name: String, val installCommand: String, val installArgs: Seq[String]) { /** * Runs the command `cmd` @@ -19,7 +19,7 @@ abstract class PackageManager(val name: String, val args: Seq[String], val insta * @param workingDir Working directory of the process * @param logger Logger */ - def run(args: String*)(workingDir: File, logger: Logger): Unit = + protected def run(args: String*)(workingDir: File, logger: Logger): Unit = Commands.run(cmd ++: args, workingDir, logger) private val cmd = sys.props("os.name").toLowerCase match { @@ -37,7 +37,7 @@ abstract class PackageManager(val name: String, val args: Seq[String], val insta () } - run(installCommand +: args: _*)(installDir, logger) + run(installCommand +: installArgs: _*)(installDir, logger) this match { case lfs: LockFileSupport => @@ -53,6 +53,7 @@ abstract class PackageManager(val name: String, val args: Seq[String], val insta trait AddPackagesSupport { this: PackageManager => val addPackagesCommand: String + val addPackagesArgs: Seq[String] /** * Locally install NPM packages @@ -73,7 +74,7 @@ trait AddPackagesSupport { this: PackageManager => () } - run(addPackagesCommand +: (args ++ npmPackages): _*)(installDir, logger) + run(addPackagesCommand +: (addPackagesArgs ++ npmPackages): _*)(installDir, logger) this match { case lfs: LockFileSupport => @@ -118,9 +119,11 @@ trait LockFileSupport { case class Npm( lockFileName: String = "package-lock.json", - override val args: Seq[String] = Seq.empty, - addPackagesCommand: String = "install" -) extends PackageManager("npm", args, "install") + override val installCommand: String = "install", + override val installArgs: Seq[String] = Seq.empty, + addPackagesCommand: String = "install", + override val addPackagesArgs: Seq[String] = Seq.empty, +) extends PackageManager("npm", "install", installArgs) with LockFileSupport with AddPackagesSupport { override val packageJsonContents: Map[String, JSON] = Map.empty @@ -129,9 +132,13 @@ case class Npm( case class Yarn( version: String, lockFileName: String = "yarn.lock", - override val args: Seq[String] = Yarn.DefaultArgs, - addPackagesCommand: String = "add") extends PackageManager("yarn", args, "install") - with LockFileSupport with AddPackagesSupport { + override val installCommand: String = "install", + override val installArgs: Seq[String] = Yarn.DefaultArgs, + addPackagesCommand: String = "add", + override val addPackagesArgs: Seq[String] = Yarn.DefaultArgs +) extends PackageManager("yarn", "install", installArgs) + with LockFileSupport + with AddPackagesSupport { override val packageJsonContents: Map[String, JSON] = Map("packageManager" -> JSON.str(s"yarn@$version")) } object Yarn { diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala index de8c306f..2a92e51f 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala @@ -6,10 +6,8 @@ import sbt.Keys._ import sbt.{Def, _} import scalajsbundler.AddPackagesSupport import scalajsbundler.{BundlerFile, NpmDependencies, Webpack, WebpackDevServer} -import scalajsbundler.ExternalCommand.addPackages import scalajsbundler.Npm import scalajsbundler.PackageManager -import scalajsbundler.Yarn import scalajsbundler.util.{JSON, ScalaJSNativeLibraries} diff --git a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/additonalNpmConfig/build.sbt b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/additonalNpmConfig/build.sbt index d0929914..7be317ad 100644 --- a/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/additonalNpmConfig/build.sbt +++ b/sbt-scalajs-bundler/src/sbt-test/sbt-scalajs-bundler/additonalNpmConfig/build.sbt @@ -1,4 +1,5 @@ import scalajsbundler.util.JSON._ +import scalajsbundler.Npm val checkPackageJson = taskKey[Unit]("Check that the package.json file does not contain duplicate entries for the 'react' dependency") @@ -8,7 +9,7 @@ lazy val npmConfig = .settings( scalaVersion := "2.13.1", npmDependencies in Compile += "react" -> "16.13.1", - npmExtraArgs in Compile := Seq("-silent"), + packageManager in Compile := Npm(installArgs = Seq("-silent"), addPackagesArgs = Seq("-silent")), additionalNpmConfig in Compile := Map( "name" -> str("foo"), "version" -> str("1.0.0"),