Skip to content

Commit

Permalink
Add Scala.js support for completions install on node
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jun 14, 2024
1 parent daab94d commit 8db890b
Show file tree
Hide file tree
Showing 14 changed files with 472 additions and 265 deletions.
7 changes: 7 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ object tests extends Module {
Deps.utest
)
def testFramework = "utest.runner.Framework"
// not sure why we need to manually add this one
def sources = T.sources(super.sources() ++ CrossSources.extraSourcesDirs(
scalaVersion(),
millSourcePath,
"js",
"test"
))
}
}
trait TestsNative extends Tests0 with CaseAppScalaNativeModule {
Expand Down

This file was deleted.

5 changes: 5 additions & 0 deletions core/js/src/main/scala/caseapp/core/app/nio/File.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package caseapp.core.app.nio

object File {
def separator = Path.nodePath.sep.asInstanceOf[String]
}
34 changes: 34 additions & 0 deletions core/js/src/main/scala/caseapp/core/app/nio/FileOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package caseapp.core.app.nio

import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}

object FileOps {

private lazy val nodeFs = g.require("fs")
private lazy val nodeOs = g.require("os")
private lazy val nodeProcess = g.require("process")

def readFile(path: Path): String =
nodeFs.readFileSync(path.underlying, js.Dictionary("encoding" -> "utf8")).asInstanceOf[String]
def writeFile(path: Path, content: String): Unit =
nodeFs.writeFileSync(path.underlying, content)
def appendToFile(path: Path, content: String): Unit =
nodeFs.writeFileSync(path.underlying, content, js.Dictionary("mode" -> "a"))
def readEnv(varName: String): Option[String] =
nodeProcess.env.asInstanceOf[js.Dictionary[String]].get(varName)
def homeDir: Path =
Paths.get(nodeOs.homedir().asInstanceOf[String])

def createDirectories(path: Path): Unit =
nodeFs.mkdirSync(path.underlying, js.Dictionary("recursive" -> true))

// simple aliases, to avoid explicit imports of Files,
// which might point to _root_.java.nio.file.Files or _root_.caseapp.core.app.nio.Files
def exists(path: Path): Boolean =
Files.exists(path)
def isRegularFile(path: Path): Boolean =
Files.isRegularFile(path)
def deleteIfExists(path: Path): Boolean =
Files.deleteIfExists(path)
}
20 changes: 20 additions & 0 deletions core/js/src/main/scala/caseapp/core/app/nio/Files.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package caseapp.core.app.nio

import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}

object Files {

def exists(path: Path): Boolean =
nodeFs.existsSync(path.underlying).asInstanceOf[Boolean]
def createDirectories(path: Path): Unit =
nodeFs.mkdirSync(path.underlying, js.Dictionary("recursive" -> true))
def isRegularFile(path: Path): Boolean =
exists(path) && nodeFs.statSync(path.underlying).isFile().asInstanceOf[Boolean]
def deleteIfExists(path: Path): Boolean = {
nodeFs.rmSync(path.underlying, js.Dictionary("recursive" -> true))
true
}

private lazy val nodeFs = g.require("fs")
}
19 changes: 19 additions & 0 deletions core/js/src/main/scala/caseapp/core/app/nio/Path.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package caseapp.core.app.nio

import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}

final case class Path(underlying: String) {
import Path.nodePath
def resolve(chunk: String): Path =
Path(nodePath.join(underlying, chunk).asInstanceOf[String])
def getFileName: Path =
Path(nodePath.basename(underlying).asInstanceOf[String])
def getParent: Path =
Path(nodePath.join(underlying, "..").asInstanceOf[String])
override def toString: String = underlying
}

object Path {
private[nio] lazy val nodePath = g.require("path")
}
6 changes: 6 additions & 0 deletions core/js/src/main/scala/caseapp/core/app/nio/Paths.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package caseapp.core.app.nio

object Paths {
def get(path: String): Path =
Path(path)
}

This file was deleted.

34 changes: 34 additions & 0 deletions core/jvm-native/src/main/scala/caseapp/core/app/nio/FileOps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package caseapp.core.app.nio

import java.nio.charset.StandardCharsets
import java.nio.file.{FileAlreadyExistsException, Files, Path, Paths, StandardOpenOption}

object FileOps {

def readFile(path: Path): String =
new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
def writeFile(path: Path, content: String): Unit =
Files.write(path, content.getBytes(StandardCharsets.UTF_8))
def appendToFile(path: Path, content: String): Unit =
Files.write(path, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND)
def readEnv(varName: String): Option[String] =
Option(System.getenv(varName))
def homeDir: Path =
Paths.get(sys.props("user.home"))

def createDirectories(path: Path): Unit =
try Files.createDirectories(path)
catch {
// Ignored, see https://bugs.openjdk.java.net/browse/JDK-8130464
case _: FileAlreadyExistsException if Files.isDirectory(path) =>
}

// simple aliases, to avoid explicit imports of Files,
// which might point to _root_.java.nio.file.Files or _root_.caseapp.core.app.nio.Files
def exists(path: Path): Boolean =
Files.exists(path)
def isRegularFile(path: Path): Boolean =
Files.isRegularFile(path)
def deleteIfExists(path: Path): Boolean =
Files.deleteIfExists(path)
}
17 changes: 17 additions & 0 deletions core/jvm-native/src/main/scala/caseapp/core/app/nio/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package caseapp.core.app

package object nio {

type Path = java.nio.file.Path

object Paths {
def get(path: String): Path =
java.nio.file.Paths.get(path)
}

object File {
def separator: String =
java.io.File.separator
}

}
Loading

0 comments on commit 8db890b

Please sign in to comment.