xref: /expo/packages/@expo/cli/src/utils/open.ts (revision 065a44f7)
1*065a44f7SCedric van Puttenimport betterOpenBrowserAsync from 'better-opn';
2*065a44f7SCedric van Putten
3*065a44f7SCedric van Putten/**
4*065a44f7SCedric van Putten * Due to a bug in `open`, which is used as fallback on Windows, we need to ensure `process.env.SYSTEMROOT` is set.
5*065a44f7SCedric van Putten * This environment variable is set by Windows on `SystemRoot`, causing `open` to execute a command with an "unknown" drive letter.
6*065a44f7SCedric van Putten *
7*065a44f7SCedric van Putten * @see https://github.com/sindresorhus/open/issues/205
8*065a44f7SCedric van Putten */
9*065a44f7SCedric van Puttenexport async function openBrowserAsync(
10*065a44f7SCedric van Putten  target: string,
11*065a44f7SCedric van Putten  options?: any
12*065a44f7SCedric van Putten): Promise<import('child_process').ChildProcess | false> {
13*065a44f7SCedric van Putten  if (process.platform !== 'win32') {
14*065a44f7SCedric van Putten    return await betterOpenBrowserAsync(target, options);
15*065a44f7SCedric van Putten  }
16*065a44f7SCedric van Putten
17*065a44f7SCedric van Putten  const oldSystemRoot = process.env.SYSTEMROOT;
18*065a44f7SCedric van Putten  try {
19*065a44f7SCedric van Putten    process.env.SYSTEMROOT = process.env.SYSTEMROOT ?? process.env.SystemRoot;
20*065a44f7SCedric van Putten    return await betterOpenBrowserAsync(target, options);
21*065a44f7SCedric van Putten  } finally {
22*065a44f7SCedric van Putten    process.env.SYSTEMROOT = oldSystemRoot;
23*065a44f7SCedric van Putten  }
24*065a44f7SCedric van Putten}
25