forked from netlify/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-browser.ts
48 lines (40 loc) · 1.35 KB
/
open-browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import process from 'process'
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'bett... Remove this comment to see the full error message
import open from 'better-opn'
import isDockerContainer from 'is-docker'
import { chalk, log } from './command-helpers.js'
type BrowserUnableMessage = {
message: string
url: string
}
const unableToOpenBrowserMessage = function ({ message, url }: BrowserUnableMessage) {
log('---------------------------')
log(chalk.redBright(`Error: Unable to open browser automatically: ${message}`))
log(chalk.cyan('Please open your browser and open the URL below:'))
log(chalk.bold(url))
log('---------------------------')
}
type OpenBrowsrProps = {
silentBrowserNoneError: boolean
url: string
}
const openBrowser = async function ({ silentBrowserNoneError, url }: OpenBrowsrProps) {
if (isDockerContainer()) {
unableToOpenBrowserMessage({ url, message: 'Running inside a docker container' })
return
}
if (process.env.BROWSER === 'none') {
if (!silentBrowserNoneError) {
unableToOpenBrowserMessage({ url, message: "BROWSER environment variable is set to 'none'" })
}
return
}
try {
await open(url)
} catch (error) {
if (error instanceof Error) {
unableToOpenBrowserMessage({ url, message: error.message })
}
}
}
export default openBrowser