1import openBrowserAsync from 'better-opn';
2
3import { CommandError } from '../utils/errors';
4import { isInteractive } from '../utils/interactive';
5import { learnMore } from '../utils/link';
6import { ora } from '../utils/ora';
7
8export async function registerAsync() {
9  if (!isInteractive()) {
10    throw new CommandError(
11      'NON_INTERACTIVE',
12      `Cannot register an account in CI. Use the EXPO_TOKEN environment variable to authenticate in CI (${learnMore(
13        'https://docs.expo.dev/accounts/programmatic-access/'
14      )})`
15    );
16  }
17
18  const registrationUrl = `https://expo.dev/signup`;
19  const failedMessage = `Unable to open a web browser. Register an account at: ${registrationUrl}`;
20  const spinner = ora(`Opening ${registrationUrl}`).start();
21  try {
22    const opened = await openBrowserAsync(registrationUrl);
23
24    if (opened) {
25      spinner.succeed(`Opened ${registrationUrl}`);
26    } else {
27      spinner.fail(failedMessage);
28    }
29    return;
30  } catch (error) {
31    spinner.fail(failedMessage);
32    throw error;
33  }
34}
35