Skip to content

Commit

Permalink
Single project mode (#4890)
Browse files Browse the repository at this point in the history
* Single project mode config for the CLI and firestore.

* JSON schema update

* Prettier fix

* Prettier fixes

* Prettier

Co-authored-by: Bryan Kendall <bkend@google.com>
  • Loading branch information
christhompsongoogle and bkendall committed Oct 4, 2022
1 parent cd737c9 commit 7d9ece6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -1 +1,2 @@
- Add functions emulator support for RTDB v2 triggers (#5045).
- Enables single project mode for Firestore by default (#4890).
3 changes: 3 additions & 0 deletions schema/firebase-config.json
Expand Up @@ -266,6 +266,9 @@
},
"type": "object"
},
"singleProjectMode": {
"type": "boolean"
},
"storage": {
"additionalProperties": false,
"properties": {
Expand Down
19 changes: 18 additions & 1 deletion src/emulator/controller.ts
Expand Up @@ -622,7 +622,7 @@ export async function startAll(
host: firestoreAddr.host,
port: firestoreAddr.port,
websocket_port: websocketPort,
projectId,
project_id: projectId,
auto_download: true,
};

Expand Down Expand Up @@ -677,6 +677,23 @@ export async function startAll(
);
}

// undefined in the config defaults to setting single_project_mode.
if (
options.config.src.emulators?.singleProjectMode === undefined ||
options.config.src.emulators?.singleProjectMode
) {
if (projectId) {
args.single_project_mode = true;
args.single_project_mode_error = false;
} else {
firestoreLogger.logLabeled(
"DEBUG",
"firestore",
"Could not enable single_project_mode: missing projectId."
);
}
}

const firestoreEmulator = new FirestoreEmulator(args);
await startEmulator(firestoreEmulator);
firestoreLogger.logLabeled(
Expand Down
4 changes: 4 additions & 0 deletions src/emulator/downloadableEmulators.ts
Expand Up @@ -163,6 +163,10 @@ const Commands: { [s in DownloadableEmulators]: DownloadableEmulatorCommand } =
"websocket_port",
"functions_emulator",
"seed_from_export",
"project_id",
"single_project_mode",
// TODO(christhompson) Re-enable after firestore accepts this flag.
// "single_project_mode_error",
],
joinArgs: false,
},
Expand Down
8 changes: 5 additions & 3 deletions src/emulator/firestoreEmulator.ts
Expand Up @@ -15,11 +15,13 @@ export interface FirestoreEmulatorArgs {
port?: number;
host?: string;
websocket_port?: number;
projectId?: string;
project_id?: string;
rules?: string;
functions_emulator?: string;
auto_download?: boolean;
seed_from_export?: string;
single_project_mode?: boolean;
single_project_mode_error?: boolean;
}

export class FirestoreEmulator implements EmulatorInstance {
Expand All @@ -35,7 +37,7 @@ export class FirestoreEmulator implements EmulatorInstance {
this.args.functions_emulator = EmulatorRegistry.getInfoHostString(functionsInfo);
}

if (this.args.rules && this.args.projectId) {
if (this.args.rules && this.args.project_id) {
const rulesPath = this.args.rules;
this.rulesWatcher = chokidar.watch(rulesPath, { persistent: true, ignoreInitial: true });
this.rulesWatcher.on("change", async () => {
Expand Down Expand Up @@ -94,7 +96,7 @@ export class FirestoreEmulator implements EmulatorInstance {
}

private async updateRules(content: string): Promise<Issue[]> {
const projectId = this.args.projectId;
const projectId = this.args.project_id;

const info = this.getInfo();
const body = {
Expand Down
1 change: 1 addition & 0 deletions src/firebaseConfig.ts
Expand Up @@ -192,6 +192,7 @@ export type EmulatorsConfig = {
host?: string;
port?: number;
};
singleProjectMode?: boolean;
};

export type ExtensionsConfig = Record<string, string>;
Expand Down
5 changes: 5 additions & 0 deletions src/init/features/emulators.ts
Expand Up @@ -100,6 +100,11 @@ export async function doSetup(setup: any, config: any) {
]);
}

// Set the default behavior to be single project mode.
if (setup.config.emulators.singleProjectMode === undefined) {
setup.config.emulators.singleProjectMode = true;
}

if (selections.download) {
for (const selected of selections.emulators) {
if (isDownloadableEmulator(selected)) {
Expand Down

0 comments on commit 7d9ece6

Please sign in to comment.