classifyCommand function

CommandCategory classifyCommand(
  1. String command
)

Classify a command into a CommandCategory.

Implementation

CommandCategory classifyCommand(String command) {
  final exe = extractExecutable(command);
  if (exe == null || exe.isEmpty) return CommandCategory.other;

  final baseName = exe.split('/').last;

  // Special sub-command patterns FIRST (before generic lookup) so that
  // `flutter test`, `cargo test`, `npm test`, etc. resolve to the correct
  // category instead of falling through to packageManager via the map.
  const polysemous = {'cargo', 'dart', 'flutter', 'go', 'npm', 'yarn', 'pnpm'};
  if (!polysemous.contains(baseName)) {
    // Direct lookup.
    final cat = _categoryMap[baseName];
    if (cat != null) return cat;
  }

  if (baseName == 'cargo') {
    if (command.contains('test')) return CommandCategory.testing;
    if (command.contains('fmt') || command.contains('clippy')) {
      return CommandCategory.linting;
    }
    return CommandCategory.packageManager;
  }
  if (baseName == 'dart') {
    if (command.contains('analyze') || command.contains('format')) {
      return CommandCategory.linting;
    }
    if (command.contains('test')) return CommandCategory.testing;
    if (command.contains('pub')) return CommandCategory.packageManager;
    return CommandCategory.compiler;
  }
  if (baseName == 'flutter') {
    if (command.contains('test')) return CommandCategory.testing;
    if (command.contains('analyze')) return CommandCategory.linting;
    return CommandCategory.packageManager;
  }
  if (baseName == 'go') {
    if (command.contains('test')) return CommandCategory.testing;
    if (command.contains('vet') || command.contains('lint')) {
      return CommandCategory.linting;
    }
    return CommandCategory.packageManager;
  }
  if (baseName == 'npm' || baseName == 'yarn' || baseName == 'pnpm') {
    if (command.contains('test')) return CommandCategory.testing;
    if (command.contains('lint') || command.contains('format')) {
      return CommandCategory.linting;
    }
    return CommandCategory.packageManager;
  }

  return CommandCategory.other;
}