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

chore: user file-watcher on parent folder #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
27 changes: 18 additions & 9 deletions cli/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import typings.node.{fsMod, pathMod}

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.timers
import scala.scalajs.js.{|, timers}

object Main {

Expand Down Expand Up @@ -57,6 +57,10 @@ object Main {
var watcher: Option[fsMod.FSWatcher] = None
var lastTimeout: Option[timers.SetTimeoutHandle] = None

val jsFilePath = pathMod.parse(config.jsFileName)
val jsFileName = jsFilePath.base
val jsParentFolder = jsFilePath.dir

def run(): Unit =
setHandler(config) match {
case Right(()) => ()
Expand All @@ -74,11 +78,16 @@ object Main {

try {
val w: fsMod.FSWatcher = fsMod.watch(
filename = config.jsFileName,
listener = { (_, _) =>
println(s"${config.mode}> File changed, resetting...")
run()
watch() // since the file might have been deleted, reinitialize the watcher
filename = jsParentFolder,
listener = { (event, filename) =>
println(s"watcher triggered. event: $event, filename: ${filename}")
FloWi marked this conversation as resolved.
Show resolved Hide resolved
val parsedFilePath = pathMod.parse(filename)
val parsedFilename = parsedFilePath.base
if(parsedFilename == jsFileName) {
println(s"${config.mode}> File changed, resetting...")
run()
watch() // since the file might have been deleted, reinitialize the watcher
}
},
)

Expand Down Expand Up @@ -142,7 +151,7 @@ object Main {
Either
.catchNonFatal(requireUncached(pathMod.resolve(config.jsFileName)))
.left
.map(exception => s"Error when requiring js file: ${exception}")
.map(exception => s"Error when requiring js file: $exception")
exportedHandler <-
requiredJs
.selectDynamic(config.exportName)
Expand All @@ -167,11 +176,11 @@ object Main {
Either.catchNonFatal(function(a, b)) match {
case Right(r) =>
r.`catch`[R]({ (error: Any) =>
println(s"${mode}> Error in function promise: $error")
println(s"$mode> Error in function promise: $error")
js.Promise.reject(error)
}: js.Function1[Any, js.Thenable[R]])
case Left(e) =>
println(s"${mode}> Error in function: $e")
println(s"$mode> Error in function: $e")
throw e
}
}
Expand Down
Loading