UNPKG

firebase-tools

Version:
74 lines (73 loc) • 2.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAllServiceConfigs = getAllServiceConfigs; exports.getServiceConfigs = getServiceConfigs; exports.findServices = findServices; exports.fullServiceName = fullServiceName; exports.getExistingService = getExistingService; exports.mainContainer = mainContainer; const error_1 = require("../../error"); const runv2 = require("../../gcp/runv2"); const utils_1 = require("../../utils"); function getAllServiceConfigs(options) { const configs = [options.config?.src?.run ?? []].flat(); const names = new Set(); for (const config of configs) { if (!config.region) { throw new error_1.FirebaseError(`Cloud Run service ${config.serviceId} is missing a region in firebase.json.`); } const name = fullServiceName(config); if (names.has(name)) { throw new error_1.FirebaseError(`Cloud Run service ${name} is listed more than once in firebase.json.`); } names.add(name); } return configs; } function getServiceConfigs(options) { const configs = getAllServiceConfigs(options); const filters = options.only ? options.only.split(",") : ["run"]; if (filters.includes("run")) { return configs; } const selected = filters .filter((f) => f.startsWith("run:")) .flatMap((filter) => { const name = filter.slice("run:".length); const matches = findServices(configs, name); if (!matches.length) { throw new error_1.FirebaseError(`Cloud Run service ${name} not detected in firebase.json.`); } if (matches.length > 1) { (0, utils_1.logLabeledBullet)("run", `${filter} matches ${matches.length} services in firebase.json: ` + `${matches.map(fullServiceName).join(", ")}. Deploying all of them. ` + `To deploy just one, use --only ${filter}:<region>.`); } return matches; }); return configs.filter((c) => selected.includes(c)); } function findServices(configs, name) { const [serviceId, region, ...rest] = name.split(":"); if (!serviceId || region === "" || rest.length) { throw new error_1.FirebaseError(`Invalid Cloud Run service "${name}". Use <serviceId> or <serviceId>:<region>.`); } return configs.filter((c) => c.serviceId === serviceId && (!region || c.region === region)); } function fullServiceName(config) { return `${config.serviceId}:${config.region}`; } async function getExistingService(projectId, region, serviceId) { try { return await runv2.getService(projectId, region, serviceId); } catch (err) { if ((0, error_1.getErrStatus)(err) === 404) { return undefined; } throw err; } } function mainContainer(template) { return template?.containers?.[0]; }