question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fs with kvision-electron

See original GitHub issue

I’m building off the kvision electron template, and trying to read a files contents. There seem to be interfaces for accessing all sorts of things (like dialogs, single instance applications, the shell, etc) but nothing for reading / writing to disk.

I tried using fs from the node-ket library, as this little code snippet shows:

import node.fs.fs

val remote: Remote = require("electron").remote
val file = remote.dialog.showOpenDialog(remote.getCurrentWindow(), object : OpenDialogOptions{})

val contents = fs.readFileStringSync(file, "utf8")

but that gives me the build error When accessing module declarations from UMD, they must be marked by both @JsModule and @JsNonModule

Entire ElectronApp.kt file with this code in (just drop into the kvision template-electron to try it):

package com.example

import node.fs.fs
import pl.treksoft.kvision.Application
import pl.treksoft.kvision.electron.BrowserWindow
import pl.treksoft.kvision.electron.OpenDialogOptions
import pl.treksoft.kvision.electron.Remote
import pl.treksoft.kvision.electron.dialog
import pl.treksoft.kvision.electron.nodejs.Process
import pl.treksoft.kvision.html.button
import pl.treksoft.kvision.html.div
import pl.treksoft.kvision.i18n.DefaultI18nManager
import pl.treksoft.kvision.i18n.I18n
import pl.treksoft.kvision.i18n.I18n.tr
import pl.treksoft.kvision.panel.FlexAlignItems
import pl.treksoft.kvision.panel.hPanel
import pl.treksoft.kvision.panel.root
import pl.treksoft.kvision.panel.vPanel
import pl.treksoft.kvision.require
import pl.treksoft.kvision.startApplication
import pl.treksoft.kvision.utils.createInstance
import pl.treksoft.kvision.utils.obj
import pl.treksoft.kvision.utils.px
import kotlin.browser.window

external val process: Process

class ElectronApp : Application() {

    val remote: Remote = require("electron").remote
    private var nativeWindow: BrowserWindow? = null

    override fun start() {

        I18n.manager =
            DefaultI18nManager(
                mapOf(
                    "en" to require("i18n/messages-en.json"),
                    "pl" to require("i18n/messages-pl.json")
                )
            )

        root("kvapp") {
            vPanel(alignItems = FlexAlignItems.CENTER, spacing = 30) {
                marginTop = 50.px
                fontSize = 30.px
                hPanel(spacing = 20) {
                    div(tr("Electron version"))
                    div("${process.versions?.electron}")
                    button("open file") {
                        onClick {
                            val remote: Remote = require("electron").remote
                            val file = remote.dialog.showOpenDialog(remote.getCurrentWindow(), object : OpenDialogOptions{})
                            val contents = fs.readFileStringSync(file, "utf8")
                            this.text = contents
                        }
                    }
                }
                hPanel(spacing = 20) {
                    div(tr("Chrome version"))
                    div("${process.versions?.chrome}")
                }
            }
        }

        nativeWindow = remote.BrowserWindow.createInstance(obj {
            title = "Native window"
            width = 700
            height = 500
        })
        nativeWindow?.on("closed")
        { _ ->
            nativeWindow = null
        }

        window.onunload = {
            nativeWindow?.destroy()
            nativeWindow = null
            Unit
        }
    }

    override fun dispose(): Map<String, Any> {
        super.dispose()
        nativeWindow?.destroy()
        nativeWindow = null
        return mapOf()
    }
}

fun main() {
    startApplication(::ElectronApp)
}

Any help on getting this to work would be fantastic.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rjaroscommented, Jul 19, 2020

If you (or anyone else!) would like to contribute, the generated code in the pl.treksoft.kvision.electron package would certainly need some cleanups (it gives me a lot of warnings), but I’m not able to get through all of it myself.

0reactions
rjaroscommented, Jul 29, 2020

As JetBrains added experimental support for NodeJS API in Kotlin 1.4-rc, I plan to remove most of the NodeJS bindings I’ve created lately. They are generated from the same TypeScript declarations. Only the Electron bindings will be provided by KVision, and all NodeJS API will be available from JB bindings. Therefore some package changes will be required during migration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

alltypes - kvision
Base interface for applications. pl.treksoft.kvision.chart.ArcOptions. Chart arc options. pl.treksoft.kvision.electron.AuthInfo · pl.
Read more >
NodeJS FileSystem: Get Information about a file with fs.stat()
In this video, we looked at how we can get information about a file using the fs.stat() function of the file system module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found