diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29bb2d..03927817da2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1 @@ +- Fixes issue where initializing Hosting fails when selecting a project. (#6527) diff --git a/src/getDefaultHostingSite.ts b/src/getDefaultHostingSite.ts index 5ad09d973be..795e4b043d6 100644 --- a/src/getDefaultHostingSite.ts +++ b/src/getDefaultHostingSite.ts @@ -14,7 +14,7 @@ export const errNoDefaultSite = new FirebaseError( * @param options The command-line options object * @return The hosting site ID */ -export async function getDefaultHostingSite(options: any): Promise { +export async function getDefaultHostingSite(options: { projectId?: string }): Promise { const projectId = needProjectId(options); const project = await getFirebaseProject(projectId); let site = project.resources?.hostingSite; diff --git a/src/hosting/interactive.ts b/src/hosting/interactive.ts index f48ff3c8ef2..6611df7fc22 100644 --- a/src/hosting/interactive.ts +++ b/src/hosting/interactive.ts @@ -1,7 +1,6 @@ import { FirebaseError } from "../error"; import { logWarning } from "../utils"; import { needProjectId, needProjectNumber } from "../projectUtils"; -import { Options } from "../options"; import { promptOnce } from "../prompt"; import { Site, createSite } from "./api"; @@ -17,7 +16,7 @@ const prompt = export async function interactiveCreateHostingSite( siteId: string, appId: string, - options: Options + options: { projectId?: string; nonInteractive?: boolean } ): Promise { const projectId = needProjectId(options); const projectNumber = await needProjectNumber(options); diff --git a/src/init/features/hosting/index.ts b/src/init/features/hosting/index.ts index f0dc6500f7b..3b6a298d0dd 100644 --- a/src/init/features/hosting/index.ts +++ b/src/init/features/hosting/index.ts @@ -27,31 +27,40 @@ const DEFAULT_IGNORES = ["firebase.json", "**/.*", "**/node_modules/**"]; /** * Does the setup steps for Firebase Hosting. + * WARNING: #6527 - `options` may not have all the things you think it does. */ export async function doSetup(setup: any, config: any, options: Options): Promise { setup.hosting = {}; - let hasHostingSite = true; - try { - await getDefaultHostingSite(options); - } catch (err: unknown) { - if (err !== errNoDefaultSite) { - throw err; + // There's a path where we can set up Hosting without a project, so if + // if setup.projectId is empty, we don't do any checking for a Hosting site. + if (setup.projectId) { + let hasHostingSite = true; + try { + await getDefaultHostingSite({ projectId: setup.projectId }); + } catch (err: unknown) { + if (err !== errNoDefaultSite) { + throw err; + } + hasHostingSite = false; } - hasHostingSite = false; - } - if (!hasHostingSite) { - const confirmCreate = await promptOnce({ - type: "confirm", - message: "A Firebase Hosting site is required to deploy. Would you like to create one now?", - default: true, - }); - if (confirmCreate) { - const newSite = await interactiveCreateHostingSite("", "", options); - logger.info(); - logSuccess(`Firebase Hosting site ${last(newSite.name.split("/"))} created!`); - logger.info(); + if (!hasHostingSite) { + const confirmCreate = await promptOnce({ + type: "confirm", + message: "A Firebase Hosting site is required to deploy. Would you like to create one now?", + default: true, + }); + if (confirmCreate) { + const createOptions = { + projectId: setup.projectId, + nonInteractive: options.nonInteractive, + }; + const newSite = await interactiveCreateHostingSite("", "", createOptions); + logger.info(); + logSuccess(`Firebase Hosting site ${last(newSite.name.split("/"))} created!`); + logger.info(); + } } }