UNPKG

firebase-tools

Version:
71 lines (70 loc) 3.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.localBuild = localBuild; const build_1 = require("@apphosting/build"); const secrets_1 = require("./secrets"); const prompt_1 = require("../prompt"); const error_1 = require("../error"); async function localBuild(projectId, projectRoot, framework, env = {}, options) { const hasBuildAvailableSecrets = Object.values(env).some((v) => v.secret && (!v.availability || v.availability.includes("BUILD"))); if (hasBuildAvailableSecrets && !options?.allowLocalBuildSecrets) { if (options?.nonInteractive) { throw new error_1.FirebaseError("Using build-available secrets during a local build in non-interactive mode requires the --allow-local-build-secrets flag."); } if (!(await (0, prompt_1.confirm)({ message: "Your build includes secrets that are available to the build environment. Using secrets in local builds may leave sensitive values in local artifacts/temporary files. Do you want to continue?", default: false, }))) { throw new error_1.FirebaseError("Cancelled local build due to BUILD-available secrets."); } } const originalEnv = { ...process.env }; const addedEnv = await toProcessEnv(projectId, env); for (const [key, value] of Object.entries(addedEnv)) { process.env[key] = value; } let apphostingBuildOutput; try { apphostingBuildOutput = await (0, build_1.localBuild)(projectRoot, framework); } finally { for (const key in process.env) { if (!(key in originalEnv)) { delete process.env[key]; } } for (const [key, value] of Object.entries(originalEnv)) { process.env[key] = value; } } const annotations = Object.fromEntries(Object.entries(apphostingBuildOutput.metadata).map(([key, value]) => [key, String(value)])); const discoveredEnv = apphostingBuildOutput.runConfig.environmentVariables?.map(({ variable, value, availability }) => ({ variable, value, availability, })); return { outputFiles: apphostingBuildOutput.outputFiles?.serverApp.include ?? [], annotations, buildConfig: { runCommand: apphostingBuildOutput.runConfig.runCommand, env: discoveredEnv ?? [], }, }; } async function toProcessEnv(projectId, env) { const entries = await Promise.all(Object.entries(env).map(async ([key, value]) => { if (value.availability && !value.availability.includes("BUILD")) { return null; } if (value.secret) { const resolvedValue = await (0, secrets_1.loadSecret)(projectId, value.secret); return [key, resolvedValue]; } else { return [key, value.value || ""]; } })); const filteredEntries = entries.filter((entry) => entry !== null); return Object.fromEntries(filteredEntries); }