Skip to content

Commit

Permalink
feat: enhance index reporting with optional JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvera committed Dec 12, 2024
1 parent da7b423 commit d04a6d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
25 changes: 17 additions & 8 deletions src/getOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface Options {
help: boolean
overwrite: boolean
check: boolean
print: boolean
}

type OptionsInfo = Record<keyof Options, string>
Expand All @@ -17,14 +18,14 @@ const optionsInfo: OptionsInfo = {
projectId: 'The Firebase project ID used with the emulator',
overwrite: 'Overwrite firestore.indexes.json with the new indexes',
check: 'Check if firestore.indexes.json is up to date with the new indexes',
print: 'Print the current emulator index content',
}

const printUsage = () => {
printHeader()

console.log('Usage:')
// One of --overwrite or --check is required
console.log(' fig --projectId <projectId> [--overwrite | --check]')
console.log(' fig --projectId <projectId> [--overwrite | --check | --print]')
console.log()
console.log('Options:')

Expand Down Expand Up @@ -59,6 +60,10 @@ export const getOptions = (): Omit<Options, 'help'> => {
type: 'boolean',
default: false,
},
print: {
type: 'boolean',
default: false,
},
},
})
} catch (error) {
Expand All @@ -68,13 +73,14 @@ export const getOptions = (): Omit<Options, 'help'> => {
process.exit(1)
}

const { projectId, help, overwrite, check } = args.values
const { projectId, help, overwrite, check, print } = args.values

if (
help ||
typeof projectId === 'undefined' ||
typeof overwrite === 'undefined' ||
typeof check === 'undefined'
typeof check === 'undefined' ||
typeof print === 'undefined'
) {
console.log()
console.log(chalk.bgYellow('WARNING: Missing required options'))
Expand All @@ -83,15 +89,17 @@ export const getOptions = (): Omit<Options, 'help'> => {
process.exit(1)
}

if (overwrite && check) {
logError('Cannot use both --overwrite and --check options')
const optionCount = [overwrite, check, print].filter(Boolean).length

if (optionCount > 1) {
logError('Can only use one of --overwrite, --check, or --print options')

printUsage()
process.exit(1)
}

if (!overwrite && !check) {
logError('One of --overwrite or --check is required')
if (optionCount === 0) {
logError('One of --overwrite, --check, or --print is required')

printUsage()
process.exit(1)
Expand All @@ -101,5 +109,6 @@ export const getOptions = (): Omit<Options, 'help'> => {
projectId,
overwrite,
check,
print,
}
}
16 changes: 11 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import { getIndexFromReport } from './getIndexFromReport.js'
import { getOptions } from './getOptions.js'
import { printHeader } from './printHeader.js'

const { projectId, check, overwrite } = getOptions()
const { projectId, check, overwrite, print } = getOptions()

const { path: configPath, config } = await getFirebaseConfig()

// Get current indexes from firestore.indexes.json in the project
const { path: indexesPath, indexes: currentIndexes } =
await getFirestoreIndexes(config, configPath)

const indexReport = await getEmulatorIndexReport(
projectId,
config.emulators?.firestore?.port ?? 8080,
Expand All @@ -26,6 +22,16 @@ const newIndexes = getIndexFromReport(indexReport.reports)

printHeader()

if (print) {
console.log(JSON.stringify(newIndexes, null, 2))
console.log()
process.exit(0)
}

// Get current indexes from firestore.indexes.json in the project
const { path: indexesPath, indexes: currentIndexes } =
await getFirestoreIndexes(config, configPath)

if (check) {
checkIndexes(currentIndexes, newIndexes)
}
Expand Down

0 comments on commit d04a6d9

Please sign in to comment.