UNPKG

firebase-tools

Version:
148 lines (147 loc) 7.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBackendConfigs = void 0; const path = require("path"); const backend_1 = require("../../apphosting/backend"); const apphosting_1 = require("../../gcp/apphosting"); const devConnect_1 = require("../../gcp/devConnect"); const projectUtils_1 = require("../../projectUtils"); const prompt_1 = require("../../prompt"); const utils_1 = require("../../utils"); async function default_1(context, options) { var _a; const projectId = (0, projectUtils_1.needProjectId)(options); await (0, apphosting_1.ensureApiEnabled)(options); await (0, backend_1.ensureRequiredApisEnabled)(projectId); try { await (0, backend_1.ensureAppHostingComputeServiceAccount)(projectId, "", true); } catch (err) { if (err.status === 400) { (0, utils_1.logLabeledWarning)("apphosting", "Your App Hosting compute service account is still being provisioned. Please try again in a few moments."); } throw err; } context.backendConfigs = new Map(); context.backendLocations = new Map(); context.backendStorageUris = new Map(); const configs = getBackendConfigs(options); const { backends } = await (0, apphosting_1.listBackends)(projectId, "-"); const foundBackends = []; const notFoundBackends = []; const ambiguousBackends = []; const skippedBackends = []; for (const cfg of configs) { const filteredBackends = backends.filter((backend) => (0, apphosting_1.parseBackendName)(backend.name).id === cfg.backendId); if (filteredBackends.length === 0) { notFoundBackends.push(cfg); } else if (filteredBackends.length === 1) { foundBackends.push(cfg); } else { ambiguousBackends.push(cfg); } } for (const cfg of ambiguousBackends) { const filteredBackends = backends.filter((backend) => (0, apphosting_1.parseBackendName)(backend.name).id === cfg.backendId); const locations = filteredBackends.map((b) => (0, apphosting_1.parseBackendName)(b.name).location); (0, utils_1.logLabeledWarning)("apphosting", `You have multiple backends with the same ${cfg.backendId} ID in regions: ${locations.join(", ")}. This is not allowed until we can support more locations. ` + "Please delete and recreate any backends that share an ID with another backend."); } if (foundBackends.length > 0) { (0, utils_1.logLabeledBullet)("apphosting", `Found backend(s) ${foundBackends.map((cfg) => cfg.backendId).join(", ")}`); } for (const cfg of foundBackends) { const filteredBackends = backends.filter((backend) => (0, apphosting_1.parseBackendName)(backend.name).id === cfg.backendId); if (cfg.alwaysDeployFromSource === false) { skippedBackends.push(cfg); continue; } const backend = filteredBackends[0]; const { location } = (0, apphosting_1.parseBackendName)(backend.name); if (cfg.alwaysDeployFromSource === undefined && ((_a = backend.codebase) === null || _a === void 0 ? void 0 : _a.repository)) { const { connectionName, id } = (0, devConnect_1.parseGitRepositoryLinkName)(backend.codebase.repository); const gitRepositoryLink = await (0, devConnect_1.getGitRepositoryLink)(projectId, location, connectionName, id); if (!options.force) { const confirmDeploy = await (0, prompt_1.confirm)({ default: true, message: `${cfg.backendId} is linked to the remote repository at ${gitRepositoryLink.cloneUri}. Are you sure you want to deploy your local source?`, }); cfg.alwaysDeployFromSource = confirmDeploy; const configPath = path.join(options.projectRoot || "", "firebase.json"); options.config.writeProjectFile(configPath, options.config.src); (0, utils_1.logLabeledBullet)("apphosting", `On future invocations of "firebase deploy", your local source will ${!confirmDeploy ? "not " : ""}be deployed to ${cfg.backendId}. You can edit this setting in your firebase.json at any time.`); if (!confirmDeploy) { skippedBackends.push(cfg); continue; } } } context.backendConfigs.set(cfg.backendId, cfg); context.backendLocations.set(cfg.backendId, location); } if (notFoundBackends.length > 0) { if (options.force) { (0, utils_1.logLabeledWarning)("apphosting", `Skipping deployments of backend(s) ${notFoundBackends.map((cfg) => cfg.backendId).join(", ")}; ` + "the backend(s) do not exist yet and we cannot create them for you because you must choose primary regions for each one. " + "Please run 'firebase deploy' without the --force flag, or 'firebase apphosting:backends:create' to create the backend, " + "then retry deployment."); return; } const confirmCreate = await (0, prompt_1.confirm)({ default: true, message: `Did not find backend(s) ${notFoundBackends.map((cfg) => cfg.backendId).join(", ")}. Do you want to create them (you'll have the option to select which to create in the next step)?`, }); if (confirmCreate) { const selected = await (0, prompt_1.checkbox)({ message: "Which backends do you want to create and deploy to?", choices: notFoundBackends.map((cfg) => cfg.backendId), }); const selectedBackends = selected.map((id) => notFoundBackends.find((backend) => backend.backendId === id)); for (const cfg of selectedBackends) { (0, utils_1.logLabeledBullet)("apphosting", `Creating a new backend ${cfg.backendId}...`); const { location } = await (0, backend_1.doSetupSourceDeploy)(projectId, cfg.backendId); context.backendConfigs.set(cfg.backendId, cfg); context.backendLocations.set(cfg.backendId, location); } } else { skippedBackends.push(...notFoundBackends); } } if (skippedBackends.length > 0) { (0, utils_1.logLabeledWarning)("apphosting", `Skipping deployment of backend(s) ${skippedBackends.map((cfg) => cfg.backendId).join(", ")}.`); } return; } exports.default = default_1; function getBackendConfigs(options) { if (!options.config.src.apphosting) { return []; } const backendConfigs = Array.isArray(options.config.src.apphosting) ? options.config.src.apphosting : [options.config.src.apphosting]; if (!options.only) { return backendConfigs; } const selectors = options.only.split(","); const backendIds = []; for (const selector of selectors) { if (selector === "apphosting") { return backendConfigs; } if (selector.startsWith("apphosting:")) { const backendId = selector.replace("apphosting:", ""); if (backendId.length > 0) { backendIds.push(backendId); } } } if (backendIds.length === 0) { return []; } return backendConfigs.filter((cfg) => backendIds.includes(cfg.backendId)); } exports.getBackendConfigs = getBackendConfigs;