generateFeature function

void generateFeature({
  1. required String projectName,
  2. required String featureName,
})

Generates a complete feature with clean architecture structure.

Auto-detects state management from .sm_cli_config. Creates Data, Domain, and Presentation layers.

Example:

cd my_app
sm make feature auth

Implementation

void generateFeature({
  required String projectName,
  required String featureName,
}) {

  if (!RegExp(r'^[a-z][a-z0-9_]*$').hasMatch(featureName)) {
    print('❌ Invalid feature name: "$featureName"');
    print('   Use snake_case only — e.g: auth, user_profile, home_screen');
    return;
  }

  // 2. Already exists check
  if (Directory('$projectName/lib/features/$featureName').existsSync()) {
    print('⚠️ Feature "$featureName" already exists!');
    print('   Use a different name or delete the existing feature first.');
    return;
  }

  final stateManagement = ConfigService.readStateManagement(projectName);

  final folders = [
    'lib/features/$featureName/data/datasource',
    'lib/features/$featureName/data/models',
    'lib/features/$featureName/data/repository',
    'lib/features/$featureName/domain/entities',
    'lib/features/$featureName/domain/repository',
    'lib/features/$featureName/domain/usecases',
    'lib/features/$featureName/presentation/screens',
    'lib/features/$featureName/presentation/widgets',
  ];

  if (stateManagement == 'Bloc') {
    folders.add('lib/features/$featureName/presentation/bloc');
  } else if (stateManagement == 'GetX') {
    folders.add('lib/features/$featureName/presentation/controllers');
    folders.add('lib/features/$featureName/presentation/bindings');
    folders.add('lib/features/$featureName/presentation/views');
  } else {
    folders.add('lib/features/$featureName/presentation/providers');
  }

  // 1. Feature name validation


  for (final folder in folders) {
    Directory('$projectName/$folder').createSync(recursive: true);
  }

  createFeatureFiles(
    projectName: projectName,
    featureName: featureName,
    stateManagement: stateManagement,
  );

  addRouteConstant(projectName: projectName, featureName: featureName);
  addRoute(projectName: projectName, featureName: featureName);

  print('✅ Feature "$featureName" generated ($stateManagement)');
}