Skip to content

Instantly share code, notes, and snippets.

@yohangz
Last active March 27, 2019 05:41
Show Gist options
  • Save yohangz/862773fe2377ecd326cfae028c9afeb2 to your computer and use it in GitHub Desktop.
Save yohangz/862773fe2377ecd326cfae028c9afeb2 to your computer and use it in GitHub Desktop.
Play frontend build run hook
import play.sbt.PlayRunHook
import sbt._
import scala.sys.process.Process
/**
* Frontend build play run hook.
* https://www.playframework.com/documentation/2.7.x/SBTCookbook
*/
object FrontendRunHook {
def apply(base: File): PlayRunHook = {
object UIBuildHook extends PlayRunHook {
var process: Option[Process] = None
/**
* Change these commands if you want to use Yarn.
*/
var npmInstall: String = FrontendCommands.dependencyInstall
var npmRun: String = FrontendCommands.serve
// Windows requires npm commands prefixed with cmd /c
if (System.getProperty("os.name").toLowerCase().contains("win")){
npmInstall = "cmd /c" + npmInstall
npmRun = "cmd /c" + npmRun
}
/**
* Executed before play run start.
* Run npm install if node modules are not installed.
*/
override def beforeStarted(): Unit = {
if (!(base / "ui" / "node_modules").exists()) Process(npmInstall, base / "ui").!
}
/**
* Executed after play run start.
* Run npm start
*/
override def afterStarted(): Unit = {
process = Option(
Process(npmRun, base / "ui").run
)
}
/**
* Executed after play run stop.
* Cleanup frontend execution processes.
*/
override def afterStopped(): Unit = {
process.foreach(_.destroy())
process = None
}
}
UIBuildHook
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment