firebase-tools
Version:
Command-Line Interface for Firebase
95 lines (94 loc) • 4.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.command = void 0;
exports.loadCodebaseBuild = loadCodebaseBuild;
const command_1 = require("../command");
const logger_1 = require("../logger");
const prepare_1 = require("../deploy/functions/prepare");
const projectConfig_1 = require("../functions/projectConfig");
const adminSdkConfig_1 = require("../emulator/adminSdkConfig");
const projectUtils_1 = require("../projectUtils");
const error_1 = require("../error");
const ensureApiEnabled = require("../ensureApiEnabled");
const api_1 = require("../api");
const prepareFunctionsUpload_1 = require("../deploy/functions/prepareFunctionsUpload");
const self = require("./functions-lifecycle-list");
async function loadCodebaseBuild(codebase, options) {
const projectId = (0, projectUtils_1.needProjectId)(options);
if (!options.config) {
throw new error_1.FirebaseError("Not in a Firebase project directory (firebase.json not found).");
}
const fnConfig = (0, projectConfig_1.normalizeAndValidate)(options.config.src.functions);
const hasCodebase = fnConfig.some((c) => c.codebase === codebase);
if (!hasCodebase) {
throw new error_1.FirebaseError(`Codebase "${codebase}" is not defined in firebase.json.`);
}
const firebaseConfig = await (0, adminSdkConfig_1.getProjectAdminSdkConfigOrCached)(projectId);
if (!firebaseConfig) {
throw new error_1.FirebaseError("Admin SDK config unexpectedly undefined - have you run firebase init?");
}
let runtimeConfig = { firebase: firebaseConfig };
if (fnConfig.some(projectConfig_1.shouldUseRuntimeConfig)) {
try {
const runtimeConfigApiEnabled = await ensureApiEnabled.check(projectId, (0, api_1.runtimeconfigOrigin)(), "runtimeconfig", true);
if (runtimeConfigApiEnabled) {
runtimeConfig = { ...runtimeConfig, ...(await (0, prepareFunctionsUpload_1.getFunctionsConfig)(projectId)) };
}
}
catch (err) {
logger_1.logger.debug("Could not check Runtime Config API status, assuming disabled:", err);
}
}
const wantBuilds = await (0, prepare_1.loadCodebases)(fnConfig, options, firebaseConfig, runtimeConfig, undefined);
const codebaseBuild = wantBuilds[codebase];
if (!codebaseBuild) {
throw new error_1.FirebaseError(`Failed to load build for codebase "${codebase}".`);
}
return codebaseBuild;
}
exports.command = new command_1.Command("functions:lifecycle:list <codebase>")
.description("list all the lifecycle hooks defined in a codebase")
.action(async (codebase, options) => {
const codebaseBuild = await self.loadCodebaseBuild(codebase, options);
const hooks = codebaseBuild.lifecycleHooks || {};
if (Object.keys(hooks).length === 0) {
logger_1.logger.info(`No lifecycle hooks configured for codebase "${codebase}".`);
return hooks;
}
for (const [event, hook] of Object.entries(hooks)) {
logger_1.logger.info(`\nEvent: ${event}`);
if ("task" in hook) {
logger_1.logger.info(` Action: Task`);
logger_1.logger.info(` Target Function: ${hook.task.function}`);
if (hook.task.body) {
logger_1.logger.info(` Body: ${JSON.stringify(hook.task.body, null, 2).replace(/\n/g, "\n ")}`);
}
}
else if ("call" in hook) {
logger_1.logger.info(` Action: Call`);
logger_1.logger.info(` Target Function: ${hook.call.function}`);
if (hook.call.params) {
logger_1.logger.info(` Params: ${JSON.stringify(hook.call.params, null, 2).replace(/\n/g, "\n ")}`);
}
}
else if ("http" in hook) {
logger_1.logger.info(` Action: HTTP`);
if (hook.http.function) {
logger_1.logger.info(` Target Function: ${hook.http.function}`);
}
if (hook.http.url) {
logger_1.logger.info(` URL: ${hook.http.url}`);
}
if (hook.http.method) {
logger_1.logger.info(` Method: ${hook.http.method}`);
}
if (hook.http.headers) {
logger_1.logger.info(` Headers: ${JSON.stringify(hook.http.headers, null, 2).replace(/\n/g, "\n ")}`);
}
if (hook.http.body) {
logger_1.logger.info(` Body: ${JSON.stringify(hook.http.body, null, 2).replace(/\n/g, "\n ")}`);
}
}
}
return hooks;
});