generateFeature method

Future<RenderResult> generateFeature({
  1. required String rawFeatureName,
  2. required String projectPath,
})

Generates a full feature skeleton into projectPath.

Implementation

Future<RenderResult> generateFeature({
  required String rawFeatureName,
  required String projectPath,
}) async {
  final projectDir = Directory(projectPath);
  Validators.validateExistingFlutterProject(projectDir);

  final variables = VariableResolver.resolveFeature(rawFeatureName: rawFeatureName);
  final featureSnakeCase = variables['feature_name_snake_case']!;

  // Check if feature directory already exists
  final targetFeatureDir = Directory(p.join(projectPath, 'lib', 'features', featureSnakeCase));
  if (targetFeatureDir.existsSync() && targetFeatureDir.listSync().isNotEmpty) {
    throw DestinationConflictException(
      targetFeatureDir.path,
      customMessage:
          'Feature "$rawFeatureName" already exists at "${targetFeatureDir.path}". Cannot generate existing feature.',
    );
  }

  final fragmentsBasePath = await _getFragmentsBasePath();
  final fragmentDir = Directory(p.join(fragmentsBasePath, 'feature'));
  if (!fragmentDir.existsSync()) {
    throw CliException('Fragment template not found at "${fragmentDir.path}".');
  }

  final fragmentTemplate = Template(
    name: 'feature_fragment',
    description: 'Feature skeleton fragment',
    path: fragmentDir.path,
    config: const TemplateConfig(
      name: 'feature_fragment',
      description: '',
      dependencies: [],
      folders: [],
    ),
  );

  return _renderWithScopedRollback(
    template: fragmentTemplate,
    variables: variables,
    projectPath: projectPath,
  );
}