Lf94/tauri to electron (#3315)
* Get electron building something at all * Merge Frank test setup work (#3418) * Working window.electron.getPath * Loading project-specific settings in electron tests * Simplify test until we can get snapshots/traces working in electron tests * test tweaks --------- Co-authored-by: Frank Noirot <frank@kittycad.io> * add test #3375 and #3420 * put kcl files together * move files * can sort projects #3362 * File in the file pane should open with a single click #3385 * pressing delete on home screen should do nothing #3387 * add aria labels to icons * Rename and delete projects, also spam arrow keys when renaming #3364 #3365 #3259 * Fix up paths * Update flake.nix to support Electron * Remove a layer of indirection * Work without a web server * Fix settings#projectDir link on home * Fix login (requires new @kittycad/lib WHICH IS NOT INCLUDED HERE) * Lee: Tests are broken because auth skip needs to happen * get setting override envs passed through * tweak eletron CI * yml tweak * fmt * NUKE tauri shit post merge with main * another test auth tweak * Revert "another test auth tweak" This reverts commitb2254b10af. * try CI again * CI tweaks * SKIP_AUTH true now on playwright * Skipping auth when NODE_ENV=development now * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * Use BASE_URL() * fix exists Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix foldername for macos Signed-off-by: Jess Frazelle <github@jessfraz.com> * update for windows Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix version in lower right Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup unused imports Signed-off-by: Jess Frazelle <github@jessfraz.com> * progress on is playwright Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix test folders Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove tauri from actions bullshit Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove tauri dir Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups the coredump async shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * node env dev Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix cancellable Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup unnessary things Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * env vars Signed-off-by: Jess Frazelle <github@jessfraz.com> * Bring back fix for NOT using hardcoded main.kcl * env Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * Revert "updates" This reverts commit da5d9f1043eb94404e8b3f8044088e990e34a4ef. * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove tauri clippuy Signed-off-by: Jess Frazelle <github@jessfraz.com> * less retries for now, no debug Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * tsconfig Signed-off-by: Jess Frazelle <github@jessfraz.com> * small tsc fix * update some tsc Signed-off-by: Jess Frazelle <github@jessfraz.com> * tsc env Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix other tsc Signed-off-by: Jess Frazelle <github@jessfraz.com> * small change for routeLoaders * rm old screenshot Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix auth Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix last onew Signed-off-by: Jess Frazelle <github@jessfraz.com> * auth clean up * fix package.json Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * dissmissed screen on tests * add waits between files being written * put back retried Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix weird programMemory Map issue * put private back * Revert "put private back" This reverts commitd311b978ca. * Revert "fix weird programMemory Map issue" This reverts commit6c387bdf62. * remove serde-wasm-bindgen Signed-off-by: Jess Frazelle <github@jessfraz.com> * add env Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * fix tests * more test tweaks * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * another tweak * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu) * more test tweaks * more tweaks * increase macos timeout * try fix macos * disable macos playwright tests --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch> Co-authored-by: Frank Noirot <frank@kittycad.io> Co-authored-by: Adam Sunderland <iterion@gmail.com> Co-authored-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: Jess Frazelle <jessfraz@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
		
							
								
								
									
										126
									
								
								src/preload.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										126
									
								
								src/preload.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,126 @@
 | 
			
		||||
import { ipcRenderer, contextBridge } from 'electron'
 | 
			
		||||
import path from 'path'
 | 
			
		||||
import fs from 'node:fs/promises'
 | 
			
		||||
import os from 'node:os'
 | 
			
		||||
import fsSync from 'node:fs'
 | 
			
		||||
import packageJson from '../package.json'
 | 
			
		||||
import { MachinesListing } from 'lib/machineManager'
 | 
			
		||||
 | 
			
		||||
const open = (args: any) => ipcRenderer.invoke('dialog.showOpenDialog', args)
 | 
			
		||||
const save = (args: any) => ipcRenderer.invoke('dialog.showSaveDialog', args)
 | 
			
		||||
const openExternal = (url: any) => ipcRenderer.invoke('shell.openExternal', url)
 | 
			
		||||
const showInFolder = (path: string) =>
 | 
			
		||||
  ipcRenderer.invoke('shell.showItemInFolder', path)
 | 
			
		||||
const login = (host: string): Promise<string> =>
 | 
			
		||||
  ipcRenderer.invoke('login', host)
 | 
			
		||||
 | 
			
		||||
const isMac = os.platform() === 'darwin'
 | 
			
		||||
const isWindows = os.platform() === 'win32'
 | 
			
		||||
const isLinux = os.platform() === 'linux'
 | 
			
		||||
 | 
			
		||||
const readFile = (path: string) => fs.readFile(path, 'utf-8')
 | 
			
		||||
// It seems like from the node source code this does not actually block but also
 | 
			
		||||
// don't trust me on that (jess).
 | 
			
		||||
const exists = (path: string) => fsSync.existsSync(path)
 | 
			
		||||
const rename = (prev: string, next: string) => fs.rename(prev, next)
 | 
			
		||||
const writeFile = (path: string, data: string | Uint8Array) =>
 | 
			
		||||
  fs.writeFile(path, data, 'utf-8')
 | 
			
		||||
const readdir = (path: string) => fs.readdir(path, 'utf-8')
 | 
			
		||||
const stat = (path: string) =>
 | 
			
		||||
  fs.stat(path).catch((e) => Promise.reject(e.code))
 | 
			
		||||
// Electron has behavior where it doesn't clone the prototype chain over.
 | 
			
		||||
// So we need to call stat.isDirectory on this side.
 | 
			
		||||
const statIsDirectory = (path: string) =>
 | 
			
		||||
  stat(path).then((res) => res.isDirectory())
 | 
			
		||||
const getPath = async (name: string) => ipcRenderer.invoke('app.getPath', name)
 | 
			
		||||
 | 
			
		||||
const exposeProcessEnvs = (varNames: Array<string>) => {
 | 
			
		||||
  const envs: Record<string, string> = {}
 | 
			
		||||
  varNames.forEach((varName) => {
 | 
			
		||||
    const envVar = process.env[varName]
 | 
			
		||||
    if (envVar) {
 | 
			
		||||
      envs[varName] = envVar
 | 
			
		||||
    }
 | 
			
		||||
  })
 | 
			
		||||
  return envs
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const kittycad = (access: string, args: any) =>
 | 
			
		||||
  ipcRenderer.invoke('kittycad', { access, args })
 | 
			
		||||
 | 
			
		||||
// We could probably do this from the renderer side, but I fear CORS will
 | 
			
		||||
// bite our butts.
 | 
			
		||||
const listMachines = async (): Promise<MachinesListing> => {
 | 
			
		||||
  const machineApi = await ipcRenderer.invoke('find_machine_api')
 | 
			
		||||
  if (!machineApi) return {}
 | 
			
		||||
 | 
			
		||||
  return fetch(`http://${machineApi}/machines`).then((resp) => resp.json())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const getMachineApiIp = async (): Promise<String | null> =>
 | 
			
		||||
  ipcRenderer.invoke('find_machine_api')
 | 
			
		||||
 | 
			
		||||
contextBridge.exposeInMainWorld('electron', {
 | 
			
		||||
  login,
 | 
			
		||||
  // 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.
 | 
			
		||||
  readFile,
 | 
			
		||||
  writeFile,
 | 
			
		||||
  exists,
 | 
			
		||||
  readdir,
 | 
			
		||||
  rename,
 | 
			
		||||
  rm: fs.rm,
 | 
			
		||||
  path,
 | 
			
		||||
  stat,
 | 
			
		||||
  statIsDirectory,
 | 
			
		||||
  mkdir: fs.mkdir,
 | 
			
		||||
  // opens a dialog
 | 
			
		||||
  open,
 | 
			
		||||
  save,
 | 
			
		||||
  // opens the URL
 | 
			
		||||
  openExternal,
 | 
			
		||||
  showInFolder,
 | 
			
		||||
  getPath,
 | 
			
		||||
  packageJson,
 | 
			
		||||
  arch: process.arch,
 | 
			
		||||
  platform: process.platform,
 | 
			
		||||
  version: process.version,
 | 
			
		||||
  join: path.join,
 | 
			
		||||
  sep: path.sep,
 | 
			
		||||
  os: {
 | 
			
		||||
    isMac,
 | 
			
		||||
    isWindows,
 | 
			
		||||
    isLinux,
 | 
			
		||||
  },
 | 
			
		||||
  // IMPORTANT NOTE: kittycad.ts reads process.env.BASE_URL. But there is
 | 
			
		||||
  // no way to set it across the bridge boundary. We need to make it a command.
 | 
			
		||||
  setBaseUrl: (value: string) => (process.env.BASE_URL = value),
 | 
			
		||||
  process: {
 | 
			
		||||
    // Setter/getter has to be created because
 | 
			
		||||
    // these are read-only over the boundary.
 | 
			
		||||
    env: Object.assign(
 | 
			
		||||
      {},
 | 
			
		||||
      exposeProcessEnvs([
 | 
			
		||||
        'NODE_ENV',
 | 
			
		||||
        'TEST_SETTINGS_FILE_KEY',
 | 
			
		||||
        'VITE_KC_API_WS_MODELING_URL',
 | 
			
		||||
        'VITE_KC_API_BASE_URL',
 | 
			
		||||
        'VITE_KC_SITE_BASE_URL',
 | 
			
		||||
        'VITE_KC_SKIP_AUTH',
 | 
			
		||||
        'VITE_KC_CONNECTION_TIMEOUT_MS',
 | 
			
		||||
        'VITE_KC_DEV_TOKEN',
 | 
			
		||||
        'IS_PLAYWRIGHT',
 | 
			
		||||
 | 
			
		||||
        // Really we shouldn't use these and our code should use NODE_ENV
 | 
			
		||||
        'DEV',
 | 
			
		||||
        'PROD',
 | 
			
		||||
        'TEST',
 | 
			
		||||
        'CI',
 | 
			
		||||
      ])
 | 
			
		||||
    ),
 | 
			
		||||
  },
 | 
			
		||||
  kittycad,
 | 
			
		||||
  listMachines,
 | 
			
		||||
  getMachineApiIp,
 | 
			
		||||
})
 | 
			
		||||
		Reference in New Issue
	
	Block a user