Skip to content

Commit

Permalink
handle browser open failure
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Oct 30, 2020
1 parent 2e4bbe1 commit c890042
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
6 changes: 4 additions & 2 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1327,9 +1327,11 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
startTimeMs: Math.round(performance.now() - serverStart),
});

// Open the user's browser
// Open the user's browser (ignore if failed)
if (open !== 'none') {
await openInBrowser(protocol, hostname, port, open);
await openInBrowser(protocol, hostname, port, open).catch((err) => {
logger.debug(`Browser open error: ${err}`);
});
}

// Start watching the file system.
Expand Down
88 changes: 44 additions & 44 deletions snowpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,65 +178,65 @@ const appNames = {
},
};

async function openInExistingChromeBrowser(url: string) {
// see if Chrome process is open; fail if not
await execa.command('ps cax | grep "Google Chrome"', {
shell: true,
});
// use open Chrome tab if exists; create new Chrome tab if not
const openChrome = execa(
'osascript ../assets/openChrome.appleScript "' + encodeURI(url) + '"',
{
cwd: __dirname,
stdio: 'ignore',
shell: true,
},
);
// if Chrome doesn’t respond within 3s, fall back to opening new tab in default browser
let isChromeStalled = setTimeout(() => {
openChrome.cancel();
}, 3000);
try {
await openChrome;
} catch (err) {
if (err.isCanceled) {
console.warn(`Chrome not responding to Snowpack after 3s. Opening in new tab.`);
} else {
console.error(err.toString() || err);
}
throw err;
} finally {
clearTimeout(isChromeStalled);
}

}
export async function openInBrowser(
protocol: string,
hostname: string,
port: number,
browser: string,
) {
): Promise<void> {
const url = `${protocol}//${hostname}:${port}`;
browser = /chrome/i.test(browser)
? appNames[process.platform]['chrome']
: /brave/i.test(browser)
? appNames[process.platform]['brave']
: browser;
const isMac = process.platform === 'darwin';
const isOpeningInChrome = /chrome|default/i.test(browser);
if (isMac && isOpeningInChrome) {
const isBrowserChrome = /chrome|default/i.test(browser);
if (!isMac || !isBrowserChrome) {
await (browser === 'default' ? open(url) : open(url, {app: browser}));
return;
}

try {
// If we're on macOS, and we haven't requested a specific browser,
// we can try opening Chrome with AppleScript. This lets us reuse an
// existing tab when possible instead of creating a new one.
try {
// see if Chrome process is open; fail if not
await execa.command('ps cax | grep "Google Chrome"', {
shell: true,
});
// use open Chrome tab if exists; create new Chrome tab if not
const openChrome = execa(
'osascript ../assets/openChrome.appleScript "' + encodeURI(url) + '"',
{
cwd: __dirname,
stdio: 'ignore',
shell: true,
},
);
// if Chrome doesn’t respond within 3s, fall back to opening new tab in default browser
let isChromeStalled = setTimeout(() => {
openChrome.cancel();
}, 3000);

try {
await openChrome;
} catch (err) {
if (err.isCanceled) {
console.warn(
`Chrome not responding to Snowpack after 3s. Opening dev server in new tab.`,
);
} else {
console.error(err.toString() || err);
}
open(url);
} finally {
clearTimeout(isChromeStalled);
}
return true;
} catch (err) {
// if no open Chrome process, open default browser
// no error message needed here
open(url);
}
} else {
browser === 'default' ? open(url) : open(url, {app: browser});
await openInExistingChromeBrowser(url);
} catch (err) {
// if no open Chrome process, just go ahead and open default browser.
await open(url);
}
}

Expand Down

1 comment on commit c890042

@vercel
Copy link

@vercel vercel bot commented on c890042 Oct 30, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.