{"size": 1122, "ext": "js", "max_stars_repo_path": "lib/cleaner.js", "max_stars_repo_name": "Zahidul-Islam/lambda-perf", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/cleaner.js", "max_issues_repo_name": "Zahidul-Islam/lambda-perf", "max_issues_repo_issues_event_min_datetime": "2021-05-10T22:58:33.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-10T22:58:33.000Z", "max_forks_repo_path": "lib/cleaner.js", "max_forks_repo_name": "Zahidul-Islam/lambda-perf", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 26.7142857143, "max_line_length": 79, "alphanum_fraction": 0.6675579323}
import utils from "../lib/utils";

const powerValues = process.env.MEMORY_ALLOCATION.split(",");

const validateInput = lambdaARN => {
  if (!lambdaARN) {
    throw new Error("Missing or empty lambdaARN");
  }
  if (!powerValues.length) {
    throw new Error("Missing or empty env.powerValues");
  }
};

const cleanup = async (lambdaARN, alias) => {
  try {
    const { FunctionVersion } = await utils.checkLambdaAlias(lambdaARN, alias);
    // delete both alias and version (could be done in parallel!)
    await utils.deleteLambdaAlias(lambdaARN, alias);
    await utils.deleteLambdaVersion(lambdaARN, FunctionVersion);
  } catch (error) {
    if (error.code === "ResourceNotFoundException") {
      console.error("OK, even if version/alias was not found");
      console.error(error);
    } else {
      console.error(error);
      throw error;
    }
  }
};

export default async function(options) {
  const { arn: lambdaARN } = options;
  validateInput(lambdaARN);

  const ops = powerValues.map(async value => {
    const alias = "RAM" + value;
    await cleanup(lambdaARN, alias);
  });

  await Promise.all(ops);
}
