Skip to content

Commit

Permalink
Merge branch 'release/v0.24.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
holtwick committed Sep 6, 2024
2 parents d4400f8 + 0d5d8e9 commit 86c4841
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zeed",
"type": "module",
"version": "0.24.15",
"version": "0.24.16",
"description": "🌱 Simple foundation library",
"author": {
"name": "Dirk Holtwick",
Expand Down
2 changes: 2 additions & 0 deletions src/node/files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
`)
})
Expand Down
36 changes: 36 additions & 0 deletions src/node/open-browser.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
// })
})
16 changes: 16 additions & 0 deletions src/node/open-browser.ts
Original file line number Diff line number Diff line change
@@ -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}`)
}
}

0 comments on commit 86c4841

Please sign in to comment.