Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fallback to default browser if Chrome is not found #612

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import open from 'open';
import {execSync} from 'child_process';
import {logger} from '@react-native-community/cli-tools';
import launchBrowser from './launchBrowser';
import chalk from 'chalk';

function commandExistsUnixSync(commandName) {
try {
Expand All @@ -24,6 +26,28 @@ function commandExistsUnixSync(commandName) {
}
}

function commandExistsWindowsSync(commandName) {
try {
var stdout = execSync('where ' + commandName, {stdio: []});
thecodrr marked this conversation as resolved.
Show resolved Hide resolved
return !!stdout;
} catch (error) {
return false;
}
}

function commandExists(commandName) {
switch (process.platform) {
case 'win32':
return commandExistsWindowsSync(commandName);
case 'linux':
case 'darwin':
return commandExistsUnixSync(commandName);
default:
// assume it doesn't exist, just to be safe.
return false;
}
}

function getChromeAppName(): string {
switch (process.platform) {
case 'darwin':
Expand Down Expand Up @@ -52,4 +76,18 @@ function launchChrome(url: string) {
});
}

export default launchChrome;
function launchDebugger(url: string) {
if (!commandExists(getChromeAppName())) {
logger.info(
`For a better debugging experience please install Google Chrome from: ${chalk.underline.dim(
'https://www.google.com/chrome/',
)}`,
);
//fallback to default browser
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space here before the inline command. I also think we can rid of that comment and rename the function to something like: launchDefaultBrowser

Copy link
Contributor Author

@thecodrr thecodrr Aug 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. @thymikee

launchBrowser(url);
return;
}
launchChrome();
}

export default launchDebugger;
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Options = {

type WebSocketProxy = {
server: WebSocketServer,
isChromeConnected: () => boolean,
isDebuggerConnected: () => boolean,
};

type Connect = $Call<connect>;
Expand Down Expand Up @@ -72,7 +72,7 @@ export default class MiddlewareManager {

attachDevToolsSocket(socket: WebSocketProxy) {
this.app.use(
getDevToolsMiddleware(this.options, () => socket.isChromeConnected()),
getDevToolsMiddleware(this.options, () => socket.isDebuggerConnected()),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
*
* @format
*/
import launchChrome from '../launchChrome';
import {logger} from '@react-native-community/cli-tools';
import {exec} from 'child_process';
import launchDebugger from './../launchDebugger';
thecodrr marked this conversation as resolved.
Show resolved Hide resolved

function launchChromeDevTools(port, args = '') {
function launchDefaultDebugger(port, args = '') {
const debuggerURL = `http://localhost:${port}/debugger-ui${args}`;
logger.info('Launching Dev Tools...');
launchChrome(debuggerURL);
launchDebugger(debuggerURL);
}

function escapePath(pathname) {
// " Can escape paths with spaces in OS X, Windows, and *nix
return `"${pathname}"`;
}

function launchDevTools({port, watchFolders}, isChromeConnected) {
function launchDevTools({port, watchFolders}, isDebuggerConnected) {
// Explicit config always wins
const customDebugger = process.env.REACT_DEBUGGER;
if (customDebugger) {
startCustomDebugger({watchFolders, customDebugger});
} else if (!isChromeConnected()) {
// Dev tools are not yet open; we need to open a session
launchChromeDevTools(port);
} else if (!isDebuggerConnected()) {
// Debugger is not yet open; we need to open a session
launchDefaultDebugger(port);
}
}

Expand All @@ -43,10 +43,10 @@ function startCustomDebugger({watchFolders, customDebugger}) {
});
}

export default function getDevToolsMiddleware(options, isChromeConnected) {
export default function getDevToolsMiddleware(options, isDebuggerConnected) {
return function devToolsMiddleware(req, res, next) {
if (req.url === '/launch-js-devtools') {
launchDevTools(options, isChromeConnected);
launchDevTools(options, isDebuggerConnected);
res.end('OK');
} else {
next();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/server/webSocketProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function attachToServer(server, path) {

return {
server: wss,
isChromeConnected() {
isDebuggerConnected() {
return !!debuggerSocket;
},
};
Expand Down