diff --git a/PROGRESS.md b/PROGRESS.md index 4d27082..ed17ea0 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -33,6 +33,7 @@ Open check: builtin Whisper model download under the narrowed CSP `connect-src` - In-app name dialog replaces `window.prompt` (Electron never implements it): New Folder, New File type/name, Rename (F2) and favorite naming now work in both panes; inline validation, Esc/Cancel aborts, new folders land selected - Explorer-style drag-drop inside and between panes: drag = move, Ctrl = copy, Shift = force move, cross-drive defaults to copy; targets are folder rows/tiles, pane background, ".." and breadcrumbs; whole-selection drag; both panes refresh and the transcript logs each op; same-folder moves and drops into a folder's own subtree are no-ops - Name dialog + drag-drop rules covered by 24 new tests (188 total); `npm run ci` passes; verified in a real hidden Electron window against a temp filesystem: 14/14 UI checks (dialog in the right pane, invalid-name error, Esc cancel, F2 rename, cross-pane move, Ctrl copy, folder-row drop, same-folder no-op) +- Native-app detection: projects without Electron associations can declare a `zerogapp.json` manifest (`name`, `version`, `extensions`, `launch.command`/`args`/`cwd`); relative commands resolve against the project dir, missing executables are skipped, `package.json` associations still win; the Rust sister apps ZeroG Edit / ZeroG Play use this (8 new tests; 225 total) ## Next diff --git a/src/main/app-detect.ts b/src/main/app-detect.ts index 6fbe518..8617eba 100644 --- a/src/main/app-detect.ts +++ b/src/main/app-detect.ts @@ -10,6 +10,7 @@ import { packageAppName, packageAssociations, pickPortableExe, + zerogAppManifest, type AppLaunch, type DetectedApp } from '../shared/file-apps.js'; @@ -67,24 +68,48 @@ export async function detectLocalApps(rootRaw: unknown): Promise for (const entry of entries) { if (!entry.isDirectory() || entry.name.startsWith('.') || entry.name === 'node_modules') continue; const projectDir = join(root, entry.name); + let detected = false; try { const pkg = JSON.parse(await readFile(join(projectDir, 'package.json'), 'utf8')) as Record; const extensions = packageAssociations(pkg); - if (!extensions.length) continue; - const name = packageAppName(pkg) || entry.name; - const found = await launchForProject(projectDir, name, pkg); - if (!found) continue; + if (extensions.length) { + const name = packageAppName(pkg) || entry.name; + const found = await launchForProject(projectDir, name, pkg); + if (found) { + apps.push({ + id: `detected:${entry.name}`, + name, + version: typeof pkg.version === 'string' ? pkg.version : '', + extensions, + launch: found.launch, + source: found.source, + projectDir + }); + detected = true; + } + } + } catch { + /* no package.json — try a native manifest below */ + } + if (detected) continue; + try { + const manifest = zerogAppManifest(JSON.parse(await readFile(join(projectDir, 'zerogapp.json'), 'utf8'))); + if (!manifest) continue; + const command = isAbsolute(manifest.launch.command) + ? manifest.launch.command + : join(projectDir, manifest.launch.command); + if (!existsSync(command)) continue; apps.push({ id: `detected:${entry.name}`, - name, - version: typeof pkg.version === 'string' ? pkg.version : '', - extensions, - launch: found.launch, - source: found.source, + name: manifest.name, + version: manifest.version, + extensions: manifest.extensions, + launch: { ...manifest.launch, command }, + source: 'manifest', projectDir }); } catch { - /* not an app project */ + /* not a manifest project */ } } return apps.sort((a, b) => a.name.localeCompare(b.name)); diff --git a/src/shared/file-apps.ts b/src/shared/file-apps.ts index e4e40ea..e823b05 100644 --- a/src/shared/file-apps.ts +++ b/src/shared/file-apps.ts @@ -47,7 +47,7 @@ export type DetectedApp = { version: string; extensions: string[]; launch: AppLaunch; - source: 'unpacked' | 'portable' | 'dev'; + source: 'unpacked' | 'portable' | 'dev' | 'manifest'; projectDir: string; }; @@ -249,6 +249,43 @@ export function packageAppName(pkg: unknown): string { return typeof source.name === 'string' ? source.name.trim() : ''; } +/** A native (non-Electron) app's zerogapp.json manifest, validated. */ +export type ZerogAppManifest = { + name: string; + version: string; + extensions: string[]; + launch: AppLaunch; +}; + +export function zerogAppManifest(raw: unknown): ZerogAppManifest | null { + if (!raw || typeof raw !== 'object') return null; + const source = raw as Record; + const name = typeof source.name === 'string' ? source.name.trim() : ''; + if (!name || name.length > 80) return null; + const version = typeof source.version === 'string' ? source.version.trim() : ''; + const extensions = parseExtensionList( + Array.isArray(source.extensions) ? source.extensions.join(',') : source.extensions + ); + if (!extensions.length) return null; + const launchRaw = source.launch; + if (!launchRaw || typeof launchRaw !== 'object') return null; + const launchSource = launchRaw as Record; + const command = typeof launchSource.command === 'string' ? launchSource.command.trim() : ''; + if (!command || command.length > 2048) return null; + const args = Array.isArray(launchSource.args) + ? launchSource.args + .filter((arg): arg is string => typeof arg === 'string' && arg.length <= 2048 && !/[\0\r\n]/.test(arg)) + .slice(0, 32) + : []; + const cwd = typeof launchSource.cwd === 'string' && launchSource.cwd.trim() ? launchSource.cwd.trim() : undefined; + return { + name, + version, + extensions, + launch: { command, args: args.length ? args : ['{file}'], ...(cwd ? { cwd } : {}) } + }; +} + /** Best launchable exe among build artifacts: portable over installer-free, newest version first. */ export function pickPortableExe(names: readonly string[]): string | null { const candidates = names.filter( diff --git a/tests/app-detect.test.ts b/tests/app-detect.test.ts index fcb46f6..2333063 100644 --- a/tests/app-detect.test.ts +++ b/tests/app-detect.test.ts @@ -68,3 +68,92 @@ describe('detectLocalApps', () => { expect(await detectLocalApps(join(root, 'does-not-exist'))).toEqual([]); }); }); + +describe('detectLocalApps — zerogapp.json manifests', () => { + let manifestRoot: string; + + async function manifestProject(name: string, manifest: unknown, files: string[] = []): Promise { + const dir = join(manifestRoot, name); + await mkdir(dir, { recursive: true }); + await writeFile(join(dir, 'zerogapp.json'), JSON.stringify(manifest)); + for (const file of files) { + const target = join(dir, file); + await mkdir(join(target, '..'), { recursive: true }); + await writeFile(target, 'x'); + } + } + + beforeAll(async () => { + manifestRoot = await mkdtemp(join(tmpdir(), 'zerog-manifest-')); + await manifestProject( + 'zerogedit', + { + name: 'ZeroG Edit', + version: '0.1.0', + extensions: ['txt', 'md'], + launch: { command: 'target/release/zerogedit.exe', args: ['{file}'] } + }, + ['target/release/zerogedit.exe'] + ); + await manifestProject('no-exe', { + name: 'No Exe', + version: '1.0.0', + extensions: ['xyz'], + launch: { command: 'target/release/noexe.exe' } + }); + await manifestProject('bad-manifest', { name: 'Bad', extensions: [], launch: { command: 'x.exe' } }, ['x.exe']); + await manifestProject('absolute', { + name: 'Absolute App', + version: '2.0.0', + extensions: ['.wav'], + launch: { command: join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe') } + }); + }); + + afterAll(async () => { + await rm(manifestRoot, { recursive: true, force: true }); + }); + + it('detects native apps via zerogapp.json and resolves relative commands', async () => { + const apps = await detectLocalApps(manifestRoot); + expect(apps.map((app) => app.id)).toEqual(['detected:absolute', 'detected:zerogedit']); + const edit = apps[1]; + expect(edit.name).toBe('ZeroG Edit'); + expect(edit.version).toBe('0.1.0'); + expect(edit.extensions).toEqual(['.txt', '.md']); + expect(edit.source).toBe('manifest'); + expect(edit.launch.command).toBe(join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe')); + expect(edit.launch.args).toEqual(['{file}']); + }); + + it('defaults args to {file} and keeps absolute commands', async () => { + const apps = await detectLocalApps(manifestRoot); + const absolute = apps[0]; + expect(absolute.launch.args).toEqual(['{file}']); + expect(absolute.launch.command).toBe(join(manifestRoot, 'zerogedit', 'target', 'release', 'zerogedit.exe')); + expect(absolute.extensions).toEqual(['.wav']); + }); + + it('package.json associations win when both exist', async () => { + const bothRoot = await mkdtemp(join(tmpdir(), 'zerog-both-')); + try { + const dir = join(bothRoot, 'both'); + await mkdir(join(dir, 'dist', 'win-unpacked'), { recursive: true }); + await writeFile(join(dir, 'dist', 'win-unpacked', 'Both Electron.exe'), 'x'); + await writeFile( + join(dir, 'package.json'), + JSON.stringify({ name: 'both', version: '9.9.9', build: { productName: 'Both Electron', fileAssociations: [{ ext: 'md' }] } }) + ); + await writeFile( + join(dir, 'zerogapp.json'), + JSON.stringify({ name: 'Both Native', version: '0.1.0', extensions: ['txt'], launch: { command: 'dist/win-unpacked/Both Electron.exe' } }) + ); + const apps = await detectLocalApps(bothRoot); + expect(apps).toHaveLength(1); + expect(apps[0].name).toBe('Both Electron'); + expect(apps[0].source).toBe('unpacked'); + } finally { + await rm(bothRoot, { recursive: true, force: true }); + } + }); +}); diff --git a/tests/file-apps.test.ts b/tests/file-apps.test.ts index 73e1fd9..6fdec54 100644 --- a/tests/file-apps.test.ts +++ b/tests/file-apps.test.ts @@ -17,6 +17,7 @@ import { pickPortableExe, resolveAppArgs, resolveDefaultApp, + zerogAppManifest, type CustomApp, type DetectedApp, type FileAppPrefs @@ -251,3 +252,47 @@ describe('appCommandProblem', () => { expect(appCommandProblem('', 'linux')).toBe('App executable not found.'); }); }); + +describe('zerogAppManifest', () => { + const valid = { + name: 'ZeroG Play', + version: '0.1.0', + extensions: ['mp3', '.FLAC'], + launch: { command: 'target/release/zerogplay.exe', args: ['{file}'] } + }; + + it('parses a valid manifest and normalizes extensions', () => { + const manifest = zerogAppManifest(valid); + expect(manifest).not.toBeNull(); + expect(manifest!.name).toBe('ZeroG Play'); + expect(manifest!.extensions).toEqual(['.mp3', '.flac']); + expect(manifest!.launch).toEqual({ command: 'target/release/zerogplay.exe', args: ['{file}'] }); + }); + + it('defaults missing args to {file}', () => { + const manifest = zerogAppManifest({ ...valid, launch: { command: 'app.exe' } }); + expect(manifest!.launch.args).toEqual(['{file}']); + }); + + it('keeps an optional cwd', () => { + const manifest = zerogAppManifest({ ...valid, launch: { command: 'app.exe', cwd: 'C:/apps' } }); + expect(manifest!.launch.cwd).toBe('C:/apps'); + }); + + it('rejects missing name, extensions, launch, or command', () => { + expect(zerogAppManifest(null)).toBeNull(); + expect(zerogAppManifest({ ...valid, name: '' })).toBeNull(); + expect(zerogAppManifest({ ...valid, extensions: [] })).toBeNull(); + expect(zerogAppManifest({ ...valid, launch: undefined })).toBeNull(); + expect(zerogAppManifest({ ...valid, launch: { command: '' } })).toBeNull(); + expect(zerogAppManifest({ ...valid, name: 'x'.repeat(81) })).toBeNull(); + }); + + it('filters unsafe or oversized args', () => { + const manifest = zerogAppManifest({ + ...valid, + launch: { command: 'app.exe', args: ['{file}', 42, 'bad\0arg', 'ok'] } + }); + expect(manifest!.launch.args).toEqual(['{file}', 'ok']); + }); +});