function getCompareConfiguration()

in packages/tools/compare/src/cli.ts [57:207]


function getCompareConfiguration(args: string[]): RunConfiguration {
  let configPath: string | undefined = undefined;
  let languageToRun: AutoRestLanguage | undefined = undefined;
  let useExistingOutput: UseExistingOutput | undefined;

  function warnIfConfigFileUsed(argName: string): boolean {
    if (configPath !== undefined) {
      console.log(chalk.gray(`Skipping argument '${argName}' and using value from ${configPath}`));

      return true;
    }

    return false;
  }

  const languageConfig: LanguageConfiguration = {
    language: undefined as any,
    outputPath: undefined as any,
    oldArgs: [],
    newArgs: [],
  };

  const specConfig: SpecConfiguration = {
    specRootPath: undefined as any,
    specPaths: [],
  };

  let runConfig: RunConfiguration = {
    debug: false,
    specs: [specConfig],
    languages: [languageConfig],
  };

  while (args.length > 0) {
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    const [argName, argValue] = parseArgument(args.shift()!);

    switch (argName) {
      case "compare":
        if (argValue !== "true") {
          if (!fs.existsSync(argValue)) {
            throw new Error(`Configuration file does not exist: ${argValue}`);
          }

          configPath = argValue;
          // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
          runConfig = loadConfiguration(configPath)!;
        }
        break;

      case "old-args":
        if (!warnIfConfigFileUsed("old-args")) {
          [languageConfig.oldArgs, args] = getAutoRestArgs(args);
        }
        break;

      case "new-args":
        if (!warnIfConfigFileUsed("new-args")) {
          [languageConfig.newArgs, args] = getAutoRestArgs(args);
        }
        break;

      case "spec-path":
        if (!warnIfConfigFileUsed("spec-path")) {
          specConfig.specPaths.push(argValue);
        }
        break;

      case "spec-root-path":
        if (!warnIfConfigFileUsed("spec-root-path")) {
          specConfig.specRootPath = argValue;
        }
        break;

      case "output-path":
        if (!warnIfConfigFileUsed("output-path")) {
          languageConfig.outputPath = argValue;
        }
        break;

      case "use-existing-output":
        if (argValue === "old" || argValue === "all" || argValue === "none") {
          useExistingOutput = argValue;
        } else {
          throw new Error(`Unexpected value for --use-existing-output: ${argValue}`);
        }
        break;

      case "language":
        if (AutoRestLanguages.indexOf(argValue as AutoRestLanguage) > -1) {
          languageToRun = argValue as AutoRestLanguage;
          if (configPath === undefined) {
            languageConfig.language = languageToRun;
          }
        } else {
          throw new Error(
            `Unexpected value for --language: ${argValue}.  Supported languages are: ${AutoRestLanguages.join(", ")}.`,
          );
        }
        break;

      case "debug":
        runConfig.debug = argValue === "true";
        break;
    }
  }

  // Add debug flags if --debug was set globally
  if (runConfig.debug) {
    for (const language of runConfig.languages) {
      language.oldArgs.push("--debug");
      language.newArgs.push("--debug");
    }
  }

  if (configPath === undefined && languageConfig.language === undefined) {
    throw new Error(
      `Missing language parameter.  Please use one of the following:\n${["", ...AutoRestLanguages].join(
        "\n    --language:",
      )}\n`,
    );
  }

  if (configPath === undefined && languageConfig.outputPath === undefined) {
    throw new Error("An output path must be provided with the --output-path parameter.");
  }

  if (configPath === undefined && specConfig.specPaths.length === 0) {
    throw new Error("A spec path must be provided with the --spec-path parameter.");
  }

  // Resolve paths relative to configuration file or the current directory
  const fullConfigPath = configPath ? path.dirname(path.resolve(configPath)) : process.cwd();

  runConfig = {
    ...runConfig,
    specs: runConfig.specs.map(resolveSpecRootPath(fullConfigPath)),
    languages: runConfig.languages.map(prepareLanguageConfiguration(fullConfigPath, useExistingOutput)),
  };

  // Filter language configurations to desired language
  if (languageToRun) {
    const language = runConfig.languages.find((l) => l.language === languageToRun);
    if (language === undefined) {
      throw new Error(`No language configurations are available after --language:${languageToRun} filter.`);
    }
    runConfig.languages = [language];
  }

  return runConfig;
}