1import { env } from '../utils/env';
2import { CommandError } from '../utils/errors';
3import { isInteractive } from '../utils/interactive';
4import { learnMore } from '../utils/link';
5import { openBrowserAsync } from '../utils/open';
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  } else if (env.EXPO_OFFLINE) {
17    throw new CommandError('OFFLINE', `Cannot register an account in offline-mode`);
18  }
19
20  const registrationUrl = `https://expo.dev/signup`;
21  const failedMessage = `Unable to open a web browser. Register an account at: ${registrationUrl}`;
22  const spinner = ora(`Opening ${registrationUrl}`).start();
23  try {
24    const opened = await openBrowserAsync(registrationUrl);
25
26    if (opened) {
27      spinner.succeed(`Opened ${registrationUrl}`);
28    } else {
29      spinner.fail(failedMessage);
30    }
31    return;
32  } catch (error) {
33    spinner.fail(failedMessage);
34    throw error;
35  }
36}
37