diff --git a/firestore-bigquery-export/scripts/gen-schema-view/src/index.ts b/firestore-bigquery-export/scripts/gen-schema-view/src/index.ts index 81c382be1..980e1f45b 100644 --- a/firestore-bigquery-export/scripts/gen-schema-view/src/index.ts +++ b/firestore-bigquery-export/scripts/gen-schema-view/src/index.ts @@ -19,33 +19,35 @@ import firebase = require("firebase-admin"); import { FirestoreBigQuerySchemaViewFactory, FirestoreSchema } from "./schema"; import { readSchemas } from "./schema-loader-utils"; -import { parseConfig } from "./config"; +import { parseConfig, CliConfig } from "./config"; import { generateSchemaFilesWithGemini } from "./schema/genkit"; -export async function run(): Promise { - const config = await parseConfig(); +export async function run(config?: CliConfig): Promise { + const cliConfig = config ?? (await parseConfig()); - process.env.PROJECT_ID = config.projectId; - process.env.GOOGLE_CLOUD_PROJECT = config.bigQueryProjectId; + process.env.PROJECT_ID = cliConfig.projectId; + process.env.GOOGLE_CLOUD_PROJECT = cliConfig.bigQueryProjectId; if (!firebase.apps.length) { firebase.initializeApp({ credential: firebase.credential.applicationDefault(), - databaseURL: `https://${config.projectId}.firebaseio.com`, + databaseURL: `https://${cliConfig.projectId}.firebaseio.com`, }); } const viewFactory = new FirestoreBigQuerySchemaViewFactory( - config.bigQueryProjectId + cliConfig.bigQueryProjectId ); // Generate schema files using Gemini if enabled // Otherwise, read schema files from the filesystem - let schemas = config.schemas; - if (config.useGemini) { + let schemas = cliConfig.schemas; + if (cliConfig.useGemini) { try { - await generateSchemaFilesWithGemini(config); - schemas = readSchemas([`./schemas/${config.geminiSchemaFileName}.json`]); + await generateSchemaFilesWithGemini(cliConfig); + schemas = readSchemas([ + `./schemas/${cliConfig.geminiSchemaFileName}.json`, + ]); console.log("Schema file generated successfully."); } catch (error) { @@ -53,7 +55,7 @@ export async function run(): Promise { throw error; } } else { - if (Object.keys(config.schemas).length === 0) { + if (Object.keys(cliConfig.schemas).length === 0) { console.log(`No schema files found!`); } } @@ -61,8 +63,8 @@ export async function run(): Promise { // Initialize schema views for (const name in schemas) { await viewFactory.initializeSchemaViewResources( - config.datasetId, - config.tableNamePrefix, + cliConfig.datasetId, + cliConfig.tableNamePrefix, name, schemas[name] ); @@ -72,14 +74,34 @@ export async function run(): Promise { } if (process.env.NODE_ENV !== "test") { - run() - .then((result) => { + let config: CliConfig | null = null; + + parseConfig() + .then((parsedConfig) => { + config = parsedConfig; + return run(parsedConfig); + }) + .then(() => { console.log("done."); - process.exit(); + process.exit(0); }) .catch((error) => { + if (config) { + const errorMessage = error.message || error.errors?.[0]?.message; + if ( + errorMessage?.includes("ProjectId must be non-empty") || + errorMessage?.includes("Cannot parse as CloudRegion") + ) { + const improvedMessage = `The BigQuery Project ID '${config.bigQueryProjectId}' is not valid. Please verify that the project ID is correct and that you have access to it.`; + error.message = improvedMessage; + if (error.errors?.[0]) { + error.errors[0].message = improvedMessage; + } + } + } + console.log(JSON.stringify(error)); console.error(error.message); - process.exit(); + process.exit(1); }); }