@@ -3,32 +3,39 @@ import * as childProcess from "child_process";
33import { dataConnectLocalConnString } from "../api" ;
44import { Constants } from "./constants" ;
55import { getPID , start , stop , downloadIfNecessary } from "./downloadableEmulators" ;
6- import { EmulatorInfo , EmulatorInstance , Emulators } from "./types" ;
6+ import { EmulatorInfo , EmulatorInstance , Emulators , ListenSpec } from "./types" ;
77import { FirebaseError } from "../error" ;
88import { EmulatorLogger } from "./emulatorLogger" ;
99import { RC } from "../rc" ;
1010import { BuildResult , requiresVector } from "../dataconnect/types" ;
11+ import { listenSpecsToString } from "./portUtils" ;
1112
1213export interface DataConnectEmulatorArgs {
1314 projectId ?: string ;
14- port ?: number ;
15- host ?: string ;
16- configDir ?: string ;
15+ listen : ListenSpec [ ] ;
16+ configDir : string ;
1717 locationId ?: string ;
1818 auto_download ?: boolean ;
1919 rc : RC ;
2020}
2121
22- const grpcDefaultPort = 9510 ;
22+ export interface DataConnectGenerateArgs {
23+ configDir : string ;
24+ locationId : string ;
25+ connectorId : string ;
26+ }
27+
28+ export interface DataConnectBuildArgs {
29+ configDir : string ;
30+ }
2331
2432export class DataConnectEmulator implements EmulatorInstance {
2533 constructor ( private args : DataConnectEmulatorArgs ) { }
2634 private logger = EmulatorLogger . forEmulator ( Emulators . DATACONNECT ) ;
2735
2836 async start ( ) : Promise < void > {
29- const port = this . args . port || Constants . getDefaultPort ( Emulators . DATACONNECT ) ;
3037 this . logger . log ( "DEBUG" , `Using Postgres connection string: ${ this . getLocalConectionString ( ) } ` ) ;
31- const info = await this . build ( ) ;
38+ const info = await DataConnectEmulator . build ( { configDir : this . args . configDir } ) ;
3239 if ( requiresVector ( info . metadata ) ) {
3340 if ( Constants . isDemoProject ( this . args . projectId ) ) {
3441 this . logger . logLabeled (
@@ -46,8 +53,7 @@ export class DataConnectEmulator implements EmulatorInstance {
4653 }
4754 return start ( Emulators . DATACONNECT , {
4855 ...this . args ,
49- http_port : port ,
50- grpc_port : grpcDefaultPort ,
56+ listen : listenSpecsToString ( this . args . listen ) ,
5157 config_dir : this . args . configDir ,
5258 local_connection_string : this . getLocalConectionString ( ) ,
5359 project_id : this . args . projectId ,
@@ -65,13 +71,11 @@ export class DataConnectEmulator implements EmulatorInstance {
6571 }
6672
6773 getInfo ( ) : EmulatorInfo {
68- const host = this . args . host || Constants . getDefaultHost ( ) ;
69- const port = this . args . port || Constants . getDefaultPort ( Emulators . DATACONNECT ) ;
70-
7174 return {
7275 name : this . getName ( ) ,
73- host,
74- port,
76+ listen : this . args . listen ,
77+ host : this . args . listen [ 0 ] . address ,
78+ port : this . args . listen [ 0 ] . port ,
7579 pid : getPID ( Emulators . DATACONNECT ) ,
7680 timeout : 10_000 ,
7781 } ;
@@ -80,26 +84,33 @@ export class DataConnectEmulator implements EmulatorInstance {
8084 return Emulators . DATACONNECT ;
8185 }
8286
83- async generate ( connectorId : string ) : Promise < string > {
87+ static async generate ( args : DataConnectGenerateArgs ) : Promise < string > {
8488 const commandInfo = await downloadIfNecessary ( Emulators . DATACONNECT ) ;
8589 const cmd = [
8690 "generate" ,
87- `--service_location=${ this . args . locationId } ` ,
88- `--config_dir=${ this . args . configDir } ` ,
89- `--connector_id=${ connectorId } ` ,
91+ `--service_location=${ args . locationId } ` ,
92+ `--config_dir=${ args . configDir } ` ,
93+ `--connector_id=${ args . connectorId } ` ,
9094 ] ;
9195 const res = childProcess . spawnSync ( commandInfo . binary , cmd , { encoding : "utf-8" } ) ;
9296 if ( res . error ) {
93- throw new FirebaseError ( `Error starting up Data Connect emulator: ${ res . error } ` ) ;
97+ throw new FirebaseError ( `Error starting up Data Connect generate: ${ res . error . message } ` , {
98+ original : res . error ,
99+ } ) ;
94100 }
95101 return res . stdout ;
96102 }
97103
98- async build ( ) : Promise < BuildResult > {
104+ static async build ( args : DataConnectBuildArgs ) : Promise < BuildResult > {
99105 const commandInfo = await downloadIfNecessary ( Emulators . DATACONNECT ) ;
100- const cmd = [ "build" , `--config_dir=${ this . args . configDir } ` ] ;
106+ const cmd = [ "build" , `--config_dir=${ args . configDir } ` ] ;
101107
102108 const res = childProcess . spawnSync ( commandInfo . binary , cmd , { encoding : "utf-8" } ) ;
109+ if ( res . error ) {
110+ throw new FirebaseError ( `Error starting up Data Connect build: ${ res . error . message } ` , {
111+ original : res . error ,
112+ } ) ;
113+ }
103114 if ( res . stderr ) {
104115 throw new FirebaseError (
105116 `Unable to build your Data Connect schema and connectors: ${ res . stderr } ` ,
0 commit comments