{"size": 1844, "ext": "js", "max_stars_repo_path": "build-system/tasks/storybook/env/disabled-stories.js", "max_stars_repo_name": "fikus/amphtml", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "build-system/tasks/storybook/env/disabled-stories.js", "max_issues_repo_name": "fikus/amphtml", "max_issues_repo_issues_event_min_datetime": "2022-02-25T13:40:02.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-25T16:14:42.000Z", "max_forks_repo_path": "build-system/tasks/storybook/env/disabled-stories.js", "max_forks_repo_name": "fikus/amphtml", "max_forks_repo_forks_event_min_datetime": "2022-03-16T13:24:31.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-16T13:24:31.000Z"}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": 1, "avg_line_length": 39.2340425532, "max_line_length": 107, "alphanum_fraction": 0.703362256}
const globby = require('globby');
const {yellow} = require('kleur/colors');
const {logWithoutTimestamp} = require('../../../common/logging');

/**
 * Remove disabled Storybook files from an evaluated glob pattern.
 * This is required since the disabled Stories use the Knobs addon, which is now
 * obsolete and prevents us from upgrading the Storybook version.
 * @param {string[]} inclusionPattern
 * @return {string[]}
 */
function globExcludeDisabledStorybookFiles(inclusionPattern) {
  const {
    forbiddenTermsGlobal,
  } = require('../../../test-configs/forbidden-terms');
  const forbiddenTerm = `@storybook/${''}addon-knobs`;
  const forbiddenTermsEntry = forbiddenTermsGlobal[forbiddenTerm];
  if (!forbiddenTermsEntry?.allowlist?.length) {
    throw new Error(
      `Forbidden terms entry for "${forbiddenTerm}" not found, or it lacks an allowlist.` +
        '\nThis likely means that globExcludeDisabledStorybookFiles() should be removed.' +
        '\nIts callsites can be replaced with its argument, like the diff at:' +
        '\nhttps://gist.github.com/alanorozco/6e8a64a38cb0d6967c193784624d1011'
    );
  }
  const excluded = new Set(forbiddenTermsEntry.allowlist);
  const prefiltered = globby.sync(inclusionPattern);
  const filtered = prefiltered.filter((filename) => {
    const relativeToRoot = filename.replace(/^([.]{1,2}\/)+/, '');
    return !excluded.has(relativeToRoot);
  });
  const excludedTotal = prefiltered.length - filtered.length;
  if (excludedTotal > 0) {
    logWithoutTimestamp(
      yellow(
        `WARNING: ${excludedTotal} Storybook files have been disabled due to usage of "${forbiddenTerm}"` +
          `\nSee allowlist for "${forbiddenTerm}" in build-system/test-configs/forbidden-terms.js`
      )
    );
  }
  return filtered;
}

module.exports = {
  globExcludeDisabledStorybookFiles,
};
