async function computePath()

in src/core/frameworks/detect.ts [78:119]


async function computePath(basePath: string, additionalPath?: string): Promise<string> {
  if (!additionalPath) {
    return basePath;
  }

  if (!additionalPath.startsWith("{")) {
    return path.join(basePath, additionalPath);
  }

  // Matches {<filename>#<expression>}, the first group is the filename, the second the expression
  const match = additionalPath.match(/^\{(.*?)#(.*?)\}$/);
  const [, filename, expression] = match || [];
  if (!filename || !expression) {
    throw new Error(`Invalid dynamic path format: ${additionalPath}`);
  }

  const files = await getFiles(basePath);
  const file = findFile(filename, files);
  if (!file) {
    throw new Error(`File "${filename}" not found in dynamic path: ${additionalPath}`);
  }

  const json = await safeReadJson(file);
  if (!json) {
    throw new Error(`Invalid JSON file: ${file}`);
  }

  const evaluateExpression = (json: JsonData, expr: string) => Function(`"use strict";return data => (${expr})`)()(json);

  try {
    const result = evaluateExpression(json, expression);
    if (result) {
      return path.join(basePath, result);
    }
  } catch (error) {
    const err = error as Error;
    logger.silly(err.stack || err.message);
    throw new Error(`Invalid expression "${expression}" in dynamic path: ${additionalPath}`);
  }

  return basePath;
}