Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent errors in finding workspaceId from interrupting link prompts #10681

Merged
merged 1 commit into from
Apr 4, 2024
Merged
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/khaki-turkeys-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Prevent errors in finding workspaceId from interrupting link prompts
25 changes: 16 additions & 9 deletions packages/db/src/core/cli/commands/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ export async function cmd() {
console.error(MISSING_SESSION_ID_ERROR);
process.exit(1);
}
const getWorkspaceIdAsync = getWorkspaceId();
const getWorkspaceIdAsync = getWorkspaceId().catch(err => {
return err as Error;
});
await promptBegin();
const isLinkExisting = await promptLinkExisting();
if (isLinkExisting) {
const workspaceId = await getWorkspaceIdAsync;
const workspaceId = unwrapWorkspaceId(await getWorkspaceIdAsync);
const existingProjectData = await promptExistingProjectName({ workspaceId });
return await linkProject(existingProjectData.id);
}

const isLinkNew = await promptLinkNew();
if (isLinkNew) {
const workspaceId = await getWorkspaceIdAsync;
const workspaceId = unwrapWorkspaceId(await getWorkspaceIdAsync);
const newProjectName = await promptNewProjectName();
const newProjectRegion = await promptNewProjectRegion();
const spinner = ora('Creating new project...').start();
Expand Down Expand Up @@ -64,26 +66,31 @@ async function getWorkspaceId(): Promise<string> {
(res) => {
// Unauthorized
if (res.status === 401) {
console.error(
throw new Error(
`${bgRed('Unauthorized')}\n\n Are you logged in?\n Run ${cyan(
'astro db login'
)} to authenticate and then try linking again.\n\n`
);
process.exit(1);
}
console.error(`Failed to fetch user workspace: ${res.status} ${res.statusText}`);
process.exit(1);
throw new Error(`Failed to fetch user workspace: ${res.status} ${res.statusText}`);
}
);

const { data, success } = (await response.json()) as Result<{ id: string }[]>;
if (!success) {
console.error(`Failed to fetch user's workspace.`);
process.exit(1);
throw new Error(`Failed to fetch user's workspace.`);
}
return data[0].id;
}

function unwrapWorkspaceId(workspaceId: string | Error): string {
if(typeof workspaceId !== 'string') {
console.error(workspaceId.message);
process.exit(1);
}
return workspaceId;
}

export async function createNewProject({
workspaceId,
name,
Expand Down
Loading