storybook/lib/core-server/src/utils/open-in-browser.ts
Norbert de Langen 5d6972902d
try this
2022-06-30 00:48:42 +02:00

28 lines
936 B
TypeScript

import { logger } from '@storybook/node-logger';
// @ts-ignore
import betterOpn from 'better-opn'; // betterOpn alias used because also loading open
import open from 'open';
// @ts-ignore
import getDefaultBrowser from 'x-default-browser';
import { dedent } from 'ts-dedent';
export function openInBrowser(address: string) {
getDefaultBrowser(async (err: any, res: any) => {
try {
if (res && (res.isChrome || res.isChromium)) {
// We use betterOpn for Chrome because it is better at handling which chrome tab
// or window the preview loads in.
betterOpn(address);
} else {
await open(address);
}
} catch (error) {
logger.error(dedent`
Could not open ${address} inside a browser. If you're running this command inside a
docker container or on a CI, you need to pass the '--ci' flag to prevent opening a
browser by default.
`);
}
});
}