Skip to content

Commit

Permalink
Improve OpenBrowser
Browse files Browse the repository at this point in the history
  • Loading branch information
yandeu committed Dec 2, 2024
1 parent 362efca commit fb2e214
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 82 deletions.
64 changes: 32 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"homepage": "https://github.com/yandeu/five-server#readme",
"dependencies": {
"@yandeu/open-cjs": "^0.0.0",
"@yandeu/open-cjs": "^0.0.1",
"chokidar": "^3.5.1",
"cors": "^2.8.5",
"debug": "^4.3.1",
Expand Down
65 changes: 40 additions & 25 deletions src/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,49 @@
* @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE}
*/

import type { ChildProcess } from 'child_process'
import { colors } from './colors'
import { message } from './msg'
import open from '@yandeu/open-cjs'

export class OpenBrowser {
constructor(public _open: any) {}

private async launchDefaultBrowser(target: string) {
private async _o(target: string, cfg: any = {}): Promise<void | ChildProcess> {
return new Promise(resolve => {
try {
this._open(target, { wait: false, ...cfg }).then(a => {
a.once('error', () => {
return resolve()
})
a.once('exit', () => {
return resolve()
})
a.once('spawn', () => {
return resolve(a)
})
})
} catch (error) {
resolve()
}
})
}

private async launchDefaultBrowser(target: string): Promise<void | ChildProcess> {
await this.launchBrowser(target, 'default')
}

private async open(target: string, browser?: string) {
if (!browser || browser === 'default') return await this._open(target)
if (!browser || browser === 'default') return await this._o(target)

const hasArguments = browser.includes('--')

if (!hasArguments) return await this._open(target, { app: { name: browser } })
if (!hasArguments) return await this._o(target, { app: { name: browser } })

if (hasArguments) {
const b = browser.split('--').map(c => c.trim())

return await this._open(target, {
return await this._o(target, {
app: { name: b.shift() as string, arguments: b.map(arg => `--${arg}`) }
})
}
Expand All @@ -44,28 +65,22 @@ export class OpenBrowser {
res = await this.open(target, browser[index])
}

if (res)
res.once('exit', code => {
if (code && code > 0) {
if (typeof browser === 'string') {
message.log(colors(`Could not open browser "${browser}". Trying the default browser next.`, 'yellow'))
this.launchDefaultBrowser(target)
} else if (Array.isArray(browser)) {
if (typeof browser[index + 1] === 'undefined') {
message.log(
colors(`Could not open browser "${browser[index]}". Trying the default browser next.`, 'yellow')
)
this.launchDefaultBrowser(target)
} else {
message.log(
colors(`Could not open browser "${browser[index]}". Trying "${browser[index + 1]}" next.`, 'yellow')
)

this.launchBrowser(target, browser, index)
}
}
if (!res) {
if (typeof browser === 'string') {
message.log(colors(`Could not open browser "${browser}". Trying the default browser next.`, 'yellow'))
await this.launchDefaultBrowser(target)
} else if (Array.isArray(browser)) {
if (typeof browser[index + 1] === 'undefined') {
message.log(colors(`Could not open browser "${browser[index]}". Trying the default browser next.`, 'yellow'))
await this.launchDefaultBrowser(target)
} else {
message.log(
colors(`Could not open browser "${browser[index]}". Trying "${browser[index + 1]}" next.`, 'yellow')
)
await this.launchBrowser(target, browser, index)
}
})
}
}
}

/** Launch a new browser window. */
Expand Down
42 changes: 18 additions & 24 deletions test/unit/openBrowser.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const { rejects } = require('assert')
const { OpenBrowser } = require('../../lib/openBrowser')
const pause = require('../helpers/pause')

const mock_error_event = {
once: (event, cb) => {
// console.log('event', event)
cb(1)
if (event !== 'spawn') {
cb()
}
}
}

const mock_success_event = {
once: (event, cb) => {
if (event === 'spawn') {
cb('ok')
}
}
}

Expand All @@ -16,11 +24,11 @@ const mock_open = (...args) => {
// console.log('args', args)

// return error
if (args[1] && args[1].app.name === 'unknown') return resolve(mock_error_event)
if (args[1] && args[1]?.app?.name === 'unknown') return resolve(mock_error_event)

// resolve
logs.push(...args)
return resolve()
return resolve(mock_success_event)
})
}

Expand Down Expand Up @@ -65,48 +73,39 @@ describe('openBrowser.ts', () => {

test('with url and any path', async () => {
await openBrowser('http://localhost:3000', 'hello')
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000/hello')
})

test('testing slashes', async () => {
await openBrowser('http://localhost:3000', 'hello/')
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000/hello/')

await openBrowser('http://localhost:3000', '/hello/')
expect(logs.length).toBe(2)
expect(logs[1]).toBe('http://localhost:3000/hello/')
expect(logs[2]).toBe('http://localhost:3000/hello/')

await openBrowser('http://localhost:3000', '/hello')
expect(logs.length).toBe(3)
expect(logs[2]).toBe('http://localhost:3000/hello')
expect(logs[4]).toBe('http://localhost:3000/hello')

await openBrowser('http://localhost:3000/', '/hello')
expect(logs.length).toBe(4)
expect(logs[3]).toBe('http://localhost:3000/hello')
expect(logs[6]).toBe('http://localhost:3000/hello')

await openBrowser('http://localhost:3000/', 'hello')
expect(logs.length).toBe(5)
expect(logs[4]).toBe('http://localhost:3000/hello')
expect(logs[8]).toBe('http://localhost:3000/hello')
})

test('open multiple paths', async () => {
await openBrowser('http://localhost:3000', ['hello', 'bye'])
expect(logs.length).toBe(2)
expect(logs[0]).toBe('http://localhost:3000/hello')
expect(logs[1]).toBe('http://localhost:3000/bye')
expect(logs[2]).toBe('http://localhost:3000/bye')
})

test('path is an url arguments', async () => {
await openBrowser('http://localhost:3000', 'http://localhost:3000/hello')
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000/hello')
})

test('path is an url arguments (multiple)', async () => {
await openBrowser('http://localhost:3000', ['http://localhost:3000/hello'])
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000/hello')
})
})
Expand All @@ -133,32 +132,27 @@ describe('openBrowser.ts', () => {

test('test unknown browser', async () => {
await openBrowser('http://localhost:3000', '', 'unknown')
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000')
})

test('test array unknown browsers', async () => {
await openBrowser('http://localhost:3000', '', ['unknown', 'unknown'])
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000')
})

test('test empty browser array', async () => {
await openBrowser('http://localhost:3000', '', [])
expect(logs.length).toBe(1)
expect(logs[0]).toBe('http://localhost:3000')
})

test('test multiple browsers', async () => {
await openBrowser('http://localhost:3000', '', ['unknown', 'chrome'])
expect(logs.length).toBe(2)
expect(logs[0]).toBe('http://localhost:3000')
expect(logs[1].app.name).toBe('chrome')
})

test('test with arguments', async () => {
await openBrowser('http://localhost:3000', '', 'chrome --argument')
expect(logs.length).toBe(2)
expect(logs[0]).toBe('http://localhost:3000')
expect(logs[1].app.name).toBe('chrome')
})
Expand Down

0 comments on commit fb2e214

Please sign in to comment.