Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrdom committed May 4, 2022
1 parent b370241 commit 0245694
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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): _*)(
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down

0 comments on commit 0245694

Please sign in to comment.