Skip to content

Commit

Permalink
Show "Run firebase init" if dataconnect configs are missing (#7187)
Browse files Browse the repository at this point in the history
Co-authored-by: Harold Shen <hlshen@google.com>
  • Loading branch information
rrousselGit and hlshen committed May 21, 2024
1 parent fcc84ce commit 134d94a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 19 deletions.
4 changes: 4 additions & 0 deletions firebase-vscode/common/messaging/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface WebviewToExtensionParamsMap {
* Ask extension for initial data
*/
getInitialData: {};
getInitialHasFdcConfigs: void;

addUser: {};
logout: { email: string };

Expand Down Expand Up @@ -183,6 +185,8 @@ export interface ExtensionToWebviewParamsMap {
firebaseJson: ValueOrError<FirebaseConfig> | undefined;
firebaseRC: ValueOrError<RCData> | undefined;
};
/** Whether any dataconnect.yaml is present */
notifyHasFdcConfigs: boolean;

/**
* Return user-selected preview channel name
Expand Down
21 changes: 17 additions & 4 deletions firebase-vscode/src/data-connect/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { isPathInside } from "./file-utils";
import { DeepReadOnly } from "../metaprogramming";
import { ConnectorYaml, DataConnectYaml } from "../dataconnect/types";
import { Result, ResultError, ResultValue } from "../result";
import { globalSignal } from "../utils/globals";
import { Result, ResultValue } from "../result";
import { computed, effect, signal } from "@preact/signals-core";
import {
_createWatcher as createWatcher,
Expand All @@ -17,16 +16,19 @@ import {
readFirebaseJson as readFdcFirebaseJson,
} from "../../../src/dataconnect/fileUtils";
import { Config } from "../config";
import { DataConnectConfig, DataConnectMultiple } from "../firebaseConfig";
import { DataConnectMultiple } from "../firebaseConfig";
import path from "path";
import { ExtensionBrokerImpl } from "../extension-broker";

export * from "../core/config";

export const dataConnectConfigs = signal<
Result<ResolvedDataConnectConfigs | undefined> | undefined
>(undefined);

export function registerDataConnectConfigs(): vscode.Disposable {
export function registerDataConnectConfigs(
broker: ExtensionBrokerImpl,
): vscode.Disposable {
let cancel: () => void | undefined;

function handleResult(
Expand Down Expand Up @@ -62,8 +64,19 @@ export function registerDataConnectConfigs(): vscode.Disposable {
dataConnectWatcher?.onDidDelete(() => handleResult(undefined));
// TODO watch connectors

const hasConfigs = computed(() => !!dataConnectConfigs.value?.tryReadValue?.values.length);

const hasConfigSub = effect(() => {
broker.send("notifyHasFdcConfigs", hasConfigs.value);
});
const getInitialHasFdcConfigsSub = broker.on("getInitialHasFdcConfigs", () => {
broker.send("notifyHasFdcConfigs", hasConfigs.value);
});

return vscode.Disposable.from(
{ dispose: sub },
{ dispose: hasConfigSub },
{ dispose: getInitialHasFdcConfigsSub },
{ dispose: () => cancel?.() },
dataConnectWatcher,
);
Expand Down
2 changes: 1 addition & 1 deletion firebase-vscode/src/data-connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function registerFdc(
selectedProjectStatus.show();
}),
},
registerDataConnectConfigs(),
registerDataConnectConfigs(broker),
registerExecution(context, broker, fdcService, emulatorController),
registerExplorer(context, broker, fdcService),
registerFirebaseDataConnectView(context, broker, emulatorController),
Expand Down
5 changes: 3 additions & 2 deletions firebase-vscode/src/test/suite/src/dataconnect/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ firebaseSuite("registerDataConnectConfigs", async () => {
firebaseConfig: { emulators: { dataconnect: { port: 9399 } } },
});

const disposable = registerDataConnectConfigs();
const disposable = registerDataConnectConfigs(broker);
addDisposable(disposable);

broker.simulateOn("getInitialData");
Expand Down Expand Up @@ -91,7 +91,8 @@ firebaseSuite("registerDataConnectConfigs", async () => {
}),
);

const disposable = await registerDataConnectConfigs();
const broker = createTestBroker();
const disposable = await registerDataConnectConfigs(broker);
addDisposable(disposable);

const dataConnectListeners = watcherListeners["**/{dataconnect,connector}.yaml"]!;
Expand Down
32 changes: 20 additions & 12 deletions firebase-vscode/webviews/SidebarApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ import { Spacer } from "./components/ui/Spacer";
import { broker, useBroker } from "./globals/html-broker";
import { AccountSection } from "./components/AccountSection";
import { ProjectSection } from "./components/ProjectSection";
import { DeployPanel } from "./components/DeployPanel";
import { HostingInitState, DeployState } from "./webview-types";
import { EmulatorPanel } from "./components/EmulatorPanel";

import { webLogger } from "./globals/web-logger";
import { InitFirebasePanel } from "./components/InitPanel";
import { ValueOrError } from "./messaging/protocol";
import { FirebaseConfig } from "../../src/firebaseConfig";
import { RCData } from "../../src/rc";
import { VSCodeButton } from "@vscode/webview-ui-toolkit/react";
import { ServiceAccountUser } from "../common/types";

export function SidebarApp() {
const env = useBroker("notifyEnv")?.env;
Expand All @@ -28,6 +24,10 @@ export function SidebarApp() {
const configs = useBroker("notifyFirebaseConfig", {
initialRequest: "getInitialData",
});
const hasFdcConfigs =
useBroker("notifyHasFdcConfigs", {
initialRequest: "getInitialHasFdcConfigs",
}) ?? false;
const accountSection = (
<AccountSection
user={user}
Expand All @@ -46,12 +46,14 @@ export function SidebarApp() {
</>
);
}
if (!configs?.firebaseJson) {
if (!configs?.firebaseJson?.value || !hasFdcConfigs) {
const configLabel = !hasFdcConfigs ? "dataconnect.yaml" : "firebase.json";

return (
<>
{accountSection}
<p>
No <code>firebase.json</code> detected in this project
No <code>{configLabel}</code> detected in this project
</p>
<br />
<VSCodeButton
Expand Down Expand Up @@ -103,7 +105,7 @@ function SidebarContent(props: {
webLogger.debug(
"notifyFirebaseConfig",
JSON.stringify(firebaseJson),
JSON.stringify(firebaseRC)
JSON.stringify(firebaseRC),
);
if (firebaseJson?.value?.hosting) {
webLogger.debug("Detected firebase.json");
Expand All @@ -130,7 +132,7 @@ function SidebarContent(props: {
} else {
setHostingInitState(null);
}
}
},
);

broker.on("notifyHostingDeploy", ({ success }) => {
Expand Down Expand Up @@ -159,10 +161,15 @@ function SidebarContent(props: {
<Spacer size="medium" />
{accountSection}
{!!user && (
<ProjectSection user={user} projectId={projectId} isMonospace={env?.isMonospace} />
<ProjectSection
user={user}
projectId={projectId}
isMonospace={env?.isMonospace}
/>
)}
{ // TODO: disable hosting completely
/* {hostingInitState === "success" &&
{
// TODO: disable hosting completely
/* {hostingInitState === "success" &&
!!user &&
!!projectId &&
env?.isMonospace && (
Expand All @@ -186,7 +193,8 @@ function SidebarContent(props: {
hostingInitState={hostingInitState}
setHostingInitState={setHostingInitState}
/>
)} */}
)} */
}
{
// disable emulator panel for now, as we have an individual emulator panel in the FDC section
}
Expand Down

0 comments on commit 134d94a

Please sign in to comment.