export async function setTheme()

in packages/autocomplete/src/fig/themes.ts [282:317]


export async function setTheme(
  currentSystemTheme: SystemTheme,
  newTheme?: string,
) {
  try {
    if (!newTheme) throw new MissingThemeError();
    if (newTheme === "system") {
      setCSSProperties(builtInThemes[currentSystemTheme], currentSystemTheme);
      return;
    }
    if (isBuiltInTheme(newTheme)) {
      setCSSProperties(builtInThemes[newTheme], newTheme);
      return;
    }
    const theme: string | undefined = fig.constants?.themesFolder
      ? await fread(`${fig.constants.themesFolder}/${newTheme}.json`)
      : undefined;

    if (!theme) {
      throw new MissingThemeError(
        "Cannot find a local theme with the specified name",
      );
    }

    const parsedTheme = JSON.parse(theme) as ThemeDefinition;

    // All themes fallback to the dark theme if values are missing
    setCSSProperties({ ...builtInThemes.dark, ...parsedTheme }, "dark");
  } catch (err) {
    logger.info(
      "There was an error parsing the theme. Using default dark theme",
      err,
    );
    setCSSProperties(builtInThemes.dark, "dark");
  }
}