Last active
March 27, 2019 05:41
-
-
Save yohangz/862773fe2377ecd326cfae028c9afeb2 to your computer and use it in GitHub Desktop.
Play frontend build run hook
This file contains hidden or 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
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