Reputation: 375
I am starting a project in Kotlin-Multiplatform. Accessing the resource folder via Java is quite easy, but I don't know how to access it via JS targeting Node.
During testing, I found out that the resource file is stored in a separate folder. If I'm not mistaken:
build/js
|-- package
| |--project
| |--project-test \\tests are executed here via calling __dirname in node
|
|--processedResources
|-- testfile.txt \\ files in the resource folder are stored here
I would like to know: how is it done via JS/Node?
Upvotes: 3
Views: 1301
Reputation: 5382
There is a way using pure kotlinx (no javascript involved), but I don't know how stable the relative path is. If it doesn't work, try with more or fewer ../
.
import kotlinx.io.buffered
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import kotlinx.io.readString
import kotlin.test.Test
class ReadFileTest {
@Test
fun readFileTest() {
val file = Path("../../../processedResources/testfile.txt")
val fullDoc = SystemFileSystem.source(file).buffered().readString()
println(fullDoc)
}
}
Btw I access my ressources using ../../../../src/jsTest/resources/
Upvotes: 0
Reputation: 925
You can try to use fs
:
external fun require(name: String): dynamic
external val __dirname: dynamic
val fs = require("fs")
val path = require("path");
fun main() {
val path = path.join(
__dirname,
"..\\..\\..\\..",
"processedResources",
"js",
"main",
"test.txt"
)
println(fs.readFileSync(path, "utf8"))
}
Upvotes: 1