main function Null safety

Future<void> main(
  1. List<String> args
)

Implementation

Future<void> main(List<String> args) async {
  final params = _params(args);
  final directory = _directory(params);

  print('scanning ${directory.path} for routes...');
  final files = await directory.list(recursive: true).where((file) => file.path.endsWith('routes.dart')).toList();
  if(files.isEmpty) {
    print('no routes files found');
  }

  for(var file in files) {
    final path = file.path.substring(directory.path.length + 1); // remove leading slash
    final name = file.path.substring(file.parent.path.length + 1).replaceAll('.dart', '');
    print('found $name (${file.path})... processing...');

    final lines = await (file as File).readAsLines();
    final routes = RoutesParser(lines).parse();

    if(routes == null) {
      print('no routes found... exiting');
    } else {
      final gen = RoutesGenerator.generate('$name.dart', routes);
      final outputs = <File>[
        await File('${directory.path}/${path.replaceAll('.dart', '.g.dart')}').writeAsString(gen.routesLibrary),
        if(lines.contains('const genProvider = true;'))
          await File('${directory.path}/${path.replaceAll('.dart', '.g.provider.dart')}').writeAsString(gen.providerLibrary),
      ];
      print('  $path processed. formatting...');

      final results = await Process.run('dart', ['format', ...outputs.map((f) => f.path).toList()]);
      print(results.stdout);
    }
  }
}