2023-10-17 09:27:08 +02:00
|
|
|
|
import { resolve } from 'node:path'
|
2023-09-15 13:32:13 +02:00
|
|
|
|
import { contextBridge } from 'electron'
|
|
|
|
|
|
2023-10-18 05:34:40 +02:00
|
|
|
|
export const isPackaged = process.env.IS_PACKAGED === 'true'
|
2023-10-17 09:27:08 +02:00
|
|
|
|
|
2023-10-18 05:34:40 +02:00
|
|
|
|
export const extraResolve = (value) => {
|
|
|
|
|
return isPackaged
|
2023-10-17 09:27:08 +02:00
|
|
|
|
? resolve(process.resourcesPath, `extra/${value}`)
|
|
|
|
|
: resolve(`electron/resources/extra/${value}`)
|
2023-10-18 05:34:40 +02:00
|
|
|
|
}
|
2023-10-17 09:27:08 +02:00
|
|
|
|
|
|
|
|
|
export const buildResolve = value =>
|
2023-10-22 18:43:52 +02:00
|
|
|
|
resolve(`electron/resources/build/${value}`)
|
2023-10-17 09:27:08 +02:00
|
|
|
|
|
2023-09-18 09:07:28 +02:00
|
|
|
|
export function exposeContext(key, value) {
|
2023-09-15 13:32:13 +02:00
|
|
|
|
if (process.contextIsolated) {
|
|
|
|
|
try {
|
|
|
|
|
contextBridge.exposeInMainWorld(key, value)
|
|
|
|
|
}
|
|
|
|
|
catch (error) {
|
|
|
|
|
console.error(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
window[key] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-18 13:23:00 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建一个代理对象,将目标对象的指定方法转发并执行。
|
|
|
|
|
*
|
|
|
|
|
* @param {object} targetObject - 目标对象,包含要代理的方法。
|
|
|
|
|
* @param {string[]} methodNames - 要代理的方法名称数组。
|
|
|
|
|
* @returns {object} - 代理对象,包含转发的方法。
|
|
|
|
|
*/
|
|
|
|
|
export function createProxy(targetObject, methodNames) {
|
|
|
|
|
return methodNames.reduce((proxyObj, methodName) => {
|
|
|
|
|
proxyObj[methodName] = (...args) => targetObject[methodName](...args)
|
|
|
|
|
|
|
|
|
|
return proxyObj
|
|
|
|
|
}, {})
|
|
|
|
|
}
|