首页 > 其他分享 >electron暴露配置文件(用户可随时修改)

electron暴露配置文件(用户可随时修改)

时间:2024-03-07 18:14:28浏览次数:25  
标签:const 配置文件 err 暴露 electron api ini config

配置文件

一般web前端项目配置文件,写死的放在src/config下,需要打包配置的放在.env文件中。但在electron项目中,如果配置数据更改,需要每次给用户打包升级肯定是行不通的。于是外部配置文件就是有必要的,具体实现方法也比较简单,通过fs去读写外部文件就可实现

具体实现

设置文件不被压缩混淆

比如配置文件放在根目录的config文件夹
配置electron- builder文件,我这里是yml配置

...
productName: xxx
asarUnpack:
  - resources/**
extraResources:
  - ./config
...

extraResources属性添加文件夹名称
打包后路径为/resources/config/...可以打包后查看

获取路径

process.cwd()
此时获取是node服务的根路径,再拼接上本地文件的路径
dev环境为项目根目录
prod环境为安装后文件夹路径

const path = process.cwd()
const filePath = is.dev
  ? join(path, '/config/app.properties)
  : join(path, '/resources/config/app.properties')

读写文件

这里用到了fspathini等node模块,所以不可以在renderer里面操作,要通过主进程handle通信到渲染进程获取

npm i ini

class ConfigHandle {
  private getConfig(_: IpcMainInvokeEvent) {
    return new Promise((resolve, reject) => {
      fs.readFile(filePath, 'utf8', function (err, dataStr) {
        if (err) {
          return reject(err.message)
        }
        resolve(ini.parse(dataStr.toString()))
      })
    })
  }

  private setConfig(_: IpcMainInvokeEvent, config) {
    return new Promise((resolve, reject) => {
      fs.readFile(filePath, 'utf8', function (err, dataStr) {
        if (err) {
          return reject(err.message)
        }
        const origin = ini.parse(dataStr.toString())
        // 这里做了先读取再assign操作,不会全量覆盖
        fs.writeFile(filePath, ini.stringify(Object.assign(origin, config)), function (err) {
          if (err) {
            return reject(err.message)
          }
          resolve('success')
        })
      })
    })
  }

  register() {
    ipcMain.handle('get-config', this.getConfig)
    ipcMain.handle('set-config', this.setConfig)
  }
}

通信到renderer

  • main
configHandle.register()
  • preload
const api = {
  config: {
    get: () => ipcRenderer.invoke('get-config'),
    set: (config: object) => ipcRenderer.invoke('set-config', config)
  }
}

contextBridge.exposeInMainWorld('api', api)
  • renderer
export const config = await window.api.config.get()
export const setConfig = config => window.api.config.set(config)


const baseUrl = config.baseUrl

setConfig({baseUrl: "http://xxx"})

这样可以通过程序修改配置文件,或者用户自己编辑修改配置文件

  • config/app.properties
title=good title
baseUrl=great url
defaultPassword=unbelievable pwd

通过ini.parse会转成json格式,非常方便

标签:const,配置文件,err,暴露,electron,api,ini,config
From: https://www.cnblogs.com/rion1234567/p/18059244

相关文章

  • 企业级桌面软件开发框架electron
    https://gitee.com/dromara/electron-egg最大优点是可以生成桌面应用软件包;开发文档:https://www.kaka996.com/pages/c9eed1/运用它的---》远程模式功能,将生产环境地址设置到config.remoteUrl即可打包生成;具体操作步骤:vue项目启动npmrundev打包npmrunbuild生成......
  • SpringBoot3+Consul配置,启动后,居然不读bootstrap.yml的配置文件,直接连本地localhost:8
    问题描述如题。bootstrap.yml的配置文件: consul控制台打印的日志: 解决方案:booststrap.yml的配置文件缩进搞错了,所以压根就没有读到配置。正确的缩进:  ......
  • The Marvels of an Electronic Technician's World
    Welcometothefascinatingworldofelectronictechnicians!Inthisblogpost,wewilldiveintotheexcitingrealmoftheseunsungheroeswhoworkbehindthescenestokeepourgadgetsrunningsmoothly.Fromtroubleshootingtorepairing,anelectronicte......
  • 简单懂点k8s网络(4)K8S对外暴露方案
       ......
  • springboot - 配置文件 @ConfigurationProperties
    1.简单属性@Configuration@ConfigurationProperties(prefix="mail")publicclassConfigProperties{privateStringhostName;privateintport;privateStringfrom;//standardgettersandsetters}注意:如果我们不在POJO中使用@Configurati......
  • go-ini配置文件操作
    go-ini是golangini文件操作库,也兼容其他格式,包括但不限于my.cnf、.gitconfig使用三方库"gopkg.in/ini.v1"ini文件格式语法;注释使用分号;ini文件语法env=test;[]分区符[mysql]Host=127.0.0.1Port=3306User=rootPassword=123456D......
  • WPF 应用迁移到 Electron 框架过程记录
    前一段时间我用WPF开发了一个查看emoji表情的小工具https://github.com/he55/EmojiViewer,由于最近我使用macOS系统比较多,我想能在macOS系统上也能使用这个工具。于是我尝试将WPF应用迁移到Electron框架,感觉这个框架很强大,在这里记录一下应用迁移的过程。安装Elec......
  • Tomcat 的组成结构和主配置文件Server.xml详解
    Tomcat的组成结构和主配置文件Server.xml详解参考:《TOMCAT与JAVAWEB开发技术详解第3版》https://www.jianshu.com/p/2789af11299fTomcat本身由一系列可配置的组件构成,其中核心组件是Servlet容器组件,它是所有其他Tomcat组件的顶层容器,用<CATALINA_HOME>表示Tomca......
  • Tomcat 的组成结构和主配置文件Server.xml详解
    Tomcat的组成结构和主配置文件Server.xml详解参考:《TOMCAT与JAVAWEB开发技术详解第3版》https://www.jianshu.com/p/2789af11299fTomcat本身由一系列可配置的组件构成,其中核心组件是Servlet容器组件,它是所有其他Tomcat组件的顶层容器,用<CATALINA_HOME>表示Tomca......
  • SSM整合之使用配置类替换xml配置文件(2)
    SSM整合就是将MVC三层架构和框架核心API组件交给SpringIoC容器管理!一般需要配置两个IoC容器进行三层架构组件管理。容器名盛放组件web容器web相关组件(controller,springmvc核心组件)root容器业务和持久层相关组件(service,aop,tx,dataSource,mybatis,mapper等)w......