run method

  1. @override
Future<void> run()
override

Runs this command.

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

Implementation

@override
Future<void> run() async {
  if (argResults?.rest.isEmpty ?? true) {
    printError('Please provide a feature name.');
    exit(1);
  }

  final featureName = argResults!.rest[0];
  final projectDir = argResults?['project-dir'] as String;

  // Check if it's a Flutter project
  final pubspecFile = File(path.join(projectDir, 'pubspec.yaml'));
  if (!pubspecFile.existsSync()) {
    printError('Cannot find pubspec.yaml. Are you in a Flutter project?');
    exit(1);
  }

  // Create feature folder structure and files
  createFeatureFolders(featureName, projectDir);

  try {
    // Add routes
    final pathRoute = '/$featureName';
    addRouteToAppRoutes(featureName, pathRoute, projectDir);

    final className =
        '${featureName[0].toUpperCase()}${featureName.substring(1)}Page';
    addRouteToAppPages(featureName, className, projectDir);

    printSuccess('✅ Feature $featureName has been added successfully.');
  } catch (e) {
    printWarning('Route files not updated: $e');
    printWarning('You may need to add routes manually.');
  }
}