diff --git a/package.json b/package.json index f08a5ec..2b6c11e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "zeed", "type": "module", - "version": "0.24.15", + "version": "0.24.16", "description": "🌱 Simple foundation library", "author": { "name": "Dirk Holtwick", diff --git a/src/node/files.spec.ts b/src/node/files.spec.ts index 0aa86a3..cc8d6bf 100644 --- a/src/node/files.spec.ts +++ b/src/node/files.spec.ts @@ -32,6 +32,8 @@ describe('files.spec', () => { "log/log-node.ts", "log/log-util.spec.ts", "log/log-util.ts", + "open-browser.spec.ts", + "open-browser.ts", ] `) }) diff --git a/src/node/open-browser.spec.ts b/src/node/open-browser.spec.ts new file mode 100644 index 0000000..da86143 --- /dev/null +++ b/src/node/open-browser.spec.ts @@ -0,0 +1,36 @@ +import { exec } from 'node:child_process' +import { platform } from 'node:os' +import { describe, expect, it, vi } from 'vitest' +import { openBrowser } from './open-browser' + +vi.mock('node:child_process', () => ({ + exec: vi.fn(), +})) + +vi.mock('node:os', () => ({ + platform: vi.fn(), +})) + +describe('openBrowser', () => { + it('should open URL on macOS', () => { + vi.mocked(platform).mockReturnValue('darwin') + openBrowser('http://example.com') + expect(exec).toHaveBeenCalledWith('open http://example.com') + }) + + it('should open URL on Windows', () => { + vi.mocked(platform).mockReturnValue('win32') + openBrowser('http://example.com') + expect(exec).toHaveBeenCalledWith('start http://example.com') + }) + + it('should open URL on Linux', () => { + vi.mocked(platform).mockReturnValue('linux') + openBrowser('http://example.com') + expect(exec).toHaveBeenCalledWith('xdg-open http://example.com') + }) + + // it('should open URL on unknown platform for real', () => { + // openBrowser('http://example.com') + // }) +}) diff --git a/src/node/open-browser.ts b/src/node/open-browser.ts new file mode 100644 index 0000000..67fb7ef --- /dev/null +++ b/src/node/open-browser.ts @@ -0,0 +1,16 @@ +// Open URL in default browser on macOS, Linux, and Windows +import { exec } from 'node:child_process' +import { platform } from 'node:os' + +export function openBrowser(url: string) { + switch (platform()) { + case 'darwin': + exec(`open ${url}`) + break + case 'win32': + exec(`start ${url}`) + break + default: + exec(`xdg-open ${url}`) + } +}