diff --git a/firebase-vscode/CHANGELOG.md b/firebase-vscode/CHANGELOG.md index 67687f325b0..e1de60d18ff 100644 --- a/firebase-vscode/CHANGELOG.md +++ b/firebase-vscode/CHANGELOG.md @@ -1,5 +1,7 @@ ## NEXT +- Automatically pick up IDX project selection. + ## 0.5.4 - Updated internal firebase-tools dependency to 13.15.1 diff --git a/firebase-vscode/src/cli.ts b/firebase-vscode/src/cli.ts index bb68a8bb7f9..c59f0a89100 100644 --- a/firebase-vscode/src/cli.ts +++ b/firebase-vscode/src/cli.ts @@ -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 }; /** @@ -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; } @@ -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. ); } @@ -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); diff --git a/firebase-vscode/src/core/project.ts b/firebase-vscode/src/core/project.ts index fc43276619c..541cdac2383 100644 --- a/firebase-vscode/src/core/project.ts +++ b/firebase-vscode/src/core/project.ts @@ -23,34 +23,13 @@ const userScopedProjects = computed( }, ); -// 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( - () => { - // 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(() => { @@ -63,7 +42,7 @@ export function registerProject(broker: ExtensionBrokerImpl): Disposable { const sub2 = effect(() => { broker.send("notifyProjectChanged", { - projectId: currentProject.value?.projectId ?? "", + projectId: currentProjectId.value ?? "", }); }); @@ -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 () => {