<commit_before>/* @ts-ignore */
import LegacyCssFormatter from './legacy-css-formatter';
import { Style, StyleMap, StylebotOptions } from '@stylebot/types';

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const isNewFormat = (styles: any) => {
  const urls: Array<string> = Object.keys(styles);
  return !!urls.find(url => !!styles[url].css);
};

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const backupV2Styles = (styles: any) => {
  return new Promise(resolve => {
    chrome.storage.local.set({ backup_v2_styles: styles }, () => {
      resolve();
    });
  });
};

export const getMigratedStyles = (): Promise<StyleMap> => {
  return new Promise(resolve => {
    chrome.storage.local.get('styles', async items => {
      const oldStyles = items['styles'];

      if (!oldStyles) {
        resolve({});
        return;
      }

      // test to ensure styles are not already in the new format
      if (isNewFormat(oldStyles)) {
        resolve(oldStyles);
        return;
      }

      // backup old styles, in case we run into a bug
      await backupV2Styles(oldStyles);

      const formatter = new LegacyCssFormatter();
      const urls: Array<string> = Object.keys(oldStyles);

      const results: Array<Promise<Style>> = urls.map(
        async (url): Promise<Style> => {
          const style = oldStyles[url];

          return new Promise(resolveStyle => {
            formatter.format(style._rules, false, (css: string) => {
              resolveStyle({
                url,
                css,
                enabled: style._enabled,
                readability: false,
              });
            });
          });
        }
      );

      Promise.all(results).then(formattedStyles => {
        const styles: StyleMap = {};

        formattedStyles.forEach(({ url, css, enabled, readability }) => {
          styles[url] = { css, enabled, readability };
        });

        resolve(styles);
      });
    });
  });<commit_msg>add try/catch when formatting old styles<commit_after>/* @ts-ignore */
import LegacyCssFormatter from './legacy-css-formatter';
import { Style, StyleMap, StylebotOptions } from '@stylebot/types';

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const isNewFormat = (styles: any) => {
  const urls: Array<string> = Object.keys(styles);
  return !!urls.find(url => !!styles[url].css);
};

/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
const backupV2Styles = (styles: any) => {
  return new Promise(resolve => {
    chrome.storage.local.set({ backup_v2_styles: styles }, () => {
      resolve();
    });
  });
};

export const getMigratedStyles = (): Promise<StyleMap> => {
  return new Promise(resolve => {
    chrome.storage.local.get('styles', async items => {
      const oldStyles = items['styles'];

      if (!oldStyles) {
        resolve({});
        return;
      }

      // test to ensure styles are not already in the new format
      if (isNewFormat(oldStyles)) {
        resolve(oldStyles);
        return;
      }

      // backup old styles, in case we run into a bug
      await backupV2Styles(oldStyles);

      const formatter = new LegacyCssFormatter();
      const urls: Array<string> = Object.keys(oldStyles);

      const results: Array<Promise<Style>> = urls.map(
        async (url): Promise<Style> => {
          const style = oldStyles[url];

          return new Promise(resolveStyle => {
            try {
              formatter.format(style._rules, false, (css: string) => {
                resolveStyle({
                  url,
                  css,
                  enabled: style._enabled,
                  readability: false,
                });
              });
            } catch (e) {
              // guard against badly formatted style
              resolveStyle({
                url,
                css: '',
                enabled: style._enabled,
                readability: false,
              });
            }
          });
        }
      );

      Promise.all(results).then(formattedStyles => {
        const styles: StyleMap = {};

        formattedStyles.forEach(({ url, css, enabled, readability }) => {
          styles[url] = { css, enabled, readability };
        });

        resolve(styles);
      });
    });
  });
{'fraction_non_alphanumeric': 0.13934045517882024, 'fraction_numerical': 0.0013934045517882026, 'mean_word_length': 2.0589488636363638, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3510966', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1212, 'n_words': 276}