/
File.scala
78 lines (72 loc) · 1.94 KB
/
File.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package munit.internal.io
import java.net.URI
// obtained implementation by experimentation on the JDK.
class File(path: String) {
def this(parent: String, child: String) =
this(parent + File.separator + child)
def this(parent: File, child: String) =
this(parent.getPath, child)
def this(uri: URI) =
this(
if (uri.getScheme != "file") {
throw new IllegalArgumentException("URI scheme is not \"file\"")
} else {
uri.getPath
}
)
def toPath: MunitPath =
MunitPath(path)
def toURI: URI = {
val file = getAbsoluteFile.toString
val uripath =
if (file.startsWith("/")) file
else "/" + file.replace(File.separator, "/")
val withslash =
if (isDirectory && !uripath.endsWith("/")) uripath + "/" else uripath
new URI("file", null, withslash, null)
}
def getAbsoluteFile: File =
toPath.toAbsolutePath.toFile
def getAbsolutePath: String =
getAbsoluteFile.toString
def getParentFile: File =
toPath.getParent.toFile
def mkdirs(): Unit =
throw new UnsupportedOperationException(
"mkdirs() is not supported in Scala.js"
)
def getPath: String =
path
def exists(): Boolean =
JSIO.exists(path)
def isFile: Boolean =
JSIO.isFile(path)
def isDirectory: Boolean =
JSIO.isDirectory(path)
override def toString: String =
path
}
object File {
def listRoots(): Array[File] = Array(
new File(
JSIO.path match {
case Some(p) => p.parse(p.resolve()).root.asInstanceOf[String]
case None => "/"
}
// if (JSIO.isNode) JSPath.parse(JSPath.resolve()).root
// else "/"
)
)
def separatorChar: Char =
separator.charAt(0)
def separator: String =
JSIO.path match {
case Some(p) => p.sep.asInstanceOf[String]
case None => "/"
}
def pathSeparator: String =
JSIO.path match {
case Some(p) => p.delimeter.asInstanceOf[String]
case None => ":"
}
}