Skip to content

Commit 8188d02

Browse files
committed
format fix
1 parent 71911b6 commit 8188d02

21 files changed

+184
-171
lines changed

.scalafmt.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = "3.9.7"
1+
version = "3.9.9"
22
project.git = true
33

44
runner.dialect = scala3

build.mill

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ object V{
3333

3434
trait FormatFix extends ScalafmtModule with ScalafixModule with ScalaModule
3535

36+
3637
trait FormatFixPublish extends ScalaModule with FormatFix with PublishModule{
3738
override def scalaVersion = V.scalaVersion
3839

plugin/src/refresh_plugin.scala

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
package io.github.quafadas
2-
3-
import io.github.quafadas.sjsls.LiveServerConfig
4-
import mill.*
5-
import mill.scalalib.*
6-
import mill.scalajslib.*
7-
8-
import mill.api.Task.Simple
92
import fs2.concurrent.Topic
3+
104
import cats.effect.IO
115
import cats.effect.unsafe.implicits.global
6+
127
import io.github.quafadas.sjsls.LiveServerConfig
8+
import mill.*
139
import mill.api.BuildCtx
10+
import mill.api.Task.Simple
11+
import mill.scalajslib.*
1412
import mill.scalajslib.api.Report
1513
implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global
1614

1715
trait ScalaJsRefreshModule extends ScalaJSModule:
1816

1917
lazy val updateServer = Topic[IO, Unit].unsafeRunSync()
2018

21-
22-
def indexHtml = Task{
19+
def indexHtml = Task {
2320
os.write.over(Task.dest / "index.html", io.github.quafadas.sjsls.vanillaTemplate(withStyles()))
2421
PathRef(Task.dest / "index.html")
2522
}
2623

2724
def assetsDir =
28-
super.moduleDir / "assets"
25+
super.moduleDir / "assets"
2926

30-
def withStyles = Task{ true}
27+
def withStyles = Task(true)
3128

32-
def assets = Task.Source{
29+
def assets = Task.Source {
3330
assetsDir
3431
}
3532

3633
def port = Task {
3734
8080
3835
}
3936

40-
def openBrowser= Task {
37+
def openBrowser = Task {
4138
true
4239
}
4340

@@ -49,50 +46,51 @@ trait ScalaJsRefreshModule extends ScalaJSModule:
4946
true
5047
}
5148

52-
def siteGen = Task{
53-
val assets_ = assets()
54-
val path = fastLinkJS().dest.path
55-
os.copy.over(indexHtml().path, Task.dest / "index.html")
56-
os.copy(assets_.path, Task.dest, mergeFolders = true)
57-
updateServer.publish1(println("publish update")).unsafeRunSync()
58-
(Task.dest.toString(), assets_.path.toString(), path.toString())
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())
5956

6057
}
6158

62-
def lcs = Task.Worker{
59+
def lcs = Task.Worker {
6360
val (site, assets, js) = siteGen()
6461
println("Gen lsc")
6562
LiveServerConfig(
66-
baseDir = None,
67-
outDir = Some(js),
68-
port = com.comcast.ip4s.Port.fromInt(port()).getOrElse(throw new IllegalArgumentException(s"invalid port: ${port()}")),
69-
indexHtmlTemplate = Some(site),
70-
buildTool = io.github.quafadas.sjsls.NoBuildTool(), // Here we are a slave to the build tool
71-
openBrowserAt = "/index.html",
72-
preventBrowserOpen = !openBrowser(),
73-
dezombify = dezombify(),
74-
logLevel = logLevel(),
75-
customRefresh = Some(updateServer)
76-
)
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+
)
7775
}
7876

79-
def serve = Task.Worker{
77+
def serve = Task.Worker {
8078

8179
println(lcs())
8280
BuildCtx.withFilesystemCheckerDisabled {
8381
new RefreshServer(lcs())
8482
}
8583
}
8684

87-
class RefreshServer(lcs: LiveServerConfig) extends AutoCloseable {
88-
val server = io.github.quafadas.sjsls.LiveServer.main(lcs).allocated
85+
class RefreshServer(lcs: LiveServerConfig) extends AutoCloseable:
86+
val server = io.github.quafadas.sjsls.LiveServer.main(lcs).allocated
8987

9088
server.map(_._1).unsafeRunSync()
9189

92-
override def close(): Unit = {
90+
override def close(): Unit =
9391
// This is the shutdown hook for http4s
9492
println("Shutting down server...")
9593
server.map(_._2).flatten.unsafeRunSync()
96-
}
97-
}
98-
94+
end close
95+
end RefreshServer
96+
end ScalaJsRefreshModule

sjsls/src/CliOpts.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package io.github.quafadas.sjsls
22

3-
import com.monovore.decline.Opts
43
import com.comcast.ip4s.Port
4+
import com.monovore.decline.Opts
55

66
private[sjsls] object CliOps:
77
val logLevelOpt: Opts[String] = Opts
8-
.option[String]("log-level", help = "The log level. info, debug, error, trace)")
9-
.withDefault("info")
10-
.validate("Invalid log level") {
11-
case "info" => true
12-
case "debug" => true
13-
case "error" => true
14-
case "warn" => true
15-
case "trace" => true
16-
case _ => false
17-
}
8+
.option[String]("log-level", help = "The log level. info, debug, error, trace)")
9+
.withDefault("info")
10+
.validate("Invalid log level") {
11+
case "info" => true
12+
case "debug" => true
13+
case "error" => true
14+
case "warn" => true
15+
case "trace" => true
16+
case _ => false
17+
}
1818

1919
val openBrowserAtOpt =
2020
Opts
@@ -135,4 +135,4 @@ private[sjsls] object CliOps:
135135
"Whether or not to attempt killing any processes that are using the specified port. Default: true"
136136
)
137137
.orTrue
138-
138+
end CliOps

sjsls/src/CliValidationError.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ package io.github.quafadas.sjsls
22

33
import scala.util.control.NoStackTrace
44

5-
private case class CliValidationError(message: String) extends NoStackTrace
5+
private case class CliValidationError(message: String) extends NoStackTrace

sjsls/src/LiveServerConfig.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.github.quafadas.sjsls
22

33
import com.comcast.ip4s.Port
4+
45
import fs2.concurrent.Topic
6+
57
import cats.effect.IO
68

79
case class LiveServerConfig(
@@ -23,4 +25,4 @@ case class LiveServerConfig(
2325
injectPreloads: Boolean = false,
2426
dezombify: Boolean = true,
2527
customRefresh: Option[Topic[IO, Unit]] = None
26-
)
28+
)

sjsls/src/dezombify.scala

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,72 @@
11
package io.github.quafadas.sjsls
22

3-
import cats.effect.IO
3+
import scala.concurrent.duration.*
4+
5+
import com.comcast.ip4s.Port
6+
47
import fs2.io.process
8+
9+
import cats.effect.IO
510
import cats.effect.kernel.Resource
6-
import com.comcast.ip4s.Port
7-
import scala.concurrent.duration._
811

9-
private[sjsls] def checkPortInUse(port: Port): IO[Boolean] = {
12+
private[sjsls] def checkPortInUse(port: Port): IO[Boolean] =
1013
val osName = System.getProperty("os.name").toLowerCase
1114
val portInt = port.value
1215

13-
if (osName.contains("win")) {
14-
val ps = s"Get-NetTCPConnection -LocalPort $portInt -ErrorAction SilentlyContinue | Measure-Object | Select-Object -ExpandProperty Count"
16+
if osName.contains("win") then
17+
val ps =
18+
s"Get-NetTCPConnection -LocalPort $portInt -ErrorAction SilentlyContinue | Measure-Object | Select-Object -ExpandProperty Count"
1519

16-
process.ProcessBuilder("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", ps)
20+
process
21+
.ProcessBuilder("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", ps)
1722
.spawn[IO]
18-
.use { proc =>
19-
proc.stdout.through(fs2.text.utf8.decode).compile.string.map(_.trim.toIntOption.getOrElse(0) > 0)
23+
.use {
24+
proc =>
25+
proc.stdout.through(fs2.text.utf8.decode).compile.string.map(_.trim.toIntOption.getOrElse(0) > 0)
2026
}
21-
} else {
27+
else
2228
val sh = s"lsof -ti tcp:$portInt 2>/dev/null | wc -l"
2329

24-
process.ProcessBuilder("sh", "-c", sh)
30+
process
31+
.ProcessBuilder("sh", "-c", sh)
2532
.spawn[IO]
26-
.use { proc =>
27-
proc.stdout.through(fs2.text.utf8.decode).compile.string.map(_.trim.toIntOption.getOrElse(0) > 0)
33+
.use {
34+
proc =>
35+
proc.stdout.through(fs2.text.utf8.decode).compile.string.map(_.trim.toIntOption.getOrElse(0) > 0)
2836
}
29-
}
30-
}
37+
end if
38+
end checkPortInUse
3139

32-
private[sjsls] def dezombify(port: Port): Resource[IO, Unit] = {
40+
private[sjsls] def dezombify(port: Port): Resource[IO, Unit] =
3341
val portInt = port.value
3442

35-
val checkAndKill = for {
43+
val checkAndKill = for
3644
_ <- scribe.cats[IO].debug(s"Checking if port $portInt is in use before attempting cleanup")
3745
portInUse <- checkPortInUse(port)
38-
_ <- if (portInUse) {
39-
scribe.cats[IO].warn(s"Found zombie server on port $portInt - attempting to kill it")
40-
} else {
41-
scribe.cats[IO].debug(s"Port $portInt appears to be free, no zombie cleanup needed")
42-
}
43-
_ <- if (portInUse) killProcessesOnPort(port) else IO.unit
44-
_ <- if (portInUse) {
45-
for {
46-
_ <- IO.sleep(scala.concurrent.duration.Duration.fromNanos(500_000_000)) // 500ms
47-
stillInUse <- checkPortInUse(port)
48-
_ <- if (stillInUse) {
49-
scribe.cats[IO].error(s"Port $portInt still appears to be in use after cleanup attempt")
50-
} else {
51-
scribe.cats[IO].debug(s"Successfully cleaned up port $portInt")
52-
}
53-
} yield ()
54-
} else IO.unit
55-
} yield ()
46+
_ <-
47+
if portInUse then scribe.cats[IO].warn(s"Found zombie server on port $portInt - attempting to kill it")
48+
else scribe.cats[IO].debug(s"Port $portInt appears to be free, no zombie cleanup needed")
49+
_ <- if portInUse then killProcessesOnPort(port) else IO.unit
50+
_ <-
51+
if portInUse then
52+
for
53+
_ <- IO.sleep(scala.concurrent.duration.Duration.fromNanos(500_000_000)) // 500ms
54+
stillInUse <- checkPortInUse(port)
55+
_ <-
56+
if stillInUse then scribe.cats[IO].error(s"Port $portInt still appears to be in use after cleanup attempt")
57+
else scribe.cats[IO].debug(s"Successfully cleaned up port $portInt")
58+
yield ()
59+
else IO.unit
60+
yield ()
5661

5762
checkAndKill.toResource
63+
end dezombify
5864

59-
}
60-
61-
private def killProcessesOnPort(port: Port): IO[Unit] = {
65+
private def killProcessesOnPort(port: Port): IO[Unit] =
6266
val osName = System.getProperty("os.name").toLowerCase
6367
val portInt = port.value
6468

65-
if (osName.contains("win")) {
69+
if osName.contains("win") then
6670
// Windows: try PowerShell Get-NetTCPConnection, fallback to netstat/taskkill
6771
val ps = s"""
6872
|if (Get-Command Get-NetTCPConnection -ErrorAction SilentlyContinue) {
@@ -87,15 +91,16 @@ private def killProcessesOnPort(port: Port): IO[Unit] = {
8791
|}
8892
|""".stripMargin
8993

90-
for {
94+
for
9195
_ <- scribe.cats[IO].debug(s"Running Windows cleanup command for port $portInt")
92-
exitCode <- process.ProcessBuilder("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", ps)
96+
exitCode <- process
97+
.ProcessBuilder("powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-Command", ps)
9398
.spawn[IO]
9499
.use(_.exitValue)
95100
_ <- scribe.cats[IO].debug(s"Windows cleanup command completed with exit code $exitCode")
96-
} yield ()
97-
98-
} else {
101+
yield ()
102+
end for
103+
else
99104
// macOS/Linux: use lsof if available, fallback to fuser
100105
val sh = s"""
101106
|if command -v lsof >/dev/null 2>&1; then
@@ -116,17 +121,20 @@ private def killProcessesOnPort(port: Port): IO[Unit] = {
116121
|fi
117122
|""".stripMargin
118123

119-
for {
124+
for
120125
_ <- scribe.cats[IO].debug(s"Running Unix cleanup command for port $portInt")
121126
// Use a timeout to prevent hanging
122-
result <- process.ProcessBuilder("sh", "-c", sh)
127+
result <- process
128+
.ProcessBuilder("sh", "-c", sh)
123129
.spawn[IO]
124130
.use(_.exitValue)
125131
.timeout(5.seconds)
126-
.handleErrorWith { err =>
127-
scribe.cats[IO].warn(s"Process cleanup timed out or failed: ${err.getMessage}") *> IO.pure(-1)
132+
.handleErrorWith {
133+
err =>
134+
scribe.cats[IO].warn(s"Process cleanup timed out or failed: ${err.getMessage}") *> IO.pure(-1)
128135
}
129136
_ <- scribe.cats[IO].debug(s"Unix cleanup command completed with exit code $result")
130-
} yield ()
131-
}
132-
}
137+
yield ()
138+
end for
139+
end if
140+
end killProcessesOnPort

0 commit comments

Comments
 (0)