firebase-tools 15.30.0 → 15.30.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,6 +24,7 @@ exports.validateAndResolveDirectoryKit = validateAndResolveDirectoryKit;
24
24
  exports.buildAndInstallDirectoryKit = buildAndInstallDirectoryKit;
25
25
  exports.findKitConfig = findKitConfig;
26
26
  exports.addInstanceToKitConfig = addInstanceToKitConfig;
27
+ exports.revertFunctionsConfig = revertFunctionsConfig;
27
28
  exports.scaffoldKit = scaffoldKit;
28
29
  exports.addInstanceToKit = addInstanceToKit;
29
30
  exports.discoverKitBuild = discoverKitBuild;
@@ -56,9 +57,10 @@ const iam = require("../../gcp/iam");
56
57
  const functionsEnv = require("../env");
57
58
  const functionsConfig = require("../../functionsConfig");
58
59
  const prepare_1 = require("../../deploy/functions/prepare");
59
- const utils_2 = require("../../utils");
60
+ const functional_1 = require("../../functional");
60
61
  const env_1 = require("../env");
61
62
  const env_2 = require("./env");
63
+ const fsutils_1 = require("../../fsutils");
62
64
  exports.TEMPLATES = {
63
65
  installation: "init/functions/typescript/index-kit.ts",
64
66
  migration: "init/functions/typescript/index-kit-migration.ts",
@@ -235,7 +237,8 @@ async function promptKitId(packageName, existingKitIds, nonInteractive, customKi
235
237
  }
236
238
  return kitId;
237
239
  }
238
- async function promptSecurityConfirmation(rawPkgName, packageName, nonInteractive) {
240
+ async function promptSecurityConfirmation(options) {
241
+ const { rawPkgName, packageName, nonInteractive, force } = options;
239
242
  const isThirdParty = isThirdPartyPackage(packageName);
240
243
  if (isThirdParty) {
241
244
  (0, utils_1.logLabeledWarning)("functions", `Package ${clc.bold(packageName)} is a third-party kit (outside the @firebase-function-kits scope).`);
@@ -259,6 +262,7 @@ async function promptSecurityConfirmation(rawPkgName, packageName, nonInteractiv
259
262
  message: confirmMessage,
260
263
  default: false,
261
264
  nonInteractive,
265
+ force,
262
266
  });
263
267
  if (!confirmInstallation) {
264
268
  throw new error_1.FirebaseError("Installation cancelled.");
@@ -538,6 +542,22 @@ function addInstanceToKitConfig(config, kitId, instanceId, configDirPath) {
538
542
  target.instances[instanceId] = configDirPath;
539
543
  config.writeProjectFile("firebase.json", config.src);
540
544
  }
545
+ function revertFunctionsConfig(options) {
546
+ try {
547
+ if (!(0, utils_1.deepEqual)(options.config.src.functions, options.originalFunctions)) {
548
+ if (options.originalFunctions === undefined) {
549
+ delete options.config.src.functions;
550
+ }
551
+ else {
552
+ options.config.src.functions = options.originalFunctions;
553
+ }
554
+ options.config.writeProjectFile("firebase.json", options.config.src);
555
+ }
556
+ }
557
+ catch (err) {
558
+ logger_1.logger.debug(`Failed to revert firebase.json: ${(0, error_1.getErrMsg)(err)}`);
559
+ }
560
+ }
541
561
  async function scaffoldKit(options) {
542
562
  const paths = await scaffoldKitFiles(options.config, options.kitId, options.instanceId, options.packageName, options.templateType);
543
563
  if (options.seedEnv?.envs && Object.keys(options.seedEnv.envs).length > 0) {
@@ -589,7 +609,8 @@ async function discoverKitBuild(options, absSourcePath) {
589
609
  runtime: supported.latest("nodejs"),
590
610
  };
591
611
  const runtimeDelegate = await runtimes.getRuntimeDelegate(delegateContext);
592
- return runtimeDelegate.discoverBuild({}, {});
612
+ const firebaseEnvs = functionsEnv.loadFirebaseEnvs({ projectId }, projectId, options.instanceId);
613
+ return runtimeDelegate.discoverBuild({}, firebaseEnvs);
593
614
  }
594
615
  async function promptAndWriteKitParams(options) {
595
616
  if (!options.params || options.params.length === 0) {
@@ -608,6 +629,12 @@ async function promptAndWriteKitParams(options) {
608
629
  };
609
630
  const rawUserEnvs = functionsEnv.loadUserEnvs(userEnvOpt);
610
631
  const { userEnvs, secretRefs } = (0, prepare_1.partitionUserEnvs)(rawUserEnvs);
632
+ const parsedSecretRefs = (0, functional_1.mapObject)(secretRefs, (unparsed) => build.parseSecretRef(unparsed));
633
+ const secretPrefixForInstance = (0, projectConfig_1.addKitPrefix)(options.instanceId);
634
+ for (const secretParam of options.params.filter((p) => params.isSecretParam(p))) {
635
+ secretParam.resourceId = `${secretPrefixForInstance}-${secretParam.name}`;
636
+ }
637
+ build.applyEnvSecretBindingsToParams(options.params, parsedSecretRefs);
611
638
  let firebaseConfig = { projectId: options.projectId };
612
639
  try {
613
640
  firebaseConfig = await functionsConfig.getFirebaseConfig({ projectId: options.projectId });
@@ -616,7 +643,14 @@ async function promptAndWriteKitParams(options) {
616
643
  logger_1.logger.debug(`Could not fetch Firebase config for project ${options.projectId}: ${(0, error_1.getErrMsg)(err)}`);
617
644
  }
618
645
  const typedUserEnvs = build.envWithTypes(options.params, userEnvs);
619
- const { paramValues: resolvedEnvs, secretRefs: resolvedSecretRefs } = await params.resolveParams(options.params, firebaseConfig, typedUserEnvs, options.instanceId, options.nonInteractive, options.force);
646
+ const { paramValues: resolvedEnvs, secretRefs: resolvedSecretRefs } = await params.resolveParams({
647
+ params: options.params,
648
+ firebaseConfig: firebaseConfig,
649
+ userEnvs: typedUserEnvs,
650
+ codebase: options.instanceId,
651
+ nonInteractive: options.nonInteractive,
652
+ force: options.force,
653
+ });
620
654
  functionsEnv.writeResolvedParams(resolvedEnvs, userEnvs, userEnvOpt);
621
655
  if (experiments.isEnabled("secretEnvParams")) {
622
656
  functionsEnv.writeResolvedSecretRefs(resolvedSecretRefs, secretRefs, userEnvOpt);
@@ -627,9 +661,9 @@ async function printKitFirstDeployReport(options) {
627
661
  const prefix = (0, projectConfig_1.addKitPrefix)(options.instanceId);
628
662
  try {
629
663
  discoveredBuild = options.preDiscoveredBuild
630
- ? (0, utils_2.cloneDeep)(options.preDiscoveredBuild)
664
+ ? (0, utils_1.cloneDeep)(options.preDiscoveredBuild)
631
665
  : await discoverKitBuild(options, options.absSourcePath);
632
- build.applyPrefix(discoveredBuild, prefix);
666
+ build.applyEndpointPrefix(discoveredBuild, prefix);
633
667
  }
634
668
  catch (err) {
635
669
  logger_1.logger.debug(`Could not discover kit build for reporting: ${(0, error_1.getErrMsg)(err)}`);
@@ -677,7 +711,8 @@ async function printKitFirstDeployReport(options) {
677
711
  (0, utils_1.logLabeledWarning)("functions", `${clc.bold("Please review the changes above. If you do not want them applied to your project, uninstall this kit before running firebase deploy.")}`);
678
712
  }
679
713
  }
680
- async function addKitInstanceOrConfigureProject(options, existingKit, existingFunctionsInfo) {
714
+ async function addKitInstanceOrConfigureProject(options) {
715
+ const { existingKit, existingFunctionsInfo, createdPaths = [] } = options;
681
716
  const projectId = (0, projectUtils_1.getProjectId)(options) || options.projectId;
682
717
  const projectAlias = options.rc?.hasProjects && options.project && options.rc.hasProjectAlias(options.project)
683
718
  ? options.project
@@ -725,6 +760,9 @@ async function addKitInstanceOrConfigureProject(options, existingKit, existingFu
725
760
  if (action === "addInstance") {
726
761
  resultAction = "addedInstance";
727
762
  instanceId = await promptKitInstanceId(options.defaultInstanceId ?? existingKit.kit, existingFunctionsInfo.existingInstanceIds, existingFunctionsInfo.existingCodebases, options.nonInteractive, options.instanceId);
763
+ const expectedConfigDirPath = path.join(exports.FUNCTION_KITS_DIR, existingKit.kit, `config-${instanceId}`);
764
+ absConfigDirPath = options.config.path(expectedConfigDirPath);
765
+ createdPaths.push(absConfigDirPath);
728
766
  const result = await addInstanceToKit({
729
767
  config: options.config,
730
768
  kitId: existingKit.kit,
@@ -746,6 +784,12 @@ async function addKitInstanceOrConfigureProject(options, existingKit, existingFu
746
784
  throw new error_1.FirebaseError(`Configuration directory for instance '${instanceId}' not found in kit '${existingKit.kit}'.`);
747
785
  }
748
786
  absConfigDirPath = options.config.path(configDirPath);
787
+ if (projectId) {
788
+ const envPath = path.join(absConfigDirPath, `.env.${projectId}`);
789
+ if (!(await fs.pathExists(envPath))) {
790
+ createdPaths.push(envPath);
791
+ }
792
+ }
749
793
  if (options.seedEnv?.envs && Object.keys(options.seedEnv.envs).length > 0) {
750
794
  await fs.ensureDir(absConfigDirPath);
751
795
  (0, env_2.seedKitInstanceEnv)({
@@ -769,7 +813,7 @@ async function addKitInstanceOrConfigureProject(options, existingKit, existingFu
769
813
  const shouldConfigure = options.configure !== false;
770
814
  if (shouldConfigure) {
771
815
  try {
772
- discoveredBuild = await discoverKitBuild(options, absSourcePath);
816
+ discoveredBuild = await discoverKitBuild({ ...options, instanceId }, absSourcePath);
773
817
  }
774
818
  catch (err) {
775
819
  logger_1.logger.debug(`Could not discover kit build for params prompting: ${(0, error_1.getErrMsg)(err)}`);
@@ -840,7 +884,12 @@ async function resolvePackageSource(options) {
840
884
  }
841
885
  validateNpmPackageName(rawPkgName);
842
886
  const { packageName } = parseNpmPackageSpecifier(rawPkgName);
843
- const isThirdParty = await promptSecurityConfirmation(rawPkgName, packageName, options.nonInteractive);
887
+ const isThirdParty = await promptSecurityConfirmation({
888
+ rawPkgName,
889
+ packageName,
890
+ nonInteractive: options.nonInteractive,
891
+ force: options.force,
892
+ });
844
893
  return {
845
894
  defaultKitName: packageName,
846
895
  sourcePackageName: packageName,
@@ -882,80 +931,113 @@ async function installKitOrInstance(options) {
882
931
  if (options.directory && options.template) {
883
932
  throw new error_1.FirebaseError("Cannot specify --template with --directory.");
884
933
  }
885
- const existingFunctionsInfo = extractExistingFunctionsInfo(options.config.src.functions);
886
- const existingKit = findExistingKit(existingFunctionsInfo.existingFunctions, options);
887
- if (existingKit) {
888
- return addKitInstanceOrConfigureProject(options, existingKit, existingFunctionsInfo);
889
- }
890
- const source = options.directory
891
- ? await resolveDirectorySource(options)
892
- : await resolvePackageSource(options);
893
- const kitId = await promptKitId(source.defaultKitName, existingFunctionsInfo.existingKitIds, options.nonInteractive, options.kitId);
894
- const instanceId = await promptKitInstanceId(options.defaultInstanceId ?? kitId, existingFunctionsInfo.existingInstanceIds, existingFunctionsInfo.existingCodebases, options.nonInteractive, options.instanceId);
895
- const { sourcePath, configDirPath, absSourcePath, absConfigDirPath } = await source.setup(kitId, instanceId);
896
- if (options.seedEnv?.envs && Object.keys(options.seedEnv.envs).length > 0) {
897
- (0, env_2.seedKitInstanceEnv)({
898
- configDir: absConfigDirPath,
899
- functionsSource: absSourcePath,
900
- projectDir: options.config.projectDir,
901
- projectId: options.seedEnv.projectId,
902
- projectAlias: options.seedEnv.projectAlias,
903
- envs: options.seedEnv.envs,
904
- });
905
- }
906
- await source.buildAndInstall(absSourcePath);
907
- const projectId = (0, projectUtils_1.getProjectId)(options) || options.projectId;
908
- const projectAlias = options.rc?.hasProjects && options.project && options.rc.hasProjectAlias(options.project)
909
- ? options.project
910
- : undefined;
911
- if (projectId) {
912
- fs.ensureFileSync(path.join(absConfigDirPath, `.env.${projectId}`));
913
- }
914
- let discoveredBuild;
915
- const shouldConfigure = options.configure !== false;
916
- if (shouldConfigure) {
917
- try {
918
- discoveredBuild = await discoverKitBuild(options, absSourcePath);
934
+ const originalFunctions = (0, utils_1.cloneDeep)(options.config.src.functions);
935
+ const createdPaths = [];
936
+ let kitId;
937
+ try {
938
+ const existingFunctionsInfo = extractExistingFunctionsInfo(options.config.src.functions);
939
+ const existingKit = findExistingKit(existingFunctionsInfo.existingFunctions, options);
940
+ if (existingKit) {
941
+ kitId = existingKit.kit;
942
+ return await addKitInstanceOrConfigureProject({
943
+ ...options,
944
+ existingKit,
945
+ existingFunctionsInfo,
946
+ createdPaths,
947
+ });
919
948
  }
920
- catch (err) {
921
- logger_1.logger.debug(`Could not discover kit build for params prompting: ${(0, error_1.getErrMsg)(err)}`);
949
+ const source = options.directory
950
+ ? await resolveDirectorySource(options)
951
+ : await resolvePackageSource(options);
952
+ kitId = await promptKitId(source.defaultKitName, existingFunctionsInfo.existingKitIds, options.nonInteractive, options.kitId);
953
+ const instanceId = await promptKitInstanceId(options.defaultInstanceId ?? kitId, existingFunctionsInfo.existingInstanceIds, existingFunctionsInfo.existingCodebases, options.nonInteractive, options.instanceId);
954
+ const isPackageKit = !options.directory;
955
+ const absKitDir = options.config.path(path.join(exports.FUNCTION_KITS_DIR, kitId));
956
+ const expectedConfigDirPath = path.join(exports.FUNCTION_KITS_DIR, kitId, `config-${instanceId}`);
957
+ const absConfigDirPath = options.config.path(expectedConfigDirPath);
958
+ if (isPackageKit) {
959
+ createdPaths.push(absKitDir);
922
960
  }
923
- if (discoveredBuild?.params && discoveredBuild.params.length > 0) {
924
- await promptAndWriteKitParams({
925
- config: options.config,
926
- projectId,
927
- projectAlias,
928
- absConfigDirPath,
929
- absSourcePath,
930
- instanceId,
931
- nonInteractive: options.nonInteractive,
932
- force: options.force,
933
- params: discoveredBuild.params,
961
+ else {
962
+ createdPaths.push(absConfigDirPath);
963
+ }
964
+ const setupResult = await source.setup(kitId, instanceId);
965
+ const { sourcePath, configDirPath, absSourcePath } = setupResult;
966
+ if (options.seedEnv?.envs && Object.keys(options.seedEnv.envs).length > 0) {
967
+ (0, env_2.seedKitInstanceEnv)({
968
+ configDir: setupResult.absConfigDirPath,
969
+ functionsSource: absSourcePath,
970
+ projectDir: options.config.projectDir,
971
+ projectId: options.seedEnv.projectId,
972
+ projectAlias: options.seedEnv.projectAlias,
973
+ envs: options.seedEnv.envs,
934
974
  });
935
975
  }
976
+ await source.buildAndInstall(absSourcePath);
977
+ const projectId = (0, projectUtils_1.getProjectId)(options) || options.projectId;
978
+ const projectAlias = options.rc?.hasProjects && options.project && options.rc.hasProjectAlias(options.project)
979
+ ? options.project
980
+ : undefined;
981
+ if (projectId) {
982
+ fs.ensureFileSync(path.join(setupResult.absConfigDirPath, `.env.${projectId}`));
983
+ }
984
+ let discoveredBuild;
985
+ const shouldConfigure = options.configure !== false;
986
+ if (shouldConfigure) {
987
+ try {
988
+ discoveredBuild = await discoverKitBuild({ ...options, instanceId }, absSourcePath);
989
+ }
990
+ catch (err) {
991
+ logger_1.logger.debug(`Could not discover kit build for params prompting: ${(0, error_1.getErrMsg)(err)}`);
992
+ }
993
+ if (discoveredBuild?.params && discoveredBuild.params.length > 0) {
994
+ await promptAndWriteKitParams({
995
+ config: options.config,
996
+ projectId,
997
+ projectAlias,
998
+ absConfigDirPath: setupResult.absConfigDirPath,
999
+ absSourcePath,
1000
+ instanceId,
1001
+ nonInteractive: options.nonInteractive,
1002
+ force: options.force,
1003
+ params: discoveredBuild.params,
1004
+ });
1005
+ }
1006
+ }
1007
+ addKitToConfig(options.config, {
1008
+ kitId,
1009
+ instanceId,
1010
+ packageName: source.sourcePackageName,
1011
+ sourcePath,
1012
+ configDirPath,
1013
+ hasBuildScript: source.hasBuildScript,
1014
+ });
1015
+ (0, utils_1.logLabeledSuccess)("functions", `Function kit ${clc.bold(kitId)} successfully installed.`);
1016
+ await printKitFirstDeployReport({
1017
+ config: options.config,
1018
+ project: options.project,
1019
+ projectId: options.projectId,
1020
+ instanceId,
1021
+ absSourcePath,
1022
+ preDiscoveredBuild: discoveredBuild,
1023
+ });
1024
+ return {
1025
+ action: "installedKit",
1026
+ kitId,
1027
+ instanceId,
1028
+ sourcePath,
1029
+ configDirPath,
1030
+ };
1031
+ }
1032
+ catch (err) {
1033
+ await Promise.all(createdPaths.map((targetPath) => fs.remove(targetPath).catch((cleanupErr) => {
1034
+ logger_1.logger.debug(`Failed to clean up path '${targetPath}': ${(0, error_1.getErrMsg)(cleanupErr)}`);
1035
+ })));
1036
+ if (kitId) {
1037
+ await (0, fsutils_1.removeDirectoryIfEmpty)(options.config.path(path.join(exports.FUNCTION_KITS_DIR, kitId)));
1038
+ }
1039
+ await (0, fsutils_1.removeDirectoryIfEmpty)(options.config.path(exports.FUNCTION_KITS_DIR));
1040
+ revertFunctionsConfig({ config: options.config, originalFunctions });
1041
+ throw err;
936
1042
  }
937
- addKitToConfig(options.config, {
938
- kitId,
939
- instanceId,
940
- packageName: source.sourcePackageName,
941
- sourcePath,
942
- configDirPath,
943
- hasBuildScript: source.hasBuildScript,
944
- });
945
- (0, utils_1.logLabeledSuccess)("functions", `Function kit ${clc.bold(kitId)} successfully installed.`);
946
- await printKitFirstDeployReport({
947
- config: options.config,
948
- project: options.project,
949
- projectId: options.projectId,
950
- instanceId,
951
- absSourcePath,
952
- preDiscoveredBuild: discoveredBuild,
953
- });
954
- return {
955
- action: "installedKit",
956
- kitId,
957
- instanceId,
958
- sourcePath,
959
- configDirPath,
960
- };
961
1043
  }
package/lib/gcp/iam.js CHANGED
@@ -155,23 +155,14 @@ async function generateManagedServiceAccountName(projectId, prefix) {
155
155
  }
156
156
  throw new error_1.FirebaseError("Failed to generate a unique service account name after 10 attempts.");
157
157
  }
158
- function computeRolesEtag(roles, existingSalt) {
158
+ function computeRolesEtag(roles) {
159
+ const ALPHA_CHARS = "abcdefghijklmnopqrstuvwxyz";
159
160
  const BASE38_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_";
160
- let salt = existingSalt;
161
- if (!salt) {
162
- salt = String.fromCharCode(97 + Math.floor(Math.random() * 26));
163
- for (let i = 0; i < 9; i++) {
164
- salt += BASE38_CHARS[Math.floor(Math.random() * 38)];
165
- }
166
- }
167
- const sorted = Array.from(roles).sort();
168
- const hashBuffer = crypto
169
- .createHash("sha256")
170
- .update(salt + sorted.join(","))
171
- .digest();
172
- let hashStr = "";
173
- for (const byte of hashBuffer) {
174
- hashStr += BASE38_CHARS[byte % 38];
161
+ const sorted = Array.from(new Set(roles)).sort();
162
+ const hashBuffer = crypto.createHash("sha256").update(sorted.join(",")).digest();
163
+ let hashStr = ALPHA_CHARS[hashBuffer[0] % ALPHA_CHARS.length];
164
+ for (let i = 1; i < hashBuffer.length; i++) {
165
+ hashStr += BASE38_CHARS[hashBuffer[i] % BASE38_CHARS.length];
175
166
  }
176
- return `${salt}-${hashStr.substring(0, 52)}`;
167
+ return hashStr;
177
168
  }
package/lib/prompt.js CHANGED
@@ -82,8 +82,12 @@ async function select(opts) {
82
82
  if (shouldReturn) {
83
83
  return value;
84
84
  }
85
+ const instructions = typeof opts.instructions === "string"
86
+ ? { navigation: opts.instructions, pager: opts.instructions }
87
+ : opts.instructions;
85
88
  return inquirer.select({
86
89
  ...opts,
90
+ instructions,
87
91
  loop: false,
88
92
  });
89
93
  }