text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  var __importDefault = (this && this.__importDefault) || function (mod) {
      return (mod && mod.__esModule) ? mod : { "default": mod };
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  exports.zipFunctions = exports.zipFunction = void 0;
  const path_1 = require("path");
  const make_dir_1 = __importDefault(require("make-dir"));
  const p_map_1 = __importDefault(require("p-map"));
  const feature_flags_1 = require("./feature_flags");
  const manifest_1 = require("./manifest");
  const runtimes_1 = require("./runtimes");
  const archive_size_1 = require("./utils/archive_size");
  const format_result_1 = require("./utils/format_result");
  const fs_1 = require("./utils/fs");
  const non_nullable_1 = require("./utils/non_nullable");
  const DEFAULT_PARALLEL_LIMIT = 5;
  // TODO: now that we have types, do we still need runtime validation?
  const validateArchiveFormat = (archiveFormat) => {
      if (!['none', 'zip'].includes(archiveFormat)) {
          throw new Error(`Invalid archive format: ${archiveFormat}`);
      }
  };
  // Zip `srcFolder/*` (Node.js or Go files) to `destFolder/*.zip` so it can be
  // used by AWS Lambda
  const zipFunctions = function (relativeSrcFolders, destFolder, { archiveFormat = 'zip', basePath, config = {}, featureFlags: inputFeatureFlags, manifest, parallelLimit = DEFAULT_PARALLEL_LIMIT, repositoryRoot = basePath, } = {}) {
      return __awaiter(this, void 0, void 0, function* () {
          validateArchiveFormat(archiveFormat);
          const featureFlags = (0, feature_flags_1.getFlags)(inputFeatureFlags);
          const srcFolders = (0, fs_1.resolveFunctionsDirectories)(relativeSrcFolders);
          const [paths] = yield Promise.all([(0, fs_1.listFunctionsDirectories)(srcFolders), (0, make_dir_1.default)(destFolder)]);
          const functions = yield (0, runtimes_1.getFunctionsFromPaths)(paths, { config, dedupe: true, featureFlags });
          const results = yield (0, p_map_1.default)(functions.values(), (func) => __awaiter(this, void 0, void 0, function* () {
              const zipResult = yield func.runtime.zipFunction({
                  archiveFormat,
                  basePath,
                  config: func.config,
                  destFolder,
                  extension: func.extension,
                  filename: func.filename,
                  mainFile: func.mainFile,
                  name: func.name,
                  repositoryRoot,
                  runtime: func.runtime,
                  srcDir: func.srcDir,
                  srcPath: func.srcPath,
                  stat: func.stat,
                  featureFlags,
              });
              return Object.assign(Object.assign({}, zipResult), { mainFile: func.mainFile, name: func.name, runtime: func.runtime });
          }), {
              concurrency: parallelLimit,
          });
          const formattedResults = yield Promise.all(results.filter(non_nullable_1.nonNullable).map((result) => __awaiter(this, void 0, void 0, function* () {
              const resultWithSize = yield (0, archive_size_1.addArchiveSize)(result);
              return (0, format_result_1.formatZipResult)(resultWithSize);
          })));
          if (manifest !== undefined) {
              yield (0, manifest_1.createManifest)({ functions: formattedResults, path: (0, path_1.resolve)(manifest) });
          }
          return formattedResults;
      });
  };
  exports.zipFunctions = zipFunctions;
  const zipFunction = function (relativeSrcPath, destFolder, { archiveFormat = 'zip', basePath, config: inputConfig = {}, featureFlags: inputFeatureFlags, repositoryRoot = basePath, } = {}) {
      return __awaiter(this, void 0, void 0, function* () {
          validateArchiveFormat(archiveFormat);
          const featureFlags = (0, feature_flags_1.getFlags)(inputFeatureFlags);
          const srcPath = (0, path_1.resolve)(relativeSrcPath);
          const functions = yield (0, runtimes_1.getFunctionsFromPaths)([srcPath], { config: inputConfig, dedupe: true, featureFlags });
          if (functions.size === 0) {
              return;
          }
          const { config, extension, filename, mainFile, name, runtime, srcDir, stat: stats, } = functions.values().next().value;
          yield (0, make_dir_1.default)(destFolder);
          const zipResult = yield runtime.zipFunction({
              archiveFormat,
              basePath,
              config,
              destFolder,
              extension,
              featureFlags,
              filename,
              mainFile,
              name,
              repositoryRoot,
              runtime,
              srcDir,
              srcPath,
              stat: stats,
          });
          return (0, format_result_1.formatZipResult)(Object.assign(Object.assign({}, zipResult), { mainFile, name, runtime }));
      });
  };
  exports.zipFunction = zipFunction;
  //# sourceMappingURL=zip.js.map