Skip to content

Commit 1b729a5

Browse files
committed
Added option to command
1 parent 6366115 commit 1b729a5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- Added default value for `emulators.dataconnect.dataDir` to `init dataconnect`.
22
- Fixed an issue where `firebase` would error out instead of displaying help text.
3+
- Added `--output` option to `firestore:indexes` which writes indexes to a file.

src/commands/firestore-indexes-list.ts

+33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as clc from "colorette";
2+
import * as fs from "fs";
23

34
import { Command } from "../command";
45
import * as fsi from "../firestore/api";
@@ -8,6 +9,7 @@ import { Emulators } from "../emulator/types";
89
import { warnEmulatorNotSupported } from "../emulator/commandUtils";
910
import { FirestoreOptions } from "../firestore/options";
1011
import { PrettyPrint } from "../firestore/pretty-print";
12+
import * as utils from "../utils";
1113

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

54+
const fileOut = !!options.output;
55+
if (fileOut) {
56+
const shouldUseDefaultFilename = options.output === true || options.output === "";
57+
58+
let filename = undefined;
59+
if (shouldUseDefaultFilename) {
60+
const fsConfig = options.config.src.firestore;
61+
if (fsConfig !== undefined) {
62+
// Check if single db
63+
if (!Array.isArray(fsConfig)) {
64+
filename = fsConfig.indexes;
65+
} else {
66+
const databaseId = options.database || `(default)`;
67+
filename = fsConfig.find((db) => db.database === databaseId)?.indexes;
68+
}
69+
} else {
70+
logger.debug("Possibly invalid database config: ", JSON.stringify(fsConfig));
71+
}
72+
} else {
73+
filename = options.output;
74+
}
75+
76+
utils.assertIsString(filename);
77+
const indexTemplate = JSON.stringify(indexSpec, undefined, 2);
78+
fs.writeFileSync(filename, indexTemplate);
79+
}
80+
4881
return indexSpec;
4982
});

0 commit comments

Comments
 (0)