Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Frontend shutdown on backend shutdown #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions project/FrontendRunHook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,49 +11,55 @@ object FrontendRunHook {
def apply(base: File): PlayRunHook = {
object UIBuildHook extends PlayRunHook {

var process: Option[Process] = None
def makeProcessString(cmd: String): String = {
// Windows requires npm commands prefixed with cmd /c
if (System.getProperty("os.name").toLowerCase().contains("win")) {
s"cmd /c $cmd"
} else {
cmd
}
}

/**
* Change these commands if you want to use Yarn.
*/
var install: String = FrontendCommands.dependencyInstall
var run: String = FrontendCommands.serve
val frontend: File = base / "ui"

// Windows requires npm commands prefixed with cmd /c
if (System.getProperty("os.name").toLowerCase().contains("win")){
install = "cmd /c" + install
run = "cmd /c" + run
/**
* Runs the given shell command in the frontend dir, blocks until it is done, and returns the return code.
*/
def runAndWait(cmd: String): Int = {
val fullCmd = makeProcessString(cmd)
Process(fullCmd, frontend).!
}

/**
* 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(install, base / "ui").!
if (!(base / "ui" / "node_modules").exists()) {
println(s"Installing npm dependencies")
runAndWait(FrontendCommands.dependencyInstall)
}
}

/**
* Executed after play run start.
* Run npm start
*/
override def afterStarted(): Unit = {
process = Option(
Process(run, base / "ui").run
)
println(s"Booting frontend")
Process(makeProcessString(FrontendCommands.serve), frontend).run
}

/**
* Executed after play run stop.
* Cleanup frontend execution processes.
*/
override def afterStopped(): Unit = {
process.foreach(_.destroy())
process = None
println(s"Shutting down frontend")
runAndWait("npx kill-port 3000")
}

}

UIBuildHook
}
}
}