-
Notifications
You must be signed in to change notification settings - Fork 27
Open sketch source #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: alpha
Are you sure you want to change the base?
Open sketch source #635
Changes from 1 commit
bfa591a
d3f977e
53abea6
36b6b0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import path from 'path' | ||
| import fs from 'fs' | ||
| import { shell } from 'electron' | ||
|
|
||
| /** | ||
| * Opens the source file (index.ts or index.js) for a sketch module in the default app. | ||
| * @param sketchesDir The absolute path to the sketches directory | ||
| * @param moduleId The sketch's moduleId | ||
| * @returns Promise<{ success: boolean; error?: string }> | ||
| */ | ||
| export const openSketchSourceFile = async ( | ||
| sketchesDir: string, | ||
| moduleId: string, | ||
| ): Promise<{ success: boolean; error?: string }> => { | ||
| try { | ||
| const tsPath = path.join(sketchesDir, moduleId, 'index.ts') | ||
| const jsPath = path.join(sketchesDir, moduleId, 'index.js') | ||
| let filePath = tsPath | ||
| if (fs.existsSync(tsPath)) { | ||
| filePath = tsPath | ||
| } else if (fs.existsSync(jsPath)) { | ||
| filePath = jsPath | ||
| } else { | ||
| return { success: false, error: 'Source file not found' } | ||
|
||
| } | ||
| const openResult = await shell.openPath(filePath) | ||
| if (openResult) { | ||
| return { success: false, error: openResult } | ||
| } | ||
| return { success: true } | ||
| } catch (err) { | ||
| return { | ||
| success: false, | ||
| error: err instanceof Error ? err.message : 'Unknown error opening file', | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { app, BrowserWindow, dialog, ipcMain, screen, session } from 'electron' | |||||
| import { electronApp, optimizer } from '@electron-toolkit/utils' | ||||||
| import { REDUX_DEVTOOLS, installExtension } from '@tomjs/electron-devtools-installer' | ||||||
| import { ProjectData } from '@hedron-gl/app-store' | ||||||
| import { openSketchSourceFile } from './handlers/openSketchSourceFile' | ||||||
|
||||||
| import { openSketchSourceFile } from './handlers/openSketchSourceFile' | |
| import { openSketchSourceFile } from '@main/handlers/openSketchSourceFile' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ignore this one @cale-bradbury. Our eslint rules dictate this sort of thing. They disallow going back up the tree (e.g. ../../foo) but this way round is fine.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| // openSketchSourceFile.ts | ||||||||||||||||||||||||||||||||||||||||||||||
| // Utility to open the source file for a sketch in the default program | ||||||||||||||||||||||||||||||||||||||||||||||
| import { appStore } from '@renderer/appStore' | ||||||||||||||||||||||||||||||||||||||||||||||
| import { FileEvents } from '@shared/Events' | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||
| * Attempts to open the source file (index.ts or index.js) for a sketch module in the default app. | ||||||||||||||||||||||||||||||||||||||||||||||
| * @param moduleId The sketch's moduleId | ||||||||||||||||||||||||||||||||||||||||||||||
| * @returns Promise<void> | ||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||
| export async function openSketchSourceFile(moduleId: string) { | ||||||||||||||||||||||||||||||||||||||||||||||
| const sketchesDir = appStore.getState().sketchesDir | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!sketchesDir) { | ||||||||||||||||||||||||||||||||||||||||||||||
| alert('No sketches directory set') | ||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| // Ask main process to open the file | ||||||||||||||||||||||||||||||||||||||||||||||
| const result = await window.electronApi.ipcRenderer.invoke( | ||||||||||||||||||||||||||||||||||||||||||||||
| FileEvents.OpenSketchSourceFile, | ||||||||||||||||||||||||||||||||||||||||||||||
| sketchesDir, | ||||||||||||||||||||||||||||||||||||||||||||||
| moduleId, | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
| if (!result?.success) { | ||||||||||||||||||||||||||||||||||||||||||||||
| alert(result?.error || 'Source file not found for this sketch.') | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| const result = await window.electronApi.ipcRenderer.invoke( | |
| FileEvents.OpenSketchSourceFile, | |
| sketchesDir, | |
| moduleId, | |
| ) | |
| if (!result?.success) { | |
| alert(result?.error || 'Source file not found for this sketch.') | |
| try { | |
| const result = await window.electronApi.ipcRenderer.invoke( | |
| FileEvents.OpenSketchSourceFile, | |
| sketchesDir, | |
| moduleId, | |
| ) | |
| if (!result?.success) { | |
| alert(result?.error || 'Source file not found for this sketch.') | |
| } | |
| } catch (error: unknown) { | |
| const message = | |
| (error && typeof error === 'object' && 'message' in error | |
| ? String((error as { message?: string }).message || '') | |
| : '') || 'An unexpected error occurred.' | |
| alert(`Failed to open source file: ${message}`) |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of all this (regenerating the path), do we want to just cache the path on load @funwithtriangles? might be handy if we ever support sketch root files with different names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's discuss today, don't have my head in this one right now