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