-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #376 from olafurpg/js-node-fs
Remove Node.js requirement with Scala.js
- Loading branch information
Showing
5 changed files
with
111 additions
and
127 deletions.
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
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