getRouteCode method

String getRouteCode()

Generates the code for a stateless widget.

Implementation

String getRouteCode() {
  final buffer = StringBuffer();

  if (!File(routeFile).existsSync()) {
    Terminal.printError('Route file not found: $routeFile');
    exit(1);
  }

  // Read the file
  final pattern = RegExp(r'static\s+List<GetPage>\s+routes\s*=\s*\[');
  String code = File(routeFile).readAsStringSync().trim();

  final match = pattern.firstMatch(code);

  if (match != null) {
    // Insert custom string after matched part's end
    int insertPos = match.start;
    String customString = "";
    String routeListString = "";

    print('');
    for (var name in routes) {
      String routeName = NameHelper().toCamelCase(name);
      String route = NameHelper().toDashCase(name);

      customString += "static String $routeName = '/$route';\n  ";
      routeListString +=
          '    GetPage(name: AppConstants.$routeName, page: () => const Container()),\n';

      Terminal.printText(
        "Route added: static String /$routeName = '$route';",
      );
    }

    String result =
        '${code.substring(0, insertPos)}$customString\n  ${code.substring(insertPos)}';
    String updatedCode = insertPage(result, routeListString);
    buffer.write(updatedCode);
  } else {
    buffer.write(code);
  }

  return buffer.toString();
}