-
Notifications
You must be signed in to change notification settings - Fork 0
/
preload.js
49 lines (45 loc) · 1.57 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// From https://stackoverflow.com/questions/57807459/how-to-use-preload-js-properly-in-electron
const { app, contextBridge } = require('electron');
const fs = require('fs').promises;
const path = require('path');
// Expose protected methods that allow the renderer process to use
// the ipcRenderer without exposing the entire object
/*
contextBridge.exposeInMainWorld(
"api", {
send: (channel, data) => {
// whitelist channels
let validChannels = ["toMain"];
if (validChannels.includes(channel)) {
ipcRenderer.send(channel, data);
}
},
receive: (channel, func) => {
let validChannels = ["fromMain"];
if (validChannels.includes(channel)) {
// Deliberately strip event as it includes `sender`
ipcRenderer.on(channel, (event, ...args) => func(...args));
}
}
}
);
*/
// FIXME: for some reason, app is undefined. Hm.
// app.getPath('userData');
const appDataPath = '/Users/thorcorreia/Library/Application\ Support/LightningELN';
contextBridge.exposeInMainWorld('fs', {
readFile: (filePath) => {
const fullPath = path.join(appDataPath, filePath);
console.log(`Reading file at ${fullPath}`);
return fs.readFile(fullPath, 'utf-8');
},
writeFile: (filePath, data) => {
const fullPath = path.join(appDataPath, filePath);
console.log(`Writing file at ${fullPath}`);
return fs.writeFile(fullPath, data);
},
readDir: (dirPath) => {
const fullPath = path.join(appDataPath, dirPath);
return fs.readdir(fullPath);
},
});