createBarrelFiles method

Future<bool> createBarrelFiles(
  1. List<Barrel> barrels
)

Implementation

Future<bool> createBarrelFiles(List<Barrel> barrels) async {
  String? failure;

  for (final barrel in barrels) {
    final path = barrel.dirSettings.dirPath;
    final name = basename(barrel.barrelFile);
    final barrelString = '${blue.wrap(name)} ${darkGray.wrap('— in $path')}';
    final done = logger.progress(
      'Creating $barrelString',
    );

    try {
      final result = await barrel.create(allowChange: !exitOnChange);
      final contentMatches = result?.contentMatches == true;

      if (exitOnChange && !contentMatches) {
        final error = 'Changes detected in $barrelString';
        done.fail(barrelString);
        failure ??= '';
        failure += '\n$error';
      } else if (exitOnChange) {
        done.complete('No changes to $barrelString');
      } else {
        done.complete();
      }
    } catch (e) {
      failure ??= '';
      failure += '\n$barrelString';
      done.fail(barrelString);
      logger.err('Error building ${barrel.dirSettings.dirPath} $e');
    }
  }

  if (failure != null) {
    logger.write('\n');
    logger.err('${red.wrap('\nErrors:')}$failure');

    return false;
  }

  return true;
}