Repository navigation
feat: file-type associations with auto-detected local apps #5
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
Changes from all commits
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,91 @@ | ||
| // Find launchable apps in the development store: projects whose package.json | ||
| // declares electron-builder fileAssociations. Prefer the unpacked exe, then a | ||
| // portable artifact, then the project's own Electron binary in dev mode. | ||
|
|
||
| import { existsSync } from 'node:fs'; | ||
| import { readdir, readFile } from 'node:fs/promises'; | ||
| import { homedir } from 'node:os'; | ||
| import { isAbsolute, join } from 'node:path'; | ||
| import { | ||
| packageAppName, | ||
| packageAssociations, | ||
| pickPortableExe, | ||
| type AppLaunch, | ||
| type DetectedApp | ||
| } from '../shared/file-apps.js'; | ||
|
|
||
| export function defaultAppsRoot(): string { | ||
| return join(homedir(), 'Documents', 'development'); | ||
| } | ||
|
|
||
| function devElectronBinary(dir: string): string | null { | ||
| const base = join(dir, 'node_modules', 'electron', 'dist'); | ||
| const candidate = | ||
| process.platform === 'win32' | ||
| ? join(base, 'electron.exe') | ||
| : process.platform === 'darwin' | ||
| ? join(base, 'Electron.app', 'Contents', 'MacOS', 'Electron') | ||
| : join(base, 'electron'); | ||
| return existsSync(candidate) ? candidate : null; | ||
| } | ||
|
|
||
| async function launchForProject(dir: string, name: string, pkg: Record<string, unknown>): Promise<{ launch: AppLaunch; source: DetectedApp['source'] } | null> { | ||
| const build = (pkg.build ?? {}) as Record<string, unknown>; | ||
| const directories = (build.directories ?? {}) as Record<string, unknown>; | ||
| const outputs = [...new Set([directories.output, 'dist', 'release'].filter((value): value is string => typeof value === 'string' && Boolean(value)))]; | ||
|
|
||
| for (const out of outputs) { | ||
| const unpacked = join(dir, out, 'win-unpacked', `${name}.exe`); | ||
| if (existsSync(unpacked)) return { launch: { command: unpacked, args: ['{file}'] }, source: 'unpacked' }; | ||
| } | ||
| for (const out of outputs) { | ||
| try { | ||
| const exe = pickPortableExe(await readdir(join(dir, out))); | ||
| if (exe) return { launch: { command: join(dir, out, exe), args: ['{file}'] }, source: 'portable' }; | ||
| } catch { | ||
| /* no output folder */ | ||
| } | ||
| } | ||
| const electron = devElectronBinary(dir); | ||
| if (electron) return { launch: { command: electron, args: ['.', '{file}'], cwd: dir }, source: 'dev' }; | ||
| return null; | ||
| } | ||
|
|
||
| /** Scan one folder depth for app projects; unreadable entries are skipped. */ | ||
| export async function detectLocalApps(rootRaw: unknown): Promise<DetectedApp[]> { | ||
| const root = | ||
| typeof rootRaw === 'string' && rootRaw.trim() && isAbsolute(rootRaw.trim()) | ||
| ? rootRaw.trim() | ||
| : defaultAppsRoot(); | ||
|
Comment on lines
+55
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛑 Logic Error: |
||
| let entries; | ||
| try { | ||
| entries = await readdir(root, { withFileTypes: true }); | ||
| } catch { | ||
| return []; | ||
| } | ||
| const apps: DetectedApp[] = []; | ||
| for (const entry of entries) { | ||
| if (!entry.isDirectory() || entry.name.startsWith('.') || entry.name === 'node_modules') continue; | ||
| const projectDir = join(root, entry.name); | ||
| try { | ||
| const pkg = JSON.parse(await readFile(join(projectDir, 'package.json'), 'utf8')) as Record<string, unknown>; | ||
| const extensions = packageAssociations(pkg); | ||
| if (!extensions.length) continue; | ||
| const name = packageAppName(pkg) || entry.name; | ||
| const found = await launchForProject(projectDir, name, pkg); | ||
| if (!found) continue; | ||
| apps.push({ | ||
| id: `detected:${entry.name}`, | ||
| name, | ||
| version: typeof pkg.version === 'string' ? pkg.version : '', | ||
| extensions, | ||
| launch: found.launch, | ||
| source: found.source, | ||
| projectDir | ||
| }); | ||
| } catch { | ||
| /* not an app project */ | ||
| } | ||
| } | ||
| return apps.sort((a, b) => a.name.localeCompare(b.name)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,9 +11,12 @@ import { | |
| type IpcMainInvokeEvent | ||
| } from 'electron'; | ||
| import { existsSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { access, constants } from 'node:fs/promises'; | ||
| import { dirname, isAbsolute, join } from 'node:path'; | ||
| import { fileURLToPath, pathToFileURL } from 'node:url'; | ||
| import { detectLocalApps } from './app-detect.js'; | ||
| import { buildContextMenu, parseContextMenuTarget } from '../shared/context-menu.js'; | ||
| import { appCommandProblem, resolveAppArgs } from '../shared/file-apps.js'; | ||
| import { parseServerTranscriptionInput, transcribeViaServer } from '../shared/speech-server.js'; | ||
| import { isAppUrl, type AppLocation } from './app-origin.js'; | ||
| import { installApplicationMenu } from './app-menu.js'; | ||
|
|
@@ -231,6 +234,41 @@ handle('shell:openTerminal', (_e, path: unknown, options?: unknown) => { | |
| }); | ||
| }); | ||
|
|
||
| handle('apps:detect', (_e, root: unknown) => detectLocalApps(root)); | ||
|
|
||
| handle('apps:open', async (_e, raw: unknown) => { | ||
| const request = (raw ?? {}) as { filePath?: unknown; command?: unknown; args?: unknown; cwd?: unknown }; | ||
| const filePath = resolveLocalPath(request.filePath); | ||
| if (!existsSync(filePath)) throw new Error('That file no longer exists.'); | ||
| const command = typeof request.command === 'string' ? request.command.trim() : ''; | ||
| if (!command || !isAbsolute(command) || !existsSync(command)) throw new Error('App executable not found.'); | ||
| const commandProblem = appCommandProblem(command, process.platform); | ||
| if (commandProblem) throw new Error(commandProblem); | ||
| if (process.platform !== 'win32') { | ||
| try { | ||
| await access(command, constants.X_OK); | ||
| } catch { | ||
| throw new Error('The app must be an executable file.'); | ||
| } | ||
| } | ||
| const args = Array.isArray(request.args) ? request.args : []; | ||
| if ( | ||
| args.length > 32 || | ||
| args.some((arg) => typeof arg !== 'string' || arg.length > 2048 || /[\0\r\n]/.test(arg)) | ||
| ) { | ||
| throw new Error('Invalid app arguments.'); | ||
| } | ||
| const cwd = | ||
| typeof request.cwd === 'string' && request.cwd && isAbsolute(request.cwd) && existsSync(request.cwd) | ||
| ? request.cwd | ||
| : undefined; | ||
| const resolved = resolveAppArgs(args as string[], filePath); | ||
| await new Promise<void>((resolvePromise, reject) => { | ||
| spawnDetached(command, resolved, { cwd, onError: reject }); | ||
|
Comment on lines
+265
to
+267
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛑 Security Vulnerability: The Footnotes
|
||
| setTimeout(resolvePromise, 150); | ||
| }); | ||
| }); | ||
|
|
||
| handle('contextMenu:show', (event, raw: unknown) => { | ||
| const target = parseContextMenuTarget(raw); | ||
| const window = BrowserWindow.fromWebContents(event.sender) ?? undefined; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { DEFAULT_FILE_APP_PREFS, parseFileAppPrefs, type FileAppPrefs } from '../shared/file-apps.js'; | ||
|
|
||
| const STORAGE_KEY = 'zerogfileman.fileApps'; | ||
|
|
||
| export function loadStoredFileAppPrefs(): FileAppPrefs { | ||
| try { | ||
| const raw = localStorage.getItem(STORAGE_KEY); | ||
| if (!raw) return { ...DEFAULT_FILE_APP_PREFS, defaults: {}, customApps: [] }; | ||
| return parseFileAppPrefs(JSON.parse(raw)); | ||
| } catch { | ||
| return { ...DEFAULT_FILE_APP_PREFS, defaults: {}, customApps: [] }; | ||
| } | ||
| } | ||
|
|
||
| export function storeFileAppPrefs(prefs: FileAppPrefs): void { | ||
| try { | ||
| localStorage.setItem(STORAGE_KEY, JSON.stringify(parseFileAppPrefs(prefs))); | ||
| } catch { | ||
| /* ignore */ | ||
| } | ||
| } |
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.
🛑 Logic Error: The
win-unpackedlookup constructs a${name}.exepath unconditionally. On macOS, the equivalent would be a.appbundle, and on Linux there is no.exeextension. This means theunpackedsource is never found on non-Windows platforms, silently falling through to theportableordevfallback even when a proper unpacked build exists. The PR description states multi-platform support, so this will silently produce wrong results on macOS/Linux.