Skip to content

Commit 3e3e36b

Browse files
committed
CERun is working
1 parent e5b6cc5 commit 3e3e36b

File tree

7 files changed

+667
-1
lines changed

7 files changed

+667
-1
lines changed

build.sbt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ lazy val root = (project in file("."))
4040
"org.scala-js" %% "scalajs-js-envs" % "1.4.0",
4141
"com.google.jimfs" % "jimfs" % "1.2",
4242
"com.outr" %% "scribe" % "3.13.0",
43+
"org.typelevel" %% "cats-effect" % "3.5.2",
4344
"org.scala-js" %% "scalajs-js-envs-test-kit" % "1.1.1" % Test,
4445
"com.novocode" % "junit-interface" % "0.11" % Test
4546
),
@@ -62,6 +63,12 @@ lazy val root = (project in file("."))
6263
Test / parallelExecution := false,
6364
Test / publishArtifact := false,
6465
// sonatypeRepository := "https://oss.sonatype.org/service/local"
66+
scalacOptions ++= Seq(
67+
"-feature",
68+
"-deprecation",
69+
"-unchecked",
70+
"-language:postfixOps"
71+
),
6572
sonatypeRepository := "https://s01.oss.sonatype.org/service/local"
6673
)
6774

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package jsenv.playwright
2+
3+
import org.scalajs.jsenv._
4+
import scribe.format.{FormatterInterpolator, date, level, mdc, messages, methodName, threadName}
5+
6+
import scala.util.control.NonFatal
7+
8+
class CEEnv(
9+
browserName: String,
10+
headless: Boolean,
11+
showLogs: Boolean = false,
12+
pwConfig: PWEnv.Config
13+
) extends JSEnv {
14+
private lazy val validator = {
15+
RunConfig
16+
.Validator()
17+
.supportsInheritIO()
18+
.supportsOnOutputStream()
19+
}
20+
override val name: String = "CEEnv"
21+
setupLogger(showLogs)
22+
23+
// Define the IO Program
24+
//
25+
26+
override def start(input: Seq[Input], runConfig: RunConfig): JSRun =
27+
try {
28+
validator.validate(runConfig)
29+
new CERun(pwConfig, OutputStreams.prepare(runConfig), input)
30+
} catch {
31+
case NonFatal(t) =>
32+
JSRun.failed(t)
33+
}
34+
35+
override def startWithCom(
36+
input: Seq[Input],
37+
runConfig: RunConfig,
38+
onMessage: String => Unit
39+
): JSComRun = new CEComRun(pwConfig, OutputStreams.prepare(runConfig), input)
40+
41+
private def setupLogger(showLogs: Boolean): Unit = {
42+
val formatter =
43+
formatter"$date [$threadName] $level $methodName - $messages$mdc"
44+
if (showLogs) {
45+
scribe.Logger.root
46+
.clearHandlers()
47+
.withHandler(
48+
formatter = formatter,
49+
minimumLevel = Some(scribe.Level.Info)
50+
)
51+
.replace()
52+
scribe.Logger.root.withMinimumLevel(scribe.Level.Trace).replace()
53+
} else {
54+
scribe.Logger.root
55+
.clearHandlers()
56+
.withHandler(
57+
formatter = formatter,
58+
minimumLevel = Some(scribe.Level.Error)
59+
)
60+
.replace()
61+
scribe.Logger.root.withMinimumLevel(scribe.Level.Error).replace()
62+
}
63+
}
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package jsenv.playwright
2+
3+
import jsenv.playwright.PwRun.makeTag
4+
import org.scalajs.jsenv.{Input, UnsupportedInputException}
5+
6+
import java.nio.file.Path
7+
8+
object CEPageUtils {
9+
def htmlPage(
10+
fullInput: Seq[Input],
11+
materializer: FileMaterializer
12+
): String = {
13+
val tags = fullInput.map {
14+
case Input.Script(path) => makeTag(path, "text/javascript", materializer)
15+
case Input.ESModule(path) => makeTag(path, "module", materializer)
16+
case _ => throw new UnsupportedInputException(fullInput)
17+
}
18+
19+
s"""<html>
20+
| <meta charset="UTF-8">
21+
| <body>
22+
| ${tags.mkString("\n ")}
23+
| </body>
24+
|</html>
25+
""".stripMargin
26+
}
27+
28+
private def makeTag(
29+
path: Path,
30+
tpe: String,
31+
materializer: FileMaterializer
32+
): String = {
33+
val url = materializer.materialize(path)
34+
s"<script defer type='$tpe' src='$url'></script>"
35+
}
36+
}

0 commit comments

Comments
 (0)