|
| 1 | +package io.github.quafadas |
| 2 | +import fs2.concurrent.Topic |
| 3 | + |
| 4 | +import cats.effect.IO |
| 5 | +import cats.effect.unsafe.implicits.global |
| 6 | + |
| 7 | +import io.github.quafadas.sjsls.LiveServerConfig |
| 8 | +import mill.* |
| 9 | +import mill.api.BuildCtx |
| 10 | +import mill.api.Task.Simple |
| 11 | +import mill.scalajslib.* |
| 12 | +import mill.scalajslib.api.Report |
| 13 | +implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global |
| 14 | + |
| 15 | +trait ScalaJsRefreshModule extends ScalaJSModule: |
| 16 | + |
| 17 | + lazy val updateServer = Topic[IO, Unit].unsafeRunSync() |
| 18 | + |
| 19 | + def indexHtml = Task { |
| 20 | + os.write.over(Task.dest / "index.html", io.github.quafadas.sjsls.vanillaTemplate(withStyles())) |
| 21 | + PathRef(Task.dest / "index.html") |
| 22 | + } |
| 23 | + |
| 24 | + def assetsDir = |
| 25 | + super.moduleDir / "assets" |
| 26 | + |
| 27 | + def withStyles = Task(true) |
| 28 | + |
| 29 | + def assets = Task.Source { |
| 30 | + assetsDir |
| 31 | + } |
| 32 | + |
| 33 | + def port = Task { |
| 34 | + 8080 |
| 35 | + } |
| 36 | + |
| 37 | + def openBrowser = Task { |
| 38 | + true |
| 39 | + } |
| 40 | + |
| 41 | + def logLevel = Task { |
| 42 | + "warn" |
| 43 | + } |
| 44 | + |
| 45 | + def dezombify = Task { |
| 46 | + true |
| 47 | + } |
| 48 | + |
| 49 | + def siteGen = Task { |
| 50 | + val assets_ = assets() |
| 51 | + val path = fastLinkJS().dest.path |
| 52 | + os.copy.over(indexHtml().path, Task.dest / "index.html") |
| 53 | + os.copy(assets_.path, Task.dest, mergeFolders = true) |
| 54 | + updateServer.publish1(println("publish update")).unsafeRunSync() |
| 55 | + (Task.dest.toString(), assets_.path.toString(), path.toString()) |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + def lcs = Task.Worker { |
| 60 | + val (site, assets, js) = siteGen() |
| 61 | + println("Gen lsc") |
| 62 | + LiveServerConfig( |
| 63 | + baseDir = None, |
| 64 | + outDir = Some(js), |
| 65 | + port = |
| 66 | + com.comcast.ip4s.Port.fromInt(port()).getOrElse(throw new IllegalArgumentException(s"invalid port: ${port()}")), |
| 67 | + indexHtmlTemplate = Some(site), |
| 68 | + buildTool = io.github.quafadas.sjsls.NoBuildTool(), // Here we are a slave to the build tool |
| 69 | + openBrowserAt = "/index.html", |
| 70 | + preventBrowserOpen = !openBrowser(), |
| 71 | + dezombify = dezombify(), |
| 72 | + logLevel = logLevel(), |
| 73 | + customRefresh = Some(updateServer) |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + def serve = Task.Worker { |
| 78 | + |
| 79 | + println(lcs()) |
| 80 | + BuildCtx.withFilesystemCheckerDisabled { |
| 81 | + new RefreshServer(lcs()) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + class RefreshServer(lcs: LiveServerConfig) extends AutoCloseable: |
| 86 | + val server = io.github.quafadas.sjsls.LiveServer.main(lcs).allocated |
| 87 | + |
| 88 | + server.map(_._1).unsafeRunSync() |
| 89 | + |
| 90 | + override def close(): Unit = |
| 91 | + // This is the shutdown hook for http4s |
| 92 | + println("Shutting down server...") |
| 93 | + server.map(_._2).flatten.unsafeRunSync() |
| 94 | + end close |
| 95 | + end RefreshServer |
| 96 | +end ScalaJsRefreshModule |
0 commit comments