run method

  1. @override
Future<void> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<void> run() async {
  final argResults = super.argResults!;
  final showDescriptions = argResults['description'] as bool;

  final pubspec = Pubspec();
  final info = await pubspec.getInfo();
  final scripts = await pubspec.getScripts();

  final registry = ScriptsRegistry(scripts);
  final paths = registry.getPaths()..sort();

  final definitions = paths.map((path) => registry.getDefinition(path)).toList();
  final descriptions = definitions.map((def) => def.description).toList();
  final references =
      definitions.map((def) => def.scripts.where((s) => s.startsWith(referencePrefix)).toList()).toList();

  final buffer = StringBuffer();

  buffer.writeln('+ $info');
  buffer.writeln('│');

  final longestScriptLength = _getLongestStringLength(paths);

  for (final pathEntry in paths.asMap().entries) {
    final pathIndex = pathEntry.key;
    final path = pathEntry.value;

    final description = descriptions[pathIndex];
    final refs = references[pathIndex];

    final formattedDescription = showDescriptions && description != null
        ? '${''.padLeft(longestScriptLength + 4 - path.length)} - $description'.gray()
        : '';

    buffer.writeln('${_getPrefix(pathIndex, paths.length)} $path $formattedDescription');

    for (final refEntry in refs.asMap().entries) {
      final referenceIndex = refEntry.key;
      final reference = refEntry.value;

      final formattedReference = reference
          .replaceAll('\\$referencePrefix', referencePrefix)
          .split(referenceNestingDelimiter)
          .join(' ')
          .green();

      buffer.writeln(
        '${pathIndex == paths.length - 1 ? ' ' : '│'}'
        '   '
        '${_getPrefix(referenceIndex, refs.length)} $formattedReference',
      );
    }
  }

  stdout.writeln(buffer.toString());
}