createNewFeature function

Future<void> createNewFeature(
  1. String featureName
)

Implementation

Future<void> createNewFeature(String featureName) async {
  String destinationPath = 'lib/features/$featureName';
  String currentFilePath = Platform.script.toFilePath();
  List<String> pathSegments = currentFilePath.split(Platform.pathSeparator);
  if (pathSegments.length > 2) {
    pathSegments.removeRange(pathSegments.length - 5, pathSegments.length);
  }
  String projectDirectoryPath = pathSegments.join(Platform.pathSeparator);
  String sourceFolderPath =
      '$projectDirectoryPath/lib/create_feature/basic_structure';
  grantPermissions(sourceFolderPath);

  if (!Directory(sourceFolderPath).existsSync()) {
    print('Error: $sourceFolderPath folder does not exist');
    return;
  }

  Directory destinationDirectory = Directory(destinationPath);
  if (destinationDirectory.existsSync()) {
    print(
        'Error: Destination feature directory already exists. Please choose a different feature name');
    return;
  }

  try {
    await copyFolder(sourceFolderPath, destinationPath);
    processFiles(destinationPath, '/*', '', false);
    processFiles(destinationPath, '*/', '', false);
    renameFilesWithSubstring(destinationPath, 'basic_structure', featureName);
    processFiles(destinationPath, 'BasicStructure', featureName, true);
    processFiles(
        destinationPath, 'basic_structure', featureName.toLowerCase(), false);
    String? name = getPubspecInfo('name');
    if (name != null) {
      processFiles(destinationPath, 'flutter_clean_architecture', name, false);
    }
    print('Feature $featureName created');
  } catch (e) {
    print('Error copying folder: $e');
  }
}