Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added --output option to firestore:indexes command #8010

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- Added default value for `emulators.dataconnect.dataDir` to `init dataconnect`.
- Fixed an issue where `firebase` would error out instead of displaying help text.
- Added `--output` option to `firestore:indexes` which writes indexes to a file.
33 changes: 33 additions & 0 deletions src/commands/firestore-indexes-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as clc from "colorette";
import * as fs from "fs";

import { Command } from "../command";
import * as fsi from "../firestore/api";
Expand All @@ -8,6 +9,7 @@ import { Emulators } from "../emulator/types";
import { warnEmulatorNotSupported } from "../emulator/commandUtils";
import { FirestoreOptions } from "../firestore/options";
import { PrettyPrint } from "../firestore/pretty-print";
import * as utils from "../utils";

export const command = new Command("firestore:indexes")
.description("List indexes in your project's Cloud Firestore database.")
Expand All @@ -20,6 +22,10 @@ export const command = new Command("firestore:indexes")
"--database <databaseId>",
"Database ID of the firestore database from which to list indexes. (default) if none provided.",
)
.option(
"-o, --output [filename]",
"write indexes output to a file. if omitted, will use the path to specified database indexes file. (default) if none provided",
)
.before(requirePermissions, ["datastore.indexes.list"])
.before(warnEmulatorNotSupported, Emulators.FIRESTORE)
.action(async (options: FirestoreOptions) => {
Expand All @@ -45,5 +51,32 @@ export const command = new Command("firestore:indexes")
logger.info(JSON.stringify(indexSpec, undefined, 2));
}

const fileOut = !!options.output;
if (fileOut) {
const shouldUseDefaultFilename = options.output === true || options.output === "";

let filename = undefined;
if (shouldUseDefaultFilename) {
const fsConfig = options.config.src.firestore;
if (fsConfig !== undefined) {
// Check if single db
if (!Array.isArray(fsConfig)) {
filename = fsConfig.indexes;
} else {
const databaseId = options.database || `(default)`;
filename = fsConfig.find((db) => db.database === databaseId)?.indexes;
}
} else {
logger.debug("Possibly invalid database config: ", JSON.stringify(fsConfig));
}
} else {
filename = options.output;
}

utils.assertIsString(filename);
const indexTemplate = JSON.stringify(indexSpec, undefined, 2);
fs.writeFileSync(filename, indexTemplate);
}

return indexSpec;
});
Loading