execute method

  1. @override
Future<int> execute(
  1. List<String> arguments
)
override

Implementation

@override
Future<int> execute(List<String> arguments) async {
  final options = _RouteListOptions.parse(arguments);
  if (options == null) return ExitCode.usage;

  final entrypoint = File('${workingDirectory.path}/bin/server.dart');
  if (!entrypoint.existsSync()) {
    Console.error('bin/server.dart does not exist.');
    return ExitCode.noInput;
  }

  final result = await Process.run(
    'dart',
    ['run', 'bin/server.dart'],
    workingDirectory: workingDirectory.path,
    environment: const {'VANIA_DUMP_ROUTES': '1'},
  );
  final routes = RouteListParser.parse(result.stdout.toString());
  if (routes == null) {
    Console.error('Could not read the route table from the application.');
    final details = result.stderr.toString().trim();
    if (details.isNotEmpty) Console.line(Console.dim(details));
    return ExitCode.failure;
  }

  final matching =
      routes.where(options.filter.matches).toList()..sort((left, right) {
        final pathOrder = left.uri.compareTo(right.uri);
        return pathOrder == 0
            ? left.method.compareTo(right.method)
            : pathOrder;
      });

  if (options.json) {
    Console.line(
      const JsonEncoder.withIndent(
        '  ',
      ).convert(matching.map((route) => route.toJson()).toList()),
    );
    return ExitCode.success;
  }
  if (matching.isEmpty) {
    Console.info(
      routes.isEmpty
          ? 'No routes are registered.'
          : 'No routes match the filters.',
    );
    return ExitCode.success;
  }

  Console.table(
    const ['METHOD', 'URI', 'NAME', 'MIDDLEWARE'],
    matching
        .map(
          (route) => [
            route.method,
            route.uri,
            route.name ?? '',
            route.middleware.join(', '),
          ],
        )
        .toList(),
    colorize: (column, value) {
      if (column == 0) return _colorMethod(value.trim(), value);
      if (column >= 2) return Console.dim(value);
      return value;
    },
  );
  Console.line();
  Console.info(Console.dim('${matching.length} route(s)'));
  return ExitCode.success;
}