async function runner()

in packages/flow-dev-tools/src/update-suppressions/update-suppressionsRunner.js [367:452]


async function runner(args: Args): Promise<void> {
  let ignoredFileCount = 0;
  let ignoredErrorCount = 0;
  let removedErrorCount = 0;
  let addedSuppressionCount = 0;
  let filesWithUnusedSuppressionsCount = 0;
  let filesWithNeededSuppressionsCount = 0;
  const rootsByFile: Map<string, Set<RootName>> = await getFiles(args);
  const rawErrors: Map<
    string,
    Map<number, ErrorsForLine>,
  > = await getErrorsForAllRoots(args);
  const errors = Array.from(rawErrors.entries())
    // Filter out flowtests and generate stats
    .filter(([filename, errors]) => {
      if (!args.includeFlowtest && isFlowtest(filename)) {
        ignoredFileCount++;
        ignoredErrorCount += errors.size;
        return false;
      } else {
        let fileHasUnusedSuppression = false;
        let fileNeedsSuppression = false;
        for (const [
          _loc,
          {unusedSuppressions, errorCodes},
        ] of errors.entries()) {
          if (errorCodes.size > 0) {
            addedSuppressionCount += errorCodes.size;
            fileNeedsSuppression = true;
          }
          if (unusedSuppressions != null) {
            removedErrorCount++;
            fileHasUnusedSuppression = true;
          }
        }
        if (fileHasUnusedSuppression) {
          filesWithUnusedSuppressionsCount++;
        }
        if (fileNeedsSuppression) {
          filesWithNeededSuppressionsCount++;
        }
      }
      return true;
    });
  const errorBatches = [];
  while (errors.length) {
    errorBatches.push(errors.splice(0, 500));
  }
  for (const errors of errorBatches) {
    await Promise.all(
      errors.map(([filename, errors]) => {
        const allRoots = rootsByFile.get(filename);
        if (allRoots == null) {
          throw new Error(`${filename} not in any site???`);
        }
        return updateSuppressions(
          filename,
          allRoots,
          args.diffBin ? 2 : 1,
          errors,
          args.comment,
          args.bin,
        );
      }),
    );
  }
  console.log(
    'Removed %d comments in %d files',
    removedErrorCount,
    errors.length,
  );
  console.log(
    'Added %d comments in %d files',
    addedSuppressionCount,
    errors.length,
  );
  if (ignoredFileCount > 0) {
    console.log(
      'Ignored %d comments in %d files due to -flowtest.js suffix or ' +
        '__flowtests__ directory. Run with `--include-flowtest` to also ' +
        'remove those files.',
      ignoredErrorCount,
      ignoredFileCount,
    );
  }
}