From 02456943acdf25b204830c1ec2e91fa92ec52980 Mon Sep 17 00:00:00 2001 From: Domantas Petrauskas Date: Wed, 4 May 2022 21:23:45 +0300 Subject: [PATCH] WIP --- .../scalajsbundler/ExternalCommand.scala | 69 ++++++++++--------- .../sbtplugin/ScalaJSBundlerPlugin.scala | 48 ++----------- 2 files changed, 42 insertions(+), 75 deletions(-) diff --git a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala index 885483c6..e794e84b 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/ExternalCommand.scala @@ -10,7 +10,7 @@ import scalajsbundler.util.Commands * * @param name Name of the command to run */ -class ExternalCommand(name: String) { +abstract class PackageManager(name: String, args: Seq[String]) { /** * Runs the command `cmd` @@ -28,20 +28,13 @@ class ExternalCommand(name: String) { } -object Npm extends ExternalCommand("npm") +trait LockFileSupport { + val lockFileName: String -object Yarn extends ExternalCommand("yarn") - -object ExternalCommand { - private val yarnOptions = List("--non-interactive", "--mutex", "network") - - private def syncLockfile( - lockFileName: String, + def lockFileRead( baseDir: File, installDir: File, logger: Logger - )( - command: => Unit ): Unit = { val sourceLockFile = baseDir / lockFileName val targetLockFile = installDir / lockFileName @@ -50,34 +43,39 @@ object ExternalCommand { logger.info("Using lockfile " + sourceLockFile) IO.copyFile(sourceLockFile, targetLockFile) } + } - command + def lockFileWrite( + baseDir: File, + installDir: File, + logger: Logger + ): Unit = { + val sourceLockFile = baseDir / lockFileName + val targetLockFile = installDir / lockFileName if (targetLockFile.exists()) { logger.debug("Wrote lockfile to " + sourceLockFile) IO.copyFile(targetLockFile, sourceLockFile) } } +} - private def syncYarnLockfile( - baseDir: File, - installDir: File, - logger: Logger - )( - command: => Unit - ): Unit = { - syncLockfile("yarn.lock", baseDir, installDir, logger)(command) - } +case class Npm( + args: Seq[String] = Seq.empty, + lockFileName: String = "package-lock.json" +) extends PackageManager("npm", args) + with LockFileSupport + +case class Yarn( + args: Seq[String] = Yarn.DefaultArgs, + lockFileName: String = "yarn.lock" +) extends PackageManager("yarn", args) + with LockFileSupport +object Yarn { + val DefaultArgs: Seq[String] = Seq("--non-interactive", "--mutex", "network") +} - private def syncNpmLockfile( - baseDir: File, - installDir: File, - logger: Logger - )( - command: => Unit - ): Unit = { - syncLockfile("package-lock.json", baseDir, installDir, logger)(command) - } +object PackageManager { /** * Locally install NPM packages @@ -91,10 +89,13 @@ object ExternalCommand { */ def addPackages(baseDir: File, installDir: File, - useYarn: Boolean, logger: Logger, - npmExtraArgs: Seq[String], - yarnExtraArgs: Seq[String])(npmPackages: String*): Unit = + packageManager: PackageManager + )(npmPackages: String*): Unit = { + val maybeLockFileWrapper = packageManager match { + case lfs: LockFileSupport => () => + case _ => + } if (useYarn) { syncYarnLockfile(baseDir, installDir, logger) { Yarn.run("add" +: (yarnOptions ++ yarnExtraArgs ++ npmPackages): _*)( @@ -106,10 +107,10 @@ object ExternalCommand { Npm.run("install" +: (npmPackages ++ npmExtraArgs): _*)(installDir, logger) } } + } def install(baseDir: File, installDir: File, - useYarn: Boolean, logger: Logger, npmExtraArgs: Seq[String], yarnExtraArgs: Seq[String]): Unit = 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 fdda11e5..ea3c5761 100644 --- a/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala +++ b/sbt-scalajs-bundler/src/main/scala/scalajsbundler/sbtplugin/ScalaJSBundlerPlugin.scala @@ -5,8 +5,9 @@ import org.scalajs.sbtplugin.{ScalaJSPlugin, Stage} import sbt.Keys._ import sbt.{Def, _} import scalajsbundler.{BundlerFile, NpmDependencies, Webpack, WebpackDevServer} - import scalajsbundler.ExternalCommand.addPackages +import scalajsbundler.Npm +import scalajsbundler.PackageManager import scalajsbundler.util.{JSON, ScalaJSNativeLibraries} @@ -205,18 +206,6 @@ object ScalaJSBundlerPlugin extends AutoPlugin { val additionalNpmConfig: SettingKey[Map[String, JSON]] = settingKey[Map[String, JSON]]("Additional option to include in the generated 'package.json'") - /** - * Additional arguments for npm - * - * Defaults to an empty list. - * - * @group settings - */ - val npmExtraArgs = SettingKey[Seq[String]]( - "npmExtraArgs", - "Custom arguments for npm" - ) - /** * [[scalajsbundler.BundlingMode]] to use. * @@ -394,19 +383,10 @@ object ScalaJSBundlerPlugin extends AutoPlugin { "Custom arguments to node.js when running webpack tasks" ) - /** - * Whether to use [[https://yarnpkg.com/ Yarn]] to fetch dependencies instead - * of `npm`. Yarn has a caching mechanism that makes the process faster. - * - * If set to `true`, it requires Yarn 0.22.0+ to be available on the - * host platform. - * - * Defaults to `false`. - * - * @group settings - */ - val useYarn: SettingKey[Boolean] = - settingKey[Boolean]("Whether to use yarn for updates") + val packageManager = SettingKey[PackageManager]( + "packageManager", + "Package manager which will be used for fetching dependencies. Constructor also allows definition of extra arguments." + ) /** * Port, on which webpack-dev-server will be launched. @@ -421,17 +401,6 @@ object ScalaJSBundlerPlugin extends AutoPlugin { "Port, on which webpack-dev-server operates" ) - /** - * Additional arguments for yarn - * - * Defaults to an empty list. - * - * @group settings - */ - val yarnExtraArgs = SettingKey[Seq[String]]( - "yarnExtraArgs", - "Custom arguments for yarn" - ) /** * Additional arguments to webpack-dev-server. * @@ -568,7 +537,7 @@ object ScalaJSBundlerPlugin extends AutoPlugin { // Include the manifest in the produced artifact (products in Compile) := (products in Compile).dependsOn(scalaJSBundlerManifest).value, - useYarn := false, + packageManager := Npm(), ensureModuleKindIsCommonJSModule := { if (scalaJSLinkerConfig.value.moduleKind == ModuleKind.CommonJSModule) true @@ -585,9 +554,6 @@ object ScalaJSBundlerPlugin extends AutoPlugin { webpackExtraArgs := Seq.empty, webpackNodeArgs := Seq.empty, - npmExtraArgs := Seq.empty, - yarnExtraArgs := Seq.empty, - // The defaults are specified at top level. webpackDevServerPort := 8080, webpackDevServerExtraArgs := Seq(),