-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
94 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,14 @@ import java.io.File | |
|
||
import sbt._ | ||
import scalajsbundler.util.Commands | ||
import scalajsbundler.util.JSON | ||
|
||
/** | ||
* Attempts to smoothen platform-specific differences when invoking commands. | ||
* | ||
* @param name Name of the command to run | ||
*/ | ||
abstract class PackageManager(name: String, args: Seq[String]) { | ||
abstract class PackageManager(val name: String, val args: Seq[String], val installCommand: String) { | ||
|
||
/** | ||
* Runs the command `cmd` | ||
|
@@ -26,6 +27,61 @@ abstract class PackageManager(name: String, args: Seq[String]) { | |
case _ => Seq(name) | ||
} | ||
|
||
def install(baseDir: File, | ||
installDir: File, | ||
logger: Logger): Unit = { | ||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileRead(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
|
||
run(installCommand +: args: _*)(installDir, logger) | ||
|
||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileWrite(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
} | ||
|
||
val packageJsonContents: Map[String, JSON] | ||
} | ||
|
||
trait AddPackagesSupport { this: PackageManager => | ||
|
||
val addPackagesCommand: String | ||
|
||
/** | ||
* Locally install NPM packages | ||
* | ||
* @param baseDir The (sub-)project directory which contains yarn.lock | ||
* @param installDir The directory in which to install the packages | ||
* @param logger sbt logger | ||
* @param npmPackages Packages to install (e.g. "webpack", "[email protected]") | ||
*/ | ||
def addPackages(baseDir: File, | ||
installDir: File, | ||
logger: Logger, | ||
)(npmPackages: String*): Unit = { | ||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileRead(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
|
||
run(addPackagesCommand +: (args ++ npmPackages): _*)(installDir, logger) | ||
|
||
this match { | ||
case lfs: LockFileSupport => | ||
lfs.lockFileWrite(baseDir, installDir, logger) | ||
case _ => | ||
() | ||
} | ||
} | ||
} | ||
|
||
trait LockFileSupport { | ||
|
@@ -61,67 +117,23 @@ trait LockFileSupport { | |
} | ||
|
||
case class Npm( | ||
args: Seq[String] = Seq.empty, | ||
lockFileName: String = "package-lock.json" | ||
) extends PackageManager("npm", args) | ||
lockFileName: String = "package-lock.json", | ||
override val args: Seq[String] = Seq.empty, | ||
addPackagesCommand: String = "install" | ||
) extends PackageManager("npm", args, "install") | ||
with LockFileSupport | ||
with AddPackagesSupport { | ||
override val packageJsonContents: Map[String, JSON] = Map.empty | ||
} | ||
|
||
case class Yarn( | ||
args: Seq[String] = Yarn.DefaultArgs, | ||
lockFileName: String = "yarn.lock" | ||
) extends PackageManager("yarn", args) | ||
with LockFileSupport | ||
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 packageJsonContents: Map[String, JSON] = Map("packageManager" -> JSON.str(s"yarn@$version")) | ||
} | ||
object Yarn { | ||
val DefaultArgs: Seq[String] = Seq("--non-interactive", "--mutex", "network") | ||
} | ||
|
||
object PackageManager { | ||
|
||
/** | ||
* Locally install NPM packages | ||
* | ||
* @param baseDir The (sub-)project directory which contains yarn.lock | ||
* @param installDir The directory in which to install the packages | ||
* @param useYarn Whether to use yarn or npm | ||
* @param logger sbt logger | ||
* @param npmExtraArgs Additional arguments to pass to npm | ||
* @param npmPackages Packages to install (e.g. "webpack", "[email protected]") | ||
*/ | ||
def addPackages(baseDir: File, | ||
installDir: File, | ||
logger: Logger, | ||
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): _*)( | ||
installDir, | ||
logger) | ||
} | ||
} else { | ||
syncNpmLockfile(baseDir, installDir, logger) { | ||
Npm.run("install" +: (npmPackages ++ npmExtraArgs): _*)(installDir, logger) | ||
} | ||
} | ||
} | ||
|
||
def install(baseDir: File, | ||
installDir: File, | ||
logger: Logger, | ||
npmExtraArgs: Seq[String], | ||
yarnExtraArgs: Seq[String]): Unit = | ||
if (useYarn) { | ||
syncYarnLockfile(baseDir, installDir, logger) { | ||
Yarn.run("install" +: (yarnOptions ++ yarnExtraArgs): _*)(installDir, | ||
logger) | ||
} | ||
} else { | ||
syncNpmLockfile(baseDir, installDir, logger) { | ||
Npm.run("install" +: npmExtraArgs: _*)(installDir, logger) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters