-
Notifications
You must be signed in to change notification settings - Fork 981
/
Copy pathemulators.ts
222 lines (189 loc) · 6.98 KB
/
emulators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import vscode, { Disposable, ThemeColor } from "vscode";
import { Emulators, getEmulatorUiUrl } from "../cli";
import { ExtensionBrokerImpl } from "../extension-broker";
import { firebaseRC } from "./config";
import { EmulatorsStatus, RunningEmulatorInfo } from "../messaging/types";
import { EmulatorHubClient } from "../../../src/emulator/hubClient";
import { GetEmulatorsResponse } from "../../../src/emulator/hub";
import { EmulatorInfo } from "../emulator/types";
import { signal } from "@preact/signals-core";
import { dataConnectConfigs } from "../data-connect/config";
import { runEmulatorIssuesStream } from "../data-connect/emulator-stream";
import { getSettings } from "../utils/settings";
export class EmulatorsController implements Disposable {
constructor(private broker: ExtensionBrokerImpl) {
this.emulatorStatusItem.command = "firebase.openFirebaseRc";
// called by emulator UI
this.subscriptions.push(
broker.on("getEmulatorInfos", () => this.findRunningCliEmulators()),
);
// called by emulator UI
this.subscriptions.push(
broker.on("runStartEmulators", () => {
this.setEmulatorsStarting();
}),
);
// Subscription to open up settings window
this.subscriptions.push(
broker.on("fdc.open-emulator-settings", () => {
vscode.commands.executeCommand( 'workbench.action.openSettings', 'firebase.emulators' );
})
);
// Subscription to trigger clear emulator data when button is clicked.
this.subscriptions.push(
broker.on("fdc.clear-emulator-data", () => {
vscode.commands.executeCommand("firebase.emulators.clearData");
}),
);
// Subscription to trigger emulator exports when button is clicked.
this.subscriptions.push(broker.on("runEmulatorsExport", () => {
vscode.commands.executeCommand("firebase.emulators.exportData")
}));
}
readonly emulatorStatusItem = vscode.window.createStatusBarItem("emulators");
private currExecId = 0;
public async startEmulators() {
this.setEmulatorsStarting();
vscode.commands.executeCommand("firebase.emulators.start");
}
// called by webhook
private readonly findRunningEmulatorsCommand =
vscode.commands.registerCommand(
"firebase.emulators.findRunning",
this.findRunningCliEmulators.bind(this),
);
// called by webhook
private readonly emulatorsStoppped = vscode.commands.registerCommand(
"firebase.emulators.stopped",
this.setEmulatorsStopped.bind(this),
);
private readonly clearEmulatorDataCommand = vscode.commands.registerCommand(
"firebase.emulators.clearData",
this.clearDataConnectData.bind(this),
);
private readonly exportEmulatorDataCommand = vscode.commands.registerCommand(
"firebase.emulators.exportData",
this.exportEmulatorData.bind(this),
);
readonly emulators: { status: EmulatorsStatus; infos?: RunningEmulatorInfo } =
{
status: "stopped",
};
private readonly subscriptions: (() => void)[] = [];
notifyEmulatorStateChanged() {
this.broker.send("notifyEmulatorStateChanged", this.emulators);
}
// TODO: Move all api calls to CLI DataConnectEmulatorClient
public getLocalEndpoint = () => {
const emulatorInfos = this.emulators.infos?.displayInfo;
const dataConnectEmulator = emulatorInfos?.find(
(emulatorInfo) => emulatorInfo.name === Emulators.DATACONNECT,
);
if (!dataConnectEmulator) {
return undefined;
}
// handle ipv6
if (dataConnectEmulator.host.includes(":")) {
return `http://[${dataConnectEmulator.host}]:${dataConnectEmulator.port}`;
}
return `http://${dataConnectEmulator.host}:${dataConnectEmulator.port}`;
};
public setEmulatorsRunningInfo(info: EmulatorInfo[]) {
this.emulators.infos = {
uiUrl: getEmulatorUiUrl()!,
displayInfo: info,
};
this.emulators.status = "running";
this.notifyEmulatorStateChanged();
this.connectToEmulatorStream();
}
public setEmulatorsStarting() {
this.emulators.status = "starting";
this.notifyEmulatorStateChanged();
this.currExecId += 1;
const execId = this.currExecId;
// fallback in case we're stuck in a loading state
setTimeout(async () => {
if (this.emulators.status === "starting" && this.currExecId === execId) {
// notify UI to show reset
this.broker.send("notifyEmulatorsHanging", true);
}
}, 10000); // default 10 seconds spin up time
}
public setEmulatorsStopping() {
this.emulators.status = "stopping";
this.notifyEmulatorStateChanged();
}
public setEmulatorsStopped() {
this.emulators.status = "stopped";
this.notifyEmulatorStateChanged();
}
async findRunningCliEmulators(): Promise<
{ status: EmulatorsStatus; infos?: RunningEmulatorInfo }
> {
const hubClient = this.getHubClient();
if (hubClient) {
const response: GetEmulatorsResponse = await hubClient.getEmulators();
if (Object.values(response)) {
this.setEmulatorsRunningInfo(Object.values(response));
} else {
this.setEmulatorsStopped();
}
}
return this.emulators;
}
async clearDataConnectData(): Promise<void> {
const hubClient = this.getHubClient();
if (hubClient) {
await hubClient.clearDataConnectData();
vscode.window.showInformationMessage(`Data Connect emulator data has been cleared.`);
}
}
async exportEmulatorData(): Promise<void> {
const settings = getSettings();
const exportDir = settings.exportPath;
const hubClient = this.getHubClient();
if (hubClient) {
// TODO: Make exportDir configurable
await hubClient.postExport({path: exportDir, initiatedBy: "Data Connect VSCode extension"});
vscode.window.showInformationMessage(`Emulator Data exported to ${exportDir}`);
}
}
private getHubClient(): EmulatorHubClient | undefined {
const projectId = firebaseRC.value?.tryReadValue?.projects?.default;
// TODO: think about what to without projectID, in potentially a logged out mode
const hubClient = new EmulatorHubClient(projectId!);
if (hubClient.foundHub()) {
return hubClient;
} else {
this.setEmulatorsStopped();
}
}
public async areEmulatorsRunning(): Promise<boolean> {
if (this.emulators.status === "running") {
return true;
}
return (await this.findRunningCliEmulators())?.status === "running";
}
/** FDC specific functions */
readonly isPostgresEnabled = signal(false);
private connectToEmulatorStream() {
const configs = dataConnectConfigs.value?.tryReadValue!;
if (this.getLocalEndpoint()) {
// only if FDC emulator endpoint is found
runEmulatorIssuesStream(
configs,
this.getLocalEndpoint()!,
this.isPostgresEnabled,
);
}
}
dispose(): void {
this.subscriptions.forEach((subscription) => subscription());
this.findRunningEmulatorsCommand.dispose();
this.emulatorStatusItem.dispose();
this.emulatorsStoppped.dispose();
this.clearEmulatorDataCommand.dispose();
this.exportEmulatorDataCommand.dispose();
}
}