run method

  1. @override
Future run()
override

Runs this command.

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

Implementation

@override
Future run() async {
  final (json, output, routeExportPath, createRouteExportFile) = (
    argResults!.flag('json'),
    argResults!.option('output'),
    argResults!.option('route-export-path'),
    argResults!.flag('create-route-export-file'),
  );
  final binDir = join(Directory.current.path, 'bin');
  final routeExportFile = File(join(binDir, routeExportPath));

  if (!routeExportFile.existsSync() && !createRouteExportFile) {
    throw UsageException(
      'Route export file not found: $routeExportFile',
      usage,
    );
  }

  if (createRouteExportFile) {
    routeExportFile.createSync(recursive: true);
    routeExportFile.writeAsStringSync(await _makeRouteExportSource());
  }

  final routeExportResult = Process.runSync(
    'dart',
    ['run', routeExportFile.path],
    stdoutEncoding: utf8,
    stderrEncoding: utf8,
  );
  if (routeExportResult.exitCode != 0) {
    throw UsageException(
      'Failed to export routes: ${routeExportResult.stderr}',
      usage,
    );
  }

  final routeExportOutput = routeExportResult.stdout as String;
  final data = jsonDecode(routeExportOutput) as List;

  if (json) {
    print(
      const JsonEncoder.withIndent('  ').convert(data),
    );
  } else {
    final formatter = TableFormatter(
      data.cast<Map<String, dynamic>>().map(RouteMetadata.fromJson).toList(),
    );
    print('\n${formatter.format()}');
  }
}