Skip to content

Commit

Permalink
Prevent errors in finding workspaceId from interrupting link prompts (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Apr 4, 2024
1 parent 9a1aee8 commit 4bf8bd3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
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

0 comments on commit 4bf8bd3

Please sign in to comment.