UNPKG

firebase-tools

Version:
195 lines (194 loc) • 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VALID_FORM_FACTORS = exports.VALID_SIGNALS = exports.VALID_ERROR_TYPES = void 0; exports.requireAppId = requireAppId; exports.parsePositiveIntOption = parsePositiveIntOption; exports.writeOutputFileIfRequested = writeOutputFileIfRequested; exports.withEventFilterOptions = withEventFilterOptions; exports.parseEventFilters = parseEventFilters; const fs = require("fs-extra"); const error_1 = require("../error"); const logger_1 = require("../logger"); const prompt = require("../prompt"); const apps_1 = require("../management/apps"); const utils_1 = require("./utils"); const filters_1 = require("./filters"); exports.VALID_ERROR_TYPES = ["FATAL", "NON_FATAL", "ANR"]; exports.VALID_SIGNALS = [ "SIGNAL_EARLY", "SIGNAL_FRESH", "SIGNAL_REGRESSED", "SIGNAL_REPETITIVE", ]; exports.VALID_FORM_FACTORS = ["PHONE", "TABLET", "DESKTOP", "TV", "WATCH"]; function requireAppId(options) { const appId = typeof options.app === "string" ? options.app.trim() : ""; if (!appId) { throw new error_1.FirebaseError("Must specify '--app <appId>'. Run 'firebase apps:list' to find your Firebase App ID.", { exit: 1 }); } return appId; } function parsePositiveIntOption(value, flagName, bounds) { const { defaultValue, max } = bounds; if (value === undefined || value === "") { return defaultValue; } const parsed = typeof value === "number" ? value : Number(String(value).trim()); if (!Number.isInteger(parsed) || parsed < 1) { throw new error_1.FirebaseError(`'${flagName}' must be a positive integer.`, { exit: 1 }); } if (max !== undefined && parsed > max) { throw new error_1.FirebaseError(`'${flagName}' must be between 1 and ${max}.`, { exit: 1 }); } return parsed; } async function writeOutputFileIfRequested(data, options) { if (!options.output) { return false; } if (typeof options.output !== "string" || !options.output.trim()) { throw new error_1.FirebaseError("Must specify a file path for '-o, --output <path>'.", { exit: 1 }); } const outputPath = options.output.trim(); if (fs.existsSync(outputPath)) { const shouldOverwrite = await prompt.confirm({ message: `File ${outputPath} already exists. Overwrite?`, force: options.force, nonInteractive: options.nonInteractive, }); if (!shouldOverwrite) { logger_1.logger.info("Aborted writing output file."); return true; } } try { fs.outputFileSync(outputPath, `${JSON.stringify(data, null, 2)}\n`, "utf8"); } catch (err) { throw new error_1.FirebaseError(`Failed to write output to ${outputPath}: ${(0, error_1.getErrMsg)(err)}`, { exit: 1, original: (0, error_1.getError)(err), }); } logger_1.logger.info(`Wrote output to ${outputPath}`); return true; } function splitCommaSeparatedList(raw) { if (!raw) { return undefined; } const items = String(raw) .split(",") .map((item) => item.trim()) .filter(Boolean); return items.length > 0 ? items : undefined; } function validateEnumList(values, validValues, flagName) { if (!values) { return undefined; } for (const val of values) { if (!validValues.includes(val)) { throw new error_1.FirebaseError(`Invalid value '${val}' for '${flagName}'. Valid values are: ${validValues.join(", ")}.`, { exit: 1 }); } } return values; } const ISO_DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/; const ISO_DATE_TIME_WITH_TZ = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:\d{2})$/i; function parseTimeOption(raw, flagName) { const value = raw?.trim(); if (!value) { return undefined; } const isDateOnly = ISO_DATE_ONLY.test(value); const date = isDateOnly || ISO_DATE_TIME_WITH_TZ.test(value) ? new Date(isDateOnly ? `${value}T00:00:00Z` : value) : undefined; if (!date || isNaN(date.getTime())) { throw new error_1.FirebaseError(`Invalid ${flagName} '${value}'. Use an ISO 8601 date or timestamp, ` + `e.g. "2026-11-01" or "2026-11-01T08:00:00Z".`, { exit: 1 }); } return date.toISOString(); } function withEventFilterOptions(command) { return command .option("--issue-id <id>", "filter by Crashlytics issue ID") .option("--variant-id <id>", "filter by issue variant ID (mobile only)") .option("--versions <list>", 'comma-separated version display names (mobile: "2.4.0 (240)", web: "2.4.0")') .option("--error-types <list>", "comma-separated error types: FATAL, NON_FATAL, ANR") .option("--signals <list>", "comma-separated issue signals: SIGNAL_EARLY, SIGNAL_FRESH, SIGNAL_REGRESSED, SIGNAL_REPETITIVE (mobile only)") .option("--os-names <list>", 'comma-separated operating system display names (e.g. "Android (14)")') .option("--device-names <list>", 'comma-separated device display names (e.g. "Google (Pixel 8)", mobile only)') .option("--device-form-factors <list>", "comma-separated device form factors: PHONE, TABLET, DESKTOP, TV, WATCH (mobile only)") .option("--browser-names <list>", 'comma-separated browser display names (e.g. "Chrome (123)", web only)') .option("--start-time <iso>", 'start of the time interval as an ISO 8601 date or timestamp (e.g. "2026-11-01" or "2026-11-01T08:00:00Z"); within the last 90 days, default: 7 days ago') .option("--end-time <iso>", "end of the time interval as an ISO 8601 date or timestamp; requires --start-time (default: now)"); } const MOBILE_ONLY_FLAGS = [ ["variantId", "--variant-id"], ["signals", "--signals"], ["deviceNames", "--device-names"], ["deviceFormFactors", "--device-form-factors"], ]; const WEB_ONLY_FLAGS = [ ["browserNames", "--browser-names"], ]; function validatePlatformSpecificFlags(options, appId) { const platform = (0, utils_1.parseAppPlatform)(appId); if (!platform) { return; } const isWeb = platform === apps_1.AppPlatform.WEB; const used = (isWeb ? MOBILE_ONLY_FLAGS : WEB_ONLY_FLAGS) .filter(([key]) => { const value = options[key]; return typeof value === "string" && value.trim() !== ""; }) .map(([, flag]) => flag); if (used.length > 0) { throw new error_1.FirebaseError(`${used.join(", ")} ${used.length === 1 ? "is" : "are"} only supported for ` + `${isWeb ? "Android and Apple" : "web"} apps, but ${appId} is ` + `${isWeb ? "a web" : "an Android or Apple"} app.`, { exit: 1 }); } } function parseEventFilters(options, appId) { validatePlatformSpecificFlags(options, appId); const intervalStartTime = parseTimeOption(options.startTime, "--start-time"); const intervalEndTime = parseTimeOption(options.endTime, "--end-time"); if (intervalStartTime && intervalEndTime && intervalStartTime >= intervalEndTime) { throw new error_1.FirebaseError("'--start-time' must be before '--end-time'.", { exit: 1 }); } if (intervalEndTime && !intervalStartTime) { throw new error_1.FirebaseError("'--end-time' requires '--start-time'.", { exit: 1 }); } const filter = { intervalStartTime, intervalEndTime, versionDisplayNames: splitCommaSeparatedList(options.versions), issueId: options.issueId?.trim() || undefined, issueVariantId: options.variantId?.trim() || undefined, issueErrorTypes: validateEnumList(splitCommaSeparatedList(options.errorTypes), exports.VALID_ERROR_TYPES, "--error-types"), issueSignals: validateEnumList(splitCommaSeparatedList(options.signals), exports.VALID_SIGNALS, "--signals"), operatingSystemDisplayNames: splitCommaSeparatedList(options.osNames), deviceDisplayNames: splitCommaSeparatedList(options.deviceNames), deviceFormFactors: validateEnumList(splitCommaSeparatedList(options.deviceFormFactors), exports.VALID_FORM_FACTORS, "--device-form-factors"), browserFilterDisplayNames: splitCommaSeparatedList(options.browserNames), }; try { return (0, filters_1.validateEventFilters)(filter, appId); } catch (err) { if (err instanceof error_1.FirebaseError) { if (err.message.includes("topVersions report")) { throw new error_1.FirebaseError(`--versions must match pattern 'version (build)' (e.g. "2.4.0 (240)"). Run 'firebase crashlytics:reports:get --app ${appId} topVersions' to list valid version display names.`, { exit: 1 }); } const message = err.message .replace("intervalStartTime must be less than 90 days in the past", "'--start-time' must be within the last 90 days") .replace(/^deviceDisplayNames/, "--device-names") .replace(/^operatingSystemDisplayNames/, "--os-names"); throw new error_1.FirebaseError(message, { exit: 1 }); } throw err; } }