listing method

String listing()

The scripts and hooks in the project, followed by the run options.

Implementation

String listing() {
  final scripts = context.project?.scripts.values.toList() ?? const [];
  final runnable = scripts.where((script) => !script.isHook).toList();
  final hooks = scripts.where((script) => script.isHook).toList();
  final width = scripts.fold(
    0,
    (w, s) => s.name.length > w ? s.name.length : w,
  );

  String line(Script script) {
    final summary = script.description ?? console.dim(script.command);
    final scope = script.runInPackages == null
        ? ''
        : console.dim(' (in ${script.runInPackages!.join(', ')})');
    final needs = script.dependsOn.isEmpty
        ? ''
        : console.dim(' (depends on ${script.dependsOn.join(', ')})');
    return '  ${console.bold(script.name.padRight(width))}  '
        '$summary$scope$needs';
  }

  String hookLine(Script hook) {
    final targets = hook.all
        ? 'every command and script'
        : (hook.hookTargets?.join(', ') ?? 'nothing');
    final summary = isSharedHookName(hook.name)
        ? 'runs ${hook.name} $targets'
        : (hook.description ?? hook.command);
    return '  ${console.bold(hook.name.padRight(width))}  ${console.dim(summary)}';
  }

  return [
    'Usage: $invocation',
    '',
    if (runnable.isEmpty)
      'No scripts are defined in dpk.yaml.'
    else ...[
      'Scripts:',
      for (final script in runnable) line(script),
    ],
    if (hooks.isNotEmpty) ...[
      '',
      'Hooks:',
      for (final hook in hooks) hookLine(hook),
    ],
    '',
    argParser.usage,
    '',
    usageFooter.trimLeft(),
  ].join('\n');
}