-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Node.js requirement with Scala.js #376
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ package java.io | |
|
||
import java.net.URI | ||
import java.nio.file.Path | ||
|
||
import munit.internal.{JSIO, JSPath, NodeNIOPath} | ||
import munit.internal.JSIO | ||
import munit.internal.NodeNIOPath | ||
|
||
// obtained implementation by experimentation on the JDK. | ||
class File(path: String) { | ||
|
@@ -55,19 +55,27 @@ class File(path: String) { | |
object File { | ||
def listRoots(): Array[File] = Array( | ||
new File( | ||
if (JSIO.isNode) JSPath.parse(JSPath.resolve()).root | ||
else "/" | ||
JSIO.path match { | ||
case Some(p) => p.parse(p.resolve()).root.asInstanceOf[String] | ||
case None => "/" | ||
} | ||
// if (JSIO.isNode) JSPath.parse(JSPath.resolve()).root | ||
// else "/" | ||
Comment on lines
+62
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leftover? |
||
) | ||
) | ||
|
||
def separatorChar: Char = | ||
separator.charAt(0) | ||
|
||
def separator: String = | ||
if (JSIO.isNode) JSPath.sep | ||
else "/" | ||
JSIO.path match { | ||
case Some(p) => p.sep.asInstanceOf[String] | ||
case None => "/" | ||
} | ||
|
||
def pathSeparator: String = | ||
if (JSIO.isNode) JSPath.delimiter | ||
else ":" | ||
JSIO.path match { | ||
case Some(p) => p.delimeter.asInstanceOf[String] | ||
case None => ":" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,38 @@ | ||
package munit.internal | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.JSImport | ||
import scala.scalajs.js.annotation.JSImport.Namespace | ||
|
||
/** | ||
* Facade for the native nodejs process API | ||
* | ||
* The process object is a global that provides information about, and | ||
* control over, the current Node.js process. As a global, it is always | ||
* available to Node.js applications without using require(). | ||
* | ||
* @see https://nodejs.org/api/process.html | ||
*/ | ||
@js.native | ||
trait JSProcess extends js.Any { | ||
def cwd(): String = js.native | ||
} | ||
|
||
/** | ||
* Facade for native nodejs module "fs". | ||
* | ||
* @see https://nodejs.org/api/fs.html | ||
*/ | ||
@js.native | ||
@JSImport("fs", Namespace) | ||
object JSFs extends js.Any { | ||
|
||
/** | ||
* Returns the file contents as Buffer using blocking apis. | ||
* | ||
* NOTE: The actual return value is a Node.js buffer and not js.Array[Int]. | ||
* However, both support .length and angle bracket access (foo[1]). | ||
*/ | ||
def readFileSync(path: String): js.Array[Int] = js.native | ||
|
||
/** Returns the file contents as String using blocking apis */ | ||
def readFileSync(path: String, encoding: String): String = js.native | ||
|
||
/** Writes file contents using blocking apis */ | ||
def writeFileSync(path: String, buffer: js.Array[Int]): Unit = js.native | ||
|
||
/** Returns an array of filenames excluding '.' and '..'. */ | ||
def readdirSync(path: String): js.Array[String] = js.native | ||
|
||
/** Returns an fs.Stats for path. */ | ||
def lstatSync(path: String): JSStats = js.native | ||
|
||
/** Returns true if the file exists, false otherwise. */ | ||
def existsSync(path: String): Boolean = js.native | ||
|
||
/** Synchronously creates a directory. */ | ||
def mkdirSync(path: String): Unit = js.native | ||
} | ||
|
||
/** | ||
* Facade for nodejs class fs.Stats. | ||
* | ||
* @see https://nodejs.org/api/fs.html#fs_class_fs_stats | ||
*/ | ||
@js.native | ||
@JSImport("fs", Namespace) | ||
class JSStats extends js.Any { | ||
def isFile(): Boolean = js.native | ||
def isDirectory(): Boolean = js.native | ||
} | ||
|
||
/** | ||
* Facade for native nodejs module "path". | ||
* | ||
* @see https://nodejs.org/api/path.html | ||
*/ | ||
@js.native | ||
@JSImport("path", Namespace) | ||
object JSPath extends js.Any { | ||
def sep: String = js.native | ||
def delimiter: String = js.native | ||
def isAbsolute(path: String): Boolean = js.native | ||
def parse(path: String): JSPath.type = js.native | ||
def resolve(paths: String*): String = js.native | ||
def normalize(path: String): String = js.native | ||
def basename(path: String): String = js.native | ||
def dirname(path: String): String = js.native | ||
def root: String = js.native | ||
def relative(from: String, to: String): String = js.native | ||
def join(first: String, more: String*): String = js.native | ||
} | ||
import scala.util.Try | ||
|
||
object JSIO { | ||
private[internal] val process: JSProcess = | ||
js.Dynamic.global.process.asInstanceOf[JSProcess] | ||
def isNode: Boolean = | ||
!js.isUndefined(process) && !js.isUndefined(process.cwd()) | ||
|
||
def inNode[T](f: => T): T = | ||
if (JSIO.isNode) f | ||
else { | ||
throw new IllegalStateException( | ||
"This operation is not supported in this environment." | ||
) | ||
} | ||
private def require(module: String): Option[js.Dynamic] = { | ||
Try(js.Dynamic.global.require(module)).toOption | ||
} | ||
val process: Option[js.Dynamic] = require("process") | ||
val path: Option[js.Dynamic] = require("path") | ||
val fs: Option[js.Dynamic] = require("fs") | ||
|
||
def cwd(): String = | ||
if (isNode) process.cwd() | ||
else "/" | ||
process match { | ||
case Some(p) => p.cwd().asInstanceOf[String] | ||
case None => "/" | ||
} | ||
|
||
def exists(path: String): Boolean = | ||
if (isNode) JSFs.existsSync(path) | ||
else false | ||
fs match { | ||
case Some(f) => f.existsSync(path).asInstanceOf[Boolean] | ||
case None => false | ||
} | ||
|
||
def isFile(path: String): Boolean = | ||
exists(path) && JSFs.lstatSync(path).isFile() | ||
exists(path) && (fs match { | ||
case Some(f) => f.lstatSync(path).isFile().asInstanceOf[Boolean] | ||
case None => false | ||
}) | ||
|
||
def isDirectory(path: String): Boolean = | ||
exists(path) && JSFs.lstatSync(path).isDirectory() | ||
exists(path) && (fs match { | ||
case Some(f) => f.lstatSync(path).isDirectory().asInstanceOf[Boolean] | ||
case None => false | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just as a complete side note, I don't think we need this anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I had a draft comment saying the same thing