@@ -6,6 +6,7 @@ import { EmulatorsStatus, RunningEmulatorInfo } from "../messaging/types";
66import { EmulatorHubClient } from "../../../src/emulator/hubClient" ;
77import { GetEmulatorsResponse } from "../../../src/emulator/hub" ;
88import { EmulatorInfo } from "../emulator/types" ;
9+ import { getSettings } from "../utils/settings" ;
910export class EmulatorsController implements Disposable {
1011 constructor ( private broker : ExtensionBrokerImpl ) {
1112 this . emulatorStatusItem . command = "firebase.openFirebaseRc" ;
@@ -21,6 +22,25 @@ export class EmulatorsController implements Disposable {
2122 this . setEmulatorsStarting ( ) ;
2223 } ) ,
2324 ) ;
25+
26+ // Subscription to open up settings window
27+ this . subscriptions . push (
28+ broker . on ( "fdc.open-emulator-settings" , ( ) => {
29+ vscode . commands . executeCommand ( 'workbench.action.openSettings' , 'firebase.emulators' ) ;
30+ } )
31+ ) ;
32+
33+ // Subscription to trigger clear emulator data when button is clicked.
34+ this . subscriptions . push (
35+ broker . on ( "fdc.clear-emulator-data" , ( ) => {
36+ vscode . commands . executeCommand ( "firebase.emulators.clearData" ) ;
37+ } ) ,
38+ ) ;
39+
40+ // Subscription to trigger emulator exports when button is clicked.
41+ this . subscriptions . push ( broker . on ( "runEmulatorsExport" , ( ) => {
42+ vscode . commands . executeCommand ( "firebase.emulators.exportData" )
43+ } ) ) ;
2444 }
2545
2646 readonly emulatorStatusItem = vscode . window . createStatusBarItem ( "emulators" ) ;
@@ -39,6 +59,17 @@ export class EmulatorsController implements Disposable {
3959 this . setEmulatorsStopped . bind ( this ) ,
4060 ) ;
4161
62+ private readonly clearEmulatorDataCommand = vscode . commands . registerCommand (
63+ "firebase.emulators.clearData" ,
64+ this . clearDataConnectData . bind ( this ) ,
65+ ) ;
66+
67+
68+ private readonly exportEmulatorDataCommand = vscode . commands . registerCommand (
69+ "firebase.emulators.exportData" ,
70+ this . exportEmulatorData . bind ( this ) ,
71+ ) ;
72+
4273 readonly emulators : { status : EmulatorsStatus ; infos ?: RunningEmulatorInfo } =
4374 {
4475 status : "stopped" ,
@@ -107,23 +138,47 @@ export class EmulatorsController implements Disposable {
107138 async findRunningCliEmulators ( ) : Promise <
108139 { status : EmulatorsStatus ; infos ?: RunningEmulatorInfo } | undefined
109140 > {
110- const projectId = firebaseRC . value ?. tryReadValue ?. projects ?. default ;
111- // TODO: think about what to without projectID, in potentially a logged out mode
112- const hubClient = new EmulatorHubClient ( projectId ! ) ;
113-
114- if ( hubClient . foundHub ( ) ) {
141+ const hubClient = this . getHubClient ( ) ;
142+ if ( hubClient ) {
115143 const response : GetEmulatorsResponse = await hubClient . getEmulators ( ) ;
116144
117145 if ( Object . values ( response ) ) {
118146 this . setEmulatorsRunningInfo ( Object . values ( response ) ) ;
119147 } else {
120148 this . setEmulatorsStopped ( ) ;
121149 }
150+ }
151+ return this . emulators ;
152+ }
153+
154+ async clearDataConnectData ( ) : Promise < void > {
155+ const hubClient = this . getHubClient ( ) ;
156+ if ( hubClient ) {
157+ await hubClient . clearDataConnectData ( ) ;
158+ vscode . window . showInformationMessage ( `Data Connect emulator data has been cleared.` ) ;
159+ }
160+ }
161+
162+ async exportEmulatorData ( ) : Promise < void > {
163+ const settings = getSettings ( ) ;
164+ const exportDir = settings . exportPath ;
165+ const hubClient = this . getHubClient ( ) ;
166+ if ( hubClient ) {
167+ // TODO: Make exportDir configurable
168+ await hubClient . postExport ( { path : exportDir , initiatedBy : "Data Connect VSCode extension" } ) ;
169+ vscode . window . showInformationMessage ( `Emulator Data exported to ${ exportDir } ` ) ;
170+ }
171+ }
172+
173+ private getHubClient ( ) : EmulatorHubClient | undefined {
174+ const projectId = firebaseRC . value ?. tryReadValue ?. projects ?. default ;
175+ // TODO: think about what to without projectID, in potentially a logged out mode
176+ const hubClient = new EmulatorHubClient ( projectId ! ) ;
177+ if ( hubClient . foundHub ( ) ) {
178+ return hubClient ;
122179 } else {
123180 this . setEmulatorsStopped ( ) ;
124181 }
125-
126- return this . emulators ;
127182 }
128183
129184 public areEmulatorsRunning ( ) {
@@ -135,5 +190,7 @@ export class EmulatorsController implements Disposable {
135190 this . findRunningEmulatorsCommand . dispose ( ) ;
136191 this . emulatorStatusItem . dispose ( ) ;
137192 this . emulatorsStoppped . dispose ( ) ;
193+ this . clearEmulatorDataCommand . dispose ( ) ;
194+ this . exportEmulatorDataCommand . dispose ( ) ;
138195 }
139196}
0 commit comments