Skip to content

Commit

Permalink
Improve naming in new package manager abstraction, fix scripted test
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrdom committed May 5, 2022
1 parent 8dd72a1 commit 843f342
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ 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`
* @param args Command arguments
* @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 {
Expand All @@ -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 =>
Expand All @@ -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
Expand All @@ -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 =>
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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"),
Expand Down

0 comments on commit 843f342

Please sign in to comment.