Skip to content

Commit

Permalink
Fix up code
Browse files Browse the repository at this point in the history
Update registerAsync.ts
Remove unused settings
Drop unused
Update otp.ts
Update whoamiAsync.ts
  • Loading branch information
EvanBacon committed Jan 28, 2022
1 parent fb3dbe2 commit 979519b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 53 deletions.
8 changes: 5 additions & 3 deletions packages/expo/cli/register/registerAsync.ts
Expand Up @@ -16,17 +16,19 @@ export async function registerAsync() {
}

const registrationUrl = `https://expo.dev/signup`;

const failedMessage = `Unable to open a web browser. Register an account at: ${registrationUrl}`;
const spinner = ora(`Opening ${registrationUrl}`).start();
try {
const opened = openBrowserAsync(registrationUrl);
const opened = await openBrowserAsync(registrationUrl);

if (opened) {
spinner.succeed(`Opened ${registrationUrl}`);
} else {
spinner.fail(failedMessage);
}
return;
} catch (error) {
spinner.fail(`Unable to open a web browser. Register an account at: ${registrationUrl}`);
spinner.fail(failedMessage);
throw error;
}
}
2 changes: 0 additions & 2 deletions packages/expo/cli/utils/graphql/generated.ts
Expand Up @@ -87,8 +87,6 @@ type Account = {
appCount: Scalars['Int'];
/** Build Jobs associated with this account */
buildJobs: Array<BuildJob>;
/** (EAS Build) Builds associated with this account */
builds: Array<Build>;
/**
* Coalesced Build (EAS) or BuildJob (Classic) for all apps belonging to this account.
* @deprecated Use activityTimelineProjectActivities with filterTypes instead
Expand Down
2 changes: 0 additions & 2 deletions packages/expo/cli/utils/user/UserSettings.ts
Expand Up @@ -26,9 +26,7 @@ function getConfigDirectory() {
const SETTINGS_FILE_PATH = path.join(getConfigDirectory(), 'user-settings.json');

export type UserSettingsData = {
appleId?: string;
analyticsDeviceId?: string;
analyticsEnabled?: boolean;
};

const UserSettings = new JsonFile<UserSettingsData>(SETTINGS_FILE_PATH, {
Expand Down
30 changes: 2 additions & 28 deletions packages/expo/cli/utils/user/__tests__/actions-test.ts
@@ -1,8 +1,8 @@
import { ApiV2Error } from '../../api';
import { promptAsync } from '../../prompts';
import { ensureActorHasUsername, showLoginPromptAsync } from '../actions';
import { showLoginPromptAsync } from '../actions';
import { retryUsernamePasswordAuthWithOTPAsync, UserSecondFactorDeviceMethod } from '../otp';
import { Actor, loginAsync } from '../user';
import { loginAsync } from '../user';

jest.mock('../../prompts');
jest.mock('../../api', () => {
Expand All @@ -25,32 +25,6 @@ beforeEach(() => {
asMock(loginAsync).mockReset();
});

const userStub: Actor = {
__typename: 'User',
id: 'userId',
username: 'username',
accounts: [],
isExpoAdmin: false,
};

const robotStub: Actor = {
__typename: 'Robot',
id: 'userId',
firstName: 'GLaDOS',
accounts: [],
isExpoAdmin: false,
};

describe('ensureActorHasUsername', () => {
it('returns username for user actors', () => {
expect(ensureActorHasUsername(userStub)).toBe(userStub.username);
});

it('throws for robot actors', () => {
expect(() => ensureActorHasUsername(robotStub)).toThrow('not supported for robot');
});
});

describe(showLoginPromptAsync, () => {
it('prompts for OTP when 2FA is enabled', async () => {
asMock(promptAsync)
Expand Down
8 changes: 0 additions & 8 deletions packages/expo/cli/utils/user/actions.ts
Expand Up @@ -3,7 +3,6 @@ import chalk from 'chalk';

import * as Log from '../../log';
import { ApiV2Error } from '../api';
import { CommandError } from '../errors';
import { learnMore } from '../link';
import promptAsync, { Question } from '../prompts';
import { retryUsernamePasswordAuthWithOTPAsync } from './otp';
Expand Down Expand Up @@ -87,10 +86,3 @@ export async function ensureLoggedInAsync(): Promise<Actor> {
assert(user, 'User should be logged in');
return user;
}

export function ensureActorHasUsername(user: Actor): string {
if (user.__typename === 'User') {
return user.username;
}
throw new CommandError('This action is not supported for robot users.');
}
14 changes: 5 additions & 9 deletions packages/expo/cli/utils/user/otp.ts
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk';

import * as Log from '../../log';
import { apiClient } from '../api';
import { AbortCommandError } from '../errors';
import { AbortCommandError, CommandError } from '../errors';
import { learnMore } from '../link';
import { promptAsync, selectAsync } from '../prompts';
import { loginAsync } from './user';
Expand Down Expand Up @@ -31,8 +31,8 @@ const nonInteractiveHelp = `Use the EXPO_TOKEN environment variable to authentic
async function promptForOTPAsync(cancelBehavior: 'cancel' | 'menu'): Promise<string | null> {
const enterMessage =
cancelBehavior === 'cancel'
? `press ${chalk.bold('Enter')} to cancel`
: `press ${chalk.bold('Enter')} for more options`;
? chalk`press {bold Enter} to cancel`
: chalk`press {bold Enter} for more options`;
const { otp } = await promptAsync(
{
type: 'text',
Expand All @@ -41,11 +41,7 @@ async function promptForOTPAsync(cancelBehavior: 'cancel' | 'menu'): Promise<str
},
{ nonInteractiveHelp }
);
if (!otp) {
return null;
}

return otp;
return otp || null;
}

/**
Expand All @@ -60,7 +56,7 @@ async function promptForBackupOTPAsync(
const nonPrimarySecondFactorDevices = secondFactorDevices.filter((device) => !device.is_primary);

if (nonPrimarySecondFactorDevices.length === 0) {
throw new Error(
throw new CommandError(
'No other second-factor devices set up. Ensure you have set up and certified a backup device.'
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/expo/cli/whoami/whoamiAsync.ts
Expand Up @@ -8,6 +8,6 @@ export async function whoamiAsync() {
if (user) {
Log.exit(chalk.green(getActorDisplayName(user)), 0);
} else {
Log.exit('Not logged in', 1);
Log.exit('Not logged in');
}
}

0 comments on commit 979519b

Please sign in to comment.