Skip to content

Commit

Permalink
Pick up project selection when running in IDX (#7550)
Browse files Browse the repository at this point in the history
  • Loading branch information
joehan authored Aug 12, 2024
1 parent 83154d9 commit f9fff1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
2 changes: 2 additions & 0 deletions firebase-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## NEXT

- Automatically pick up IDX project selection.

## 0.5.4

- Updated internal firebase-tools dependency to 13.15.1
Expand Down
9 changes: 6 additions & 3 deletions firebase-vscode/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import * as commandUtils from "../../src/emulator/commandUtils";
import { currentUser } from "./core/user";
import { firstWhere } from "./utils/signal";
import { currentProjectId } from "./core/project";
export { Emulators };

/**
Expand All @@ -46,7 +47,6 @@ export async function requireAuthWrapper(

// helper to determine if VSCode options has the same account as global default
function isUserMatching(account: Account, options: Options) {

if (!options.user || !options.tokens) {
return false;
}
Expand All @@ -56,7 +56,7 @@ export async function requireAuthWrapper(
return (
account &&
account.user.email === optionsUser.email &&
account.tokens.refresh_token === optionsTokens.refresh_token // Should check refresh token which is consistent, not access_token which is short lived.
account.tokens.refresh_token === optionsTokens.refresh_token // Should check refresh token which is consistent, not access_token which is short lived.
);
}

Expand Down Expand Up @@ -86,7 +86,10 @@ export async function requireAuthWrapper(
// - we are using tokens from configstore (aka those set by firebase login), AND
// - we are calling CLI code that skips Command (where we normally call this)
currentOptions.value = optsCopy;
setAccessToken(await getAccessToken());
if (optsCopy.projectId) {
currentProjectId.value = optsCopy.projectId;
}
setAccessToken(await getAccessToken());
if (userEmail) {
pluginLogger.debug("User found: ", userEmail);

Expand Down
52 changes: 17 additions & 35 deletions firebase-vscode/src/core/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,13 @@ const userScopedProjects = computed<FirebaseProjectMetadata[] | undefined>(
},
);

// TODO(hlshen): clean up concept of currentProject and currentProjectId
/** Gets the currently selected project, fallback to first default project in RC file */
export const currentProject = computed<FirebaseProjectMetadata | undefined>(
() => {
// Service accounts should only have one project
if (isServiceAccount.value) {
return userScopedProjects.value?.[0];
}

const wantProjectId =
currentProjectId.value ||
firebaseRC.value?.tryReadValue?.projects["default"];
if (!wantProjectId) {
return undefined;
}

return userScopedProjects.value?.find((p) => p.projectId === wantProjectId);
},
);

export function registerProject(broker: ExtensionBrokerImpl): Disposable {

async function fetchNewProjects(user: User) {
const userProjects = await listProjects();
projects.value = {
...projects.value,
[user.email]: userProjects,
};
const userProjects = await listProjects();
projects.value = {
...projects.value,
[user.email]: userProjects,
};
}

const sub1 = effect(() => {
Expand All @@ -63,7 +42,7 @@ export function registerProject(broker: ExtensionBrokerImpl): Disposable {

const sub2 = effect(() => {
broker.send("notifyProjectChanged", {
projectId: currentProject.value?.projectId ?? "",
projectId: currentProjectId.value ?? "",
});
});

Expand All @@ -85,18 +64,21 @@ export function registerProject(broker: ExtensionBrokerImpl): Disposable {
});

const sub5 = broker.on("getInitialData", () => {
let wantProjectId =
currentProjectId.value ||
firebaseRC.value?.tryReadValue?.projects["default"];
// Service accounts should only have one project
if (isServiceAccount.value) {
wantProjectId = userScopedProjects.value?.[0].projectId;
}

broker.send("notifyProjectChanged", {
projectId: currentProject.value?.projectId ?? "",
projectId: wantProjectId ?? "",
});
});

// TODO: Set projectId from IDX Auth
// const monospaceLoginSub = effect(() => {
// if (currentOptions.value.projectId) {
// currentProjectId.value = currentOptions.value.projectId;
// }
// })

// TODO: In IDX, should we just client.GetProject() from the metadata server?
// Should we instead hide this command entirely?
const command = vscode.commands.registerCommand(
"firebase.selectProject",
async () => {
Expand Down

0 comments on commit f9fff1b

Please sign in to comment.