1
1
import * as clc from "colorette" ;
2
+ import * as fs from "fs" ;
2
3
3
4
import { Command } from "../command" ;
4
5
import * as fsi from "../firestore/api" ;
@@ -8,6 +9,7 @@ import { Emulators } from "../emulator/types";
8
9
import { warnEmulatorNotSupported } from "../emulator/commandUtils" ;
9
10
import { FirestoreOptions } from "../firestore/options" ;
10
11
import { PrettyPrint } from "../firestore/pretty-print" ;
12
+ import * as utils from "../utils" ;
11
13
12
14
export const command = new Command ( "firestore:indexes" )
13
15
. description ( "List indexes in your project's Cloud Firestore database." )
@@ -20,6 +22,10 @@ export const command = new Command("firestore:indexes")
20
22
"--database <databaseId>" ,
21
23
"Database ID of the firestore database from which to list indexes. (default) if none provided." ,
22
24
)
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
+ )
23
29
. before ( requirePermissions , [ "datastore.indexes.list" ] )
24
30
. before ( warnEmulatorNotSupported , Emulators . FIRESTORE )
25
31
. action ( async ( options : FirestoreOptions ) => {
@@ -45,5 +51,32 @@ export const command = new Command("firestore:indexes")
45
51
logger . info ( JSON . stringify ( indexSpec , undefined , 2 ) ) ;
46
52
}
47
53
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
+
48
81
return indexSpec ;
49
82
} ) ;
0 commit comments