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 83b2b2d commit 447d890
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions src/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,31 @@ import open from '@yandeu/open-cjs'
export class OpenBrowser {
constructor(public _open: any) {}

private async _o(target: string, cfg: any): Promise<any> {
try {
const a = await this._open(target, cfg)
return a
} catch (error: any) {
return error.message
}
}

private async launchDefaultBrowser(target: string) {
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, { wait: true })

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

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

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

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

if (res)
const launchNextBrowser = () => {
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 is an error message
if (res && typeof res === 'string') {
launchNextBrowser()
}

// if res is a childProcess
if (res && typeof res !== 'string')
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)
}
}
launchNextBrowser()
}
})
}
Expand Down

0 comments on commit 447d890

Please sign in to comment.