-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpreload.js
More file actions
53 lines (45 loc) · 2.75 KB
/
Copy pathpreload.js
File metadata and controls
53 lines (45 loc) · 2.75 KB
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
50
51
52
53
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('electronAppAPI', {
// App info
getAppName: () => ipcRenderer.invoke('get-app-name'),
getAppPath: () => ipcRenderer.invoke('get-app-path'),
// Process lifecycle
spawnProcess: (command, args) => ipcRenderer.invoke('spawn-process', command, args),
writeToProcess: (instanceId, data) => ipcRenderer.invoke('write-to-process', instanceId, data),
onProcessStdout: (callback) => ipcRenderer.on('process-stdout', (_event, instanceId, line) => callback(instanceId, line)),
onProcessStderr: (callback) => ipcRenderer.on('process-stderr', (_event, instanceId, line) => callback(instanceId, line)),
onProcessClose: (callback) => ipcRenderer.on('process-close', (_event, instanceId, data) => callback(instanceId, data)),
onProcessError: (callback) => ipcRenderer.on('process-error', (_event, instanceId, err) => callback(instanceId, err)),
// Quit the app with an exit code (for CI)
quitApp: (exitCode) => ipcRenderer.invoke('quit-app', exitCode),
// Log to main process console
consoleLog: (message) => ipcRenderer.send('console-log', message),
// Flag to identify Electron environment
isElectron: true,
// CLI
getCliArgs: () => ipcRenderer.invoke('get-cli-args')
});
// the electronFSAPI is the fn that you need to copy to your election app impl for the fs to work.
contextBridge.exposeInMainWorld('electronFSAPI', {
// Path utilities
path: {
sep: process.platform === 'win32' ? '\\' : '/'
},
documentDir: () => ipcRenderer.invoke('get-documents-dir'),
homeDir: () => ipcRenderer.invoke('get-home-dir'),
tempDir: () => ipcRenderer.invoke('get-temp-dir'),
appLocalDataDir: () => ipcRenderer.invoke('get-app-data-dir'),
getWindowsDrives: () => ipcRenderer.invoke('get-windows-drives'),
showOpenDialog: (options) => ipcRenderer.invoke('show-open-dialog', options),
showSaveDialog: (options) => ipcRenderer.invoke('show-save-dialog', options),
// FS operations — results may be {__fsError, code, message} on failure since
// Electron IPC strips Error.code. The renderer (fslib_electron.js) handles unwrapping.
fsReaddir: (path) => ipcRenderer.invoke('fs-readdir', path),
fsStat: (path) => ipcRenderer.invoke('fs-stat', path),
fsMkdir: (path, options) => ipcRenderer.invoke('fs-mkdir', path, options),
fsUnlink: (path) => ipcRenderer.invoke('fs-unlink', path),
fsRmdir: (path, options) => ipcRenderer.invoke('fs-rmdir', path, options),
fsRename: (oldPath, newPath) => ipcRenderer.invoke('fs-rename', oldPath, newPath),
fsReadFile: (path) => ipcRenderer.invoke('fs-read-file', path),
fsWriteFile: (path, data) => ipcRenderer.invoke('fs-write-file', path, data)
});