1
0
Fork 0
mirror of https://github.com/anyproto/anytype-ts.git synced 2025-06-08 14:07:01 +09:00
anytype-ts/electron/js/preload.js
Andrew Simachev 7ba2eedb1a
JS-2703: fix
2023-12-12 12:38:56 +01:00

64 lines
No EOL
1.7 KiB
JavaScript

const { ipcRenderer, contextBridge } = require('electron');
const { app, getCurrentWindow, getGlobal, dialog, BrowserWindow } = require('@electron/remote');
const fs = require('fs');
const os = require('os');
const path = require('path');
const readChunk = require('read-chunk');
const fileType = require('file-type');
const userPath = app.getPath('userData');
const tmpPath = app.getPath('temp');
const logPath = path.join(userPath, 'logs');
contextBridge.exposeInMainWorld('Electron', {
version: {
app: app.getVersion(),
os: [ os.platform(), process.arch, process.getSystemVersion() ].join(' '),
system: process.getSystemVersion(),
device: os.hostname(),
},
platform: os.platform(),
arch: process.arch,
isPackaged: app.isPackaged,
userPath,
tmpPath,
logPath,
currentWindow: () => getCurrentWindow(),
isMaximized: () => BrowserWindow.getFocusedWindow()?.isMaximized(),
getGlobal: (key) => getGlobal(key),
showOpenDialog: dialog.showOpenDialog,
fileParam: (path) => {
const stat = fs.statSync(path);
const buffer = readChunk.sync(path, 0, stat.size);
const type = fileType(buffer);
return { buffer, type };
},
fileWrite: (name, data, options) => {
name = String(name || 'temp');
options = options || {};
const fn = path.parse(name).base;
const fp = path.join(tmpPath, fn);
options.mode = 0o666;
fs.writeFileSync(fp, data, options);
return fp;
},
dirname: fp => path.dirname(fp),
on: (event, callBack) => ipcRenderer.on(event, callBack),
removeAllListeners: (event) => ipcRenderer.removeAllListeners(event),
Api: (id, cmd, args) => {
id = Number(id) || 0;
cmd = String(cmd || '');
args = args || [];
ipcRenderer.invoke('Api', id, cmd, args);
},
});