diff --git a/src/lib/desktop.ts b/src/lib/desktop.ts index 84f975bc8..6260d60ab 100644 --- a/src/lib/desktop.ts +++ b/src/lib/desktop.ts @@ -8,10 +8,6 @@ import { ProjectRoute } from 'wasm-lib/kcl/bindings/ProjectRoute' import { components } from './machine-api' import { isDesktop } from './isDesktop' -// All these functions call into lib/electron since many require filesystem -// access, and the second half is the original tauri code also stored app -// state on the "desktop" side. - // Get the app state from desktop. export async function getState(): Promise { if (!isDesktop()) { @@ -45,7 +41,7 @@ export async function renameProjectDirectory( projectPath: string, newName: string ): Promise { - return invoke('rename_project_directory', { projectPath, newName }) + return window.electron.ipcRenderer.invoke('rename_project_directory', { projectPath, newName }) } // Get the initial default dir for holding all projects. diff --git a/src/lib/electron.ts b/src/lib/electron.ts index 2e4b6084d..5e2291463 100644 --- a/src/lib/electron.ts +++ b/src/lib/electron.ts @@ -1,8 +1,19 @@ -import { app, ipcMain } from 'electron' +import { contextBridge } from 'electron' +import path from 'path' +import fs from 'node:fs' + +// All these functions call into lib/electron since many require filesystem +// access, and the second half is the original tauri code also stored app +// state on the "desktop" side. const DEFAULT_HOST = "https://api.zoo.dev" const SETTINGS_FILE_NAME = "settings.toml" const PROJECT_SETTINGS_FILE_NAME = "project.toml" const PROJECT_FOLDER = "zoo-modeling-app-projects" - +contextBridge.exposeInMainWorld("fs", { + readFile(p: string) { return fs.readFile(p, 'utf-8') }, + readdir(p: string) { return fs.readdir(p, 'utf-8') }, + join() { return path.join(...arguments) }, + exists(p: string) { fs.exists(p) }, +}) diff --git a/src/lib/fs.ts b/src/lib/fs.ts deleted file mode 100644 index 34438f694..000000000 --- a/src/lib/fs.ts +++ /dev/null @@ -1,18 +0,0 @@ -const { app, ipcMain } = require('electron') -const path = require('node:path') -const fs = require('node:fs') - -app.whenReady().then(() => { - ipcMain.handle('readFile', async (event) => { - return fs.readFile(event.data[0], 'utf-8') - }) - ipcMain.handle('readdir', async (event) => { - return fs.readdir(event.data[0], 'utf-8') - }) - ipcMain.handle('join', async (event) => { - return path.join(event.data[0]) - }) - ipcMain.handle('exists', async (event) => { - return fs.exists(event.data[0]) - }) -}) diff --git a/src/main.ts b/src/main.ts index 1188934ad..1ddf953a8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,6 @@ import { app, BrowserWindow } from 'electron' import path from 'path' -import 'lib/fs' // Handle creating/removing shortcuts on Windows when installing/uninstalling. if (require('electron-squirrel-startup')) { @@ -14,6 +13,12 @@ const createWindow = () => { const mainWindow = new BrowserWindow({ width: 800, height: 600, + webPreferences: { + nodeIntegration: false, // do not give the application implicit system access + contextIsolation: true, // expose system functions in preload + sandbox: false, // expose nodejs in preload + preload: path.join(__dirname, "./preload.js") + } }) // and load the index.html of the app. diff --git a/src/preload.ts b/src/preload.ts index e69de29bb..1fc8bcdcd 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -0,0 +1 @@ +import 'lib/electron' diff --git a/vite.preload.config.ts b/vite.preload.config.ts index b28ddbd38..f0f11c216 100644 --- a/vite.preload.config.ts +++ b/vite.preload.config.ts @@ -1,6 +1,7 @@ import type { ConfigEnv, UserConfig } from 'vite' import { defineConfig, mergeConfig } from 'vite' import { getBuildConfig, external, pluginHotRestart } from './vite.base.config' +import viteTsconfigPaths from 'vite-tsconfig-paths' // https://vitejs.dev/config export default defineConfig((env) => { @@ -22,7 +23,7 @@ export default defineConfig((env) => { }, }, }, - plugins: [pluginHotRestart('reload')], + plugins: [pluginHotRestart('reload'), viteTsconfigPaths()], } return mergeConfig(getBuildConfig(forgeEnv), config)