File tree 2 files changed +15
-4
lines changed
scalac-scoverage-runtime/js/src/main/scala/scalajssupport
2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change 1
1
package scalajssupport
2
2
3
3
import scala .scalajs .js
4
+ import js .Dynamic .{ global => g }
4
5
5
6
/**
6
7
* This wraps RhinoFile, NodeFile, or PhantomFile depending on which javascript
@@ -56,9 +57,10 @@ class File(path: String) {
56
57
}
57
58
58
59
object File {
59
- val jsFile : JsFileObject = if (js.Dynamic .global.hasOwnProperty(" Packages" ).asInstanceOf [Boolean ])
60
+
61
+ private val jsFile : JsFileObject = if (g.hasOwnProperty(" Packages" ).asInstanceOf [Boolean ])
60
62
RhinoFile
61
- else if (! js.Dynamic .global.hasOwnProperty( " window " ). asInstanceOf [ Boolean ] )
63
+ else if (js.typeOf(g .global) == " object " && js.typeOf(g.global.require) == " function " )
62
64
NodeFile
63
65
else
64
66
PhantomFile
Original file line number Diff line number Diff line change 1
1
package scalajssupport
2
2
3
3
import scala .scalajs .js
4
+ import scala .scalajs .js .annotation .JSGlobal
4
5
5
6
class NodeFile (path : String ) extends JsFile {
6
7
def this (path : String , child : String ) = {
@@ -85,9 +86,17 @@ trait NodePath extends js.Object {
85
86
def join (paths : String * ): String = js.native
86
87
}
87
88
89
+ @ js.native
90
+ @ JSGlobal (" global" )
91
+ object NodeJsGlobal extends js.Object {
92
+ def require (module : String ): js.Any = js.native
93
+ }
94
+
88
95
private [scalajssupport] object NodeFile extends JsFileObject {
89
- val fs : FS = js.Dynamic .global.require(" fs" ).asInstanceOf [FS ]
90
- val nodePath : NodePath = js.Dynamic .global.require(" path" ).asInstanceOf [NodePath ]
96
+
97
+ private val fs : FS = NodeJsGlobal .require(" fs" ).asInstanceOf [FS ]
98
+ private val nodePath : NodePath = NodeJsGlobal .require(" path" ).asInstanceOf [NodePath ]
99
+
91
100
def write (path : String , data : String , mode : String = " a" ) = {
92
101
fs.writeFileSync(path, data, js.Dynamic .literal(flag = mode))
93
102
}
You can’t perform that action at this time.
0 commit comments