Skip to content

Commit 44cd5ab

Browse files
committed
Introduce a new surface API for JS envs with Config objects.
This only changes the surface API of concrete JS envs. Their existing constructors are deprecated in favor of an overload with a `Config` object. This change provides in the 0.6.x series an API that can be used in a source-compatible way between 0.6.x and 1.x. In 1.x, deeper changes to the internal API of JS envs will be done. We also take this opportunity to "move" `JSDOMNodeJSEnv` in a different package `org.scalajs.jsenv.jsdomnodejs`. Since this JS env is scheduled to be moved in a different repository in 1.x, it should eventually be in a different package anyway.
1 parent ba9e5a0 commit 44cd5ab

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

js-envs/src/main/scala/org/scalajs/jsenv/nodejs/NodeJSEnv.scala

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,33 @@ import org.scalajs.core.tools.logging._
1919

2020
import java.io.{ Console => _, _ }
2121

22-
class NodeJSEnv private (
23-
@deprecatedName('nodejsPath)
24-
override protected val executable: String, // override val for bin compat
25-
@deprecatedName('addArgs)
26-
args: Seq[String],
27-
@deprecatedName('addEnv)
28-
env: Map[String, String],
29-
sourceMap: Boolean)
30-
extends AbstractNodeJSEnv(executable, args, env, sourceMap) {
22+
class NodeJSEnv(config: NodeJSEnv.Config)
23+
extends AbstractNodeJSEnv(config.executable, config.args, config.env,
24+
config.sourceMap) {
3125

26+
def this() = this(NodeJSEnv.Config())
27+
28+
@deprecated("Use the overload with a NodeJSEnv.Config.", "0.6.18")
3229
def this(
3330
@deprecatedName('nodejsPath)
3431
executable: String = "node",
3532
@deprecatedName('addArgs)
3633
args: Seq[String] = Seq.empty,
3734
@deprecatedName('addEnv)
3835
env: Map[String, String] = Map.empty) = {
39-
this(executable, args, env, sourceMap = true)
36+
this(NodeJSEnv.Config().withExecutable(executable).withArgs(args.toList).withEnv(env))
4037
}
4138

39+
@deprecated("Use the overloaded constructor with a NodeJSEnv.Config.",
40+
"0.6.18")
4241
def withSourceMap(sourceMap: Boolean): NodeJSEnv =
43-
new NodeJSEnv(executable, args, env, sourceMap)
42+
new NodeJSEnv(config.withSourceMap(sourceMap))
4443

4544
protected def vmName: String = "Node.js"
4645

46+
// For binary compatibility
47+
override protected val executable: String = config.executable
48+
4749
override def jsRunner(libs: Seq[ResolvedJSDependency],
4850
code: VirtualJSFile): JSRunner = {
4951
new NodeRunner(libs, code)
@@ -99,3 +101,55 @@ class NodeJSEnv private (
99101
}
100102

101103
}
104+
105+
object NodeJSEnv {
106+
final class Config private (
107+
val executable: String,
108+
val args: List[String],
109+
val env: Map[String, String],
110+
val sourceMap: Boolean
111+
) {
112+
private def this() = {
113+
this(
114+
executable = "node",
115+
args = Nil,
116+
env = Map.empty,
117+
sourceMap = true
118+
)
119+
}
120+
121+
def withExecutable(executable: String): Config =
122+
copy(executable = executable)
123+
124+
def withArgs(args: List[String]): Config =
125+
copy(args = args)
126+
127+
def withEnv(env: Map[String, String]): Config =
128+
copy(env = env)
129+
130+
def withSourceMap(sourceMap: Boolean): Config =
131+
copy(sourceMap = sourceMap)
132+
133+
private def copy(
134+
executable: String = executable,
135+
args: List[String] = args,
136+
env: Map[String, String] = env,
137+
sourceMap: Boolean = sourceMap
138+
): Config = {
139+
new Config(executable, args, env, sourceMap)
140+
}
141+
}
142+
143+
object Config {
144+
/** Returns a default configuration for a [[NodeJSEnv]].
145+
*
146+
* The defaults are:
147+
*
148+
* - `executable`: `"node"`
149+
* - `args`: `Nil`
150+
* - `env`: `Map.empty`
151+
* - `sourceMap`: `true`
152+
*/
153+
def apply(): Config = new Config()
154+
}
155+
}

0 commit comments

Comments
 (0)