Skip to content

Commit

Permalink
fix issues where project ID was missing in Hosting setup (#6528)
Browse files Browse the repository at this point in the history
* fix issues where project ID was missing in Hosting setup

fixes #6527

* formatting is SO HARD
  • Loading branch information
bkendall authored Nov 15, 2023
1 parent 3b345d5 commit 6e52869
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixes issue where initializing Hosting fails when selecting a project. (#6527)
2 changes: 1 addition & 1 deletion src/getDefaultHostingSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
export async function getDefaultHostingSite(options: { projectId?: string }): Promise<string> {
const projectId = needProjectId(options);
const project = await getFirebaseProject(projectId);
let site = project.resources?.hostingSite;
Expand Down
3 changes: 1 addition & 2 deletions src/hosting/interactive.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -17,7 +16,7 @@ const prompt =
export async function interactiveCreateHostingSite(
siteId: string,
appId: string,
options: Options
options: { projectId?: string; nonInteractive?: boolean }
): Promise<Site> {
const projectId = needProjectId(options);
const projectNumber = await needProjectNumber(options);
Expand Down
47 changes: 28 additions & 19 deletions src/init/features/hosting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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();
}
}
}

Expand Down

0 comments on commit 6e52869

Please sign in to comment.