UNPKG

1.67 kBJavaScriptView Raw
1// @flow
2
3import open from 'open';
4import {execSync} from 'child_process';
5import logger from '@parcel/logger';
6
7// Chrome app name is platform dependent. we should not hard code it.
8// https://github.com/react-native-community/cli/blob/e2be8a905285d9b37512fc78c9755b9635ecf805/packages/cli/src/commands/server/launchDebugger.ts#L28
9function getChromeAppName(): string {
10 switch (process.platform) {
11 case 'darwin':
12 return 'google chrome';
13 case 'win32':
14 return 'chrome';
15 case 'linux':
16 if (commandExistsUnixSync('google-chrome')) {
17 return 'google-chrome';
18 }
19 if (commandExistsUnixSync('chromium-browser')) {
20 return 'chromium-browser';
21 }
22 return 'chromium';
23
24 default:
25 return 'google-chrome';
26 }
27}
28
29function commandExistsUnixSync(commandName: string) {
30 try {
31 const stdout = execSync(
32 `command -v ${commandName} 2>/dev/null` +
33 ` && { echo >&1 '${commandName} found'; exit 0; }`,
34 );
35 return !!stdout;
36 } catch (error) {
37 return false;
38 }
39}
40
41function getAppName(appName: string): string {
42 if (['google', 'chrome'].includes(appName)) {
43 return getChromeAppName();
44 } else if (['brave', 'Brave'].includes(appName)) {
45 return 'Brave Browser';
46 } else return appName;
47}
48
49export default async function openInBrowser(url: string, browser: string) {
50 try {
51 const options =
52 typeof browser === 'string' ? {app: [getAppName(browser)]} : undefined;
53
54 await open(url, options);
55 } catch (err) {
56 logger.error(
57 `Unexpected error while opening in browser: ${browser}`,
58 '@parcel/utils',
59 );
60 logger.error(err, '@parcel/utils');
61 }
62}