addPage static method

void addPage({
  1. String? featureName,
  2. String? routeName,
})

Implementation

static void addPage({String? featureName, String? routeName}) async {
  if (featureName == null) {
    stdout.write("${ColorsText.blue}Enter feature name: ${ColorsText.reset}");
    featureName = stdin.readLineSync();
  }
  if (routeName == null) {
    stdout.write("${ColorsText.blue}Enter route name: ${ColorsText.reset}");
    routeName = stdin.readLineSync();
  }

  String content = await CreatorUtil.readFileContent(
      '$path/features/$featureName/${featureName}_feature.dart');

  // Find the routes list using regex
  final routesRegex = RegExp(
    r'List<GoRoute>\s+get\s+routes\s*=>\s*\[(.*?)\];',
    dotAll: true,
  );
  final match = routesRegex.firstMatch(content);

  if (match != null) {
    CreatorUtil.createDirectory('$path/features/$featureName/pages');
    CreatorUtil.createFileWithContent(
        '$path/features/$featureName/pages/${routeName}_page.dart',
        pageSample(routeName ?? ''));

    final existingRoutes = match.group(1)?.trim() ?? '';
    final routeNameCamelCase = '_$routeName';
    final routeNameCapitalized = routeName?.toCapitalized ?? '';

    // Create new route with proper formatting
    String newRoute = '''GoRoute(
    path: $routeNameCamelCase,
    name: $routeNameCamelCase,
    builder: (_, state) => const ${routeNameCapitalized}Page(),
  )''';

    // Combine routes with proper formatting
    String updatedRoutes;
    if (existingRoutes.isEmpty) {
      updatedRoutes = '\n    $newRoute,\n  ';
    } else {
      // Remove trailing comma and whitespace from existing routes
      String cleanedRoutes = existingRoutes.trimRight();
      if (!cleanedRoutes.endsWith(',')) {
        cleanedRoutes += ',';
      }
      updatedRoutes = '$cleanedRoutes\n    $newRoute,\n  ';
    }

    // Replace the routes list
    content = content.replaceFirst(
      routesRegex,
      'List<GoRoute> get routes => [$updatedRoutes];',
    );

    // Add private getter after name getter
    final nameGetterRegex = RegExp(
      r"(String\s+get\s+name\s*=>\s*'[^']*';)",
      multiLine: true,
    );
    final nameMatch = nameGetterRegex.firstMatch(content);

    if (nameMatch != null) {
      final insertPosition = nameMatch.end;
      final privateGetter = "\n\n  String get $routeNameCamelCase => '/$routeName';";
      content = content.substring(0, insertPosition) +
                privateGetter +
                content.substring(insertPosition);
    }

    // Add push function before the closing brace
    final closingBraceIndex = content.lastIndexOf('}');
    if (closingBraceIndex != -1) {
      final pushFunction = "\n\n  void push${routeNameCapitalized}() => push(name: $routeNameCamelCase);\n";
      content = content.substring(0, closingBraceIndex) +
                pushFunction +
                content.substring(closingBraceIndex);
    }

    // Add import at the top
    content = '''import 'pages/${routeName}_page.dart';\n$content''';

    CreatorUtil.editFileContent(
        '$path/features/$featureName/${featureName}_feature.dart', content);
  } else {
    print(
        '${ColorsText.red}✗ Could not find routes list in feature file${ColorsText.reset}');
  }
}