Files
modeling-app/src/lib/electron.ts

75 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-07-24 21:37:29 -04:00
import { ipcRenderer, contextBridge } from 'electron'
import path from 'path'
2024-07-24 21:37:29 -04:00
import fs from 'node:fs/promises'
import packageJson from '../../package.json'
2024-07-31 15:31:36 -04:00
const open = (args: any) => ipcRenderer.invoke('dialog.showOpenDialog', args)
const save = (args: any) => ipcRenderer.invoke('dialog.showSaveDialog', args)
2024-07-31 15:08:18 -04:00
const openExternal = (url: any) => ipcRenderer.invoke('shell.openExternal', url)
2024-07-31 15:31:55 -04:00
const showInFolder = (path: string) =>
ipcRenderer.invoke('shell.showItemInFolder', path)
2024-08-01 12:18:32 -04:00
const login = (host: string): Promise<string> =>
ipcRenderer.invoke('login', host)
2024-07-30 15:58:27 -04:00
2024-07-24 21:37:29 -04:00
const readFile = (path: string) => fs.readFile(path, 'utf-8')
2024-07-30 15:58:27 -04:00
const rename = (prev: string, next: string) => fs.rename(prev, next)
2024-07-31 16:54:32 -04:00
const writeFile = (path: string, data: string | Uint8Array) =>
2024-07-26 15:52:35 -04:00
fs.writeFile(path, data, 'utf-8')
2024-07-24 21:37:29 -04:00
const readdir = (path: string) => fs.readdir(path, 'utf-8')
2024-07-31 15:31:55 -04:00
const stat = (path: string) =>
fs.stat(path).catch((e) => Promise.reject(e.code))
2024-07-26 15:52:35 -04:00
// Electron has behavior where it doesn't clone the prototype chain over.
// So we need to call stat.isDirectory on this side.
2024-07-31 15:31:55 -04:00
const statIsDirectory = (path: string) =>
stat(path).then((res) => res.isDirectory())
2024-07-24 21:37:29 -04:00
const getPath = async (name: string) => ipcRenderer.invoke('app.getPath', name)
2024-07-26 15:52:35 -04:00
const exposeProcessEnv = (varName: string) => {
return {
[varName](value?: string) {
if (value !== undefined) {
process.env[varName] = value
} else {
return process.env[varName]
}
},
}
}
import('@kittycad/lib').then((kittycad) => {
contextBridge.exposeInMainWorld('electron', {
2024-07-31 14:22:32 -04:00
login,
2024-07-30 15:58:27 -04:00
// Passing fs directly is not recommended since it gives a lot of power
// to the browser side / potential malicious code. We restrict what is
// exported.
2024-07-26 15:52:35 -04:00
readFile,
writeFile,
readdir,
2024-07-30 15:58:27 -04:00
rename,
rm: fs.rm,
2024-07-26 15:52:35 -04:00
path,
stat,
statIsDirectory,
mkdir: fs.mkdir,
2024-07-30 15:58:27 -04:00
// opens a dialog
open,
2024-07-31 15:31:36 -04:00
save,
2024-07-31 15:08:18 -04:00
// opens the URL
openExternal,
2024-07-30 15:58:27 -04:00
showInFolder,
2024-07-26 15:52:35 -04:00
getPath,
packageJson,
2024-07-31 15:08:18 -04:00
arch: process.arch,
2024-07-26 15:52:35 -04:00
platform: process.platform,
2024-07-31 15:08:18 -04:00
version: process.version,
2024-07-26 15:52:35 -04:00
process: {
// Setter/getter has to be created because
// these are read-only over the boundary.
env: Object.assign({}, exposeProcessEnv('BASE_URL')),
},
kittycad: {
users: kittycad.users,
},
})
})