Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/preserve-creatable-project-accounts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eas-cli': patch
---

When a project is unconfigured, only suggest creating it if the current credential can create projects on the target account.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
getPrivateExpoConfigAsync,
} from '../../../project/expoConfig';
import { fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync } from '../../../project/fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync';
import { getUnconfiguredProjectError } from '../../../project/projectNotConfiguredError';
import { ProjectNotConfiguredError } from '../../../project/projectNotConfiguredError';
import { promptAsync } from '../../../prompts';
import SessionManager from '../../../user/SessionManager';
import { Actor, getActorUsername } from '../../../user/User';
Expand Down Expand Up @@ -230,7 +230,7 @@ async function chooseAccountNameForEASProjectAsync(
}

if (actor.__typename === 'Robot') {
throw getUnconfiguredProjectError({
throw new ProjectNotConfiguredError({
actor,
reason: 'A robot access token cannot configure it interactively.',
additionalFix: 'Alternatively, set the "owner" field in your app config.',
Expand All @@ -243,7 +243,7 @@ async function chooseAccountNameForEASProjectAsync(
}

if (options.nonInteractive) {
throw getUnconfiguredProjectError({ actor });
throw new ProjectNotConfiguredError({ actor });
}

const choices = getAccountChoices(actor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ import { jester } from '../../credentials/__tests__/fixtures-constants';
import { fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync } from '../fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync';

describe(fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync, () => {
it('throws when non-interactive is specified but interaction is required', async () => {
const graphqlClient = instance(mock<ExpoGraphqlClient>());
await expect(
fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync(
graphqlClient,
{ accountName: 'fake', projectName: 'fake' },
{ nonInteractive: true },
jester
)
).rejects.toThrow(
`EAS project not configured. This command cannot configure it in non-interactive mode. ` +
`Run one of the following, then re-run this command:\n\n` +
`To link an existing project:\n\n` +
` eas init --id <project-id> --non-interactive\n\n` +
`To create a new project:\n\n` +
` eas init --account fake --non-interactive\n\n` +
`Accounts you can create projects in: jester`
);
});
it.each([
{ accounts: jester.accounts, eligibleAccounts: ['jester'] },
{ accounts: [], eligibleAccounts: [] },
])(
'only suggests linking when the owner cannot create projects',
async ({ accounts, eligibleAccounts }) => {
const graphqlClient = instance(mock<ExpoGraphqlClient>());
await expect(
fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsync(
graphqlClient,
{ accountName: 'fake', projectName: 'fake' },
{ nonInteractive: true },
{ ...jester, accounts }
)
).rejects.toMatchObject({
accountName: 'fake',
eligibleAccounts,
message:
`EAS project not configured. This command cannot configure it in non-interactive mode. ` +
`To link an existing project:\n\n` +
` eas init --id <project-id> --non-interactive`,
});
}
);
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';

import { getUnconfiguredProjectError } from './projectNotConfiguredError';
import { ProjectNotConfiguredError } from './projectNotConfiguredError';
import { getProjectDashboardUrl } from '../build/utils/url';
import { ExpoGraphqlClient } from '../commandUtils/context/contextUtils/createGraphqlClient';
import { AppMutation } from '../graphql/mutations/AppMutation';
Expand Down Expand Up @@ -33,7 +33,7 @@ export async function fetchOrCreateProjectIDForWriteToConfigWithConfirmationAsyn
const allAccounts = actor.accounts;

if (options.nonInteractive) {
throw getUnconfiguredProjectError({ actor, accountName });
throw new ProjectNotConfiguredError({ actor, accountName });
}
const account = allAccounts.find(a => a.name === accountName);
if (!account) {
Expand Down
60 changes: 34 additions & 26 deletions packages/eas-cli/src/project/projectNotConfiguredError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,39 @@ import { Actor, getCreatableAccountNames } from '../user/User';
* Build the error shown when an EAS project is not configured and the CLI cannot configure it
* without more input. Lists the exact commands to run, because bare "eas init" also needs
* interaction and fails in the same situation.
*
* @param actor the logged-in actor
* @param accountName the account that should own the project, when it is already known
* @param reason why the CLI cannot configure the project here
* @param additionalFix an extra way to resolve the error, appended at the end
*/
export function getUnconfiguredProjectError({
actor,
accountName,
reason = 'This command cannot configure it in non-interactive mode.',
additionalFix,
}: {
actor: Actor;
accountName?: string;
reason?: string;
additionalFix?: string;
}): Error {
return new Error(
`EAS project not configured. ${reason} ` +
`Run one of the following, then re-run this command:\n\n` +
`To link an existing project:\n\n` +
` eas init --id <project-id> --non-interactive\n\n` +
`To create a new project:\n\n` +
` eas init --account ${accountName ?? '<account-name>'} --non-interactive\n\n` +
`Accounts you can create projects in: ${getCreatableAccountNames(actor).join(', ')}` +
(additionalFix ? `\n\n${additionalFix}` : '')
);
export class ProjectNotConfiguredError extends Error {
readonly accountName?: string;
readonly eligibleAccounts: string[];

constructor({
actor,
accountName,
reason = 'This command cannot configure it in non-interactive mode.',
additionalFix,
}: {
actor: Actor;
accountName?: string;
reason?: string;
additionalFix?: string;
}) {
const eligibleAccounts = getCreatableAccountNames(actor);
const canCreate =
accountName === undefined
? eligibleAccounts.length > 0
: eligibleAccounts.includes(accountName);
const linkExisting =
'To link an existing project:\n\n eas init --id <project-id> --non-interactive';
const guidance = canCreate
? `Run one of the following, then re-run this command:\n\n${linkExisting}\n\n` +
`To create a new project:\n\n eas init --account ${accountName ?? '<account-name>'} --non-interactive\n\n` +
`Accounts you can create projects in: ${eligibleAccounts.join(', ')}`
: linkExisting;
super(
`EAS project not configured. ${reason} ${guidance}` +
(additionalFix ? `\n\n${additionalFix}` : '')
);
this.accountName = accountName;
this.eligibleAccounts = eligibleAccounts;
}
}
Loading