removeAllRelatedApiPage method

void removeAllRelatedApiPage(
  1. String pathPage,
  2. String pageName,
  3. Map pageValue,
  4. bool isReplace,
)

Implementation

void removeAllRelatedApiPage(
  String pathPage,
  String pageName,
  Map pageValue,
  bool isReplace,
) {
  removeFile(join(pathPage, 'locator.dart'));
  removeFile(join(pathPage, 'mapper.dart'));

  if (isReplace) {
    final api = pageValue.keys;
    final pathDataDatasources = join(pathPage, 'data', 'datasources',
        '${pageName.snakeCase}_remote_data_source.dart');
    if (exists(pathDataDatasources)) {
      String classDataDatasources = readFile(pathDataDatasources);
      for (var element in api) {
        classDataDatasources = classDataDatasources.replaceAll(
          RegExp(
              '(import([\\s\\\'\\"\\.a-zA-Z\\/_]+)${element.toString().snakeCase}_(\\w+)\\.dart(\\\'|\\");)|(([a-zA-Z\\<\\>\\s]+)${element.toString().camelCase}([a-zA-Z\\s\\(\\)]+);)|((\\@override)(\\s+)([a-zA-Z\\<\\>\\s]+)${element.toString().camelCase}([a-zA-Z\\d\\<\\>\\s\\(\\)\\{=\\.\\,\\:\\;]+)})'),
          '',
        );
      }
      if (!RegExp(r'}(\s+)?}').hasMatch(classDataDatasources)) {
        classDataDatasources += '}';
      }
      pathDataDatasources.write(classDataDatasources);
    }

    final pathDataRepositories = join(pathPage, 'data', 'repositories',
        '${pageName.snakeCase}_repository_impl.dart');
    if (exists(pathDataDatasources)) {
      String dataDataRepositories = readFile(pathDataRepositories);
      for (var element in api) {
        dataDataRepositories = dataDataRepositories.replaceAll(
          RegExp(
              '(import([\\s\\\'\\"\\.a-zA-Z\\/_]+)${element.toString().snakeCase}_(\\w+)\\.dart(\\\'|\\");)|((\\@override)(\\s+)([a-zA-Z\\<\\>\\s\\,]+)${element.toString().camelCase}([a-zA-Z\\<\\>\\s\\(\\)\\{\\}=\\.\\,\\:\\;]+);(\\s+)?}(\\s+)?})'),
          '',
        );
      }
      if (!RegExp(r'}(\s+)?}(\s+)?}').hasMatch(dataDataRepositories)) {
        dataDataRepositories += '}}';
      }
      pathDataRepositories.write(dataDataRepositories);
    }

    final pathDomainRepositories = join(pathPage, 'domain', 'repositories',
        '${pageName.snakeCase}_repository.dart');
    if (exists(pathDataDatasources)) {
      String classDomainRepositories = readFile(pathDomainRepositories);
      for (var element in api) {
        classDomainRepositories = classDomainRepositories.replaceAll(
          RegExp(
              '(import([\\s\\\'\\"\\.a-zA-Z\\/_]+)${element.toString().snakeCase}_(\\w+)\\.dart(\\\'|\\");)|(([a-zA-Z\\<\\>\\s\\,]+)${element.toString().camelCase}([a-zA-Z\\s\\(\\)]+);)'),
          '',
        );
      }
      pathDomainRepositories.write(classDomainRepositories);
    }

    final pathDataModels = join(pathPage, 'data', 'models');
    final pathDomainEntities = join(pathPage, 'domain', 'entities');
    final pathDomainUsecases = join(pathPage, 'domain', 'usecases');
    final pathPresentationBloc = join(pathPage, 'presentation', 'bloc');
    for (var element in api) {
      final body = join(pathDataModels, 'body',
          '${element.toString().snakeCase}_body.dart');
      final response = join(pathDataModels, 'response',
          '${element.toString().snakeCase}_response.dart');
      final entity = join(
          pathDomainEntities, '${element.toString().snakeCase}_entity.dart');
      final useCase = join(pathDomainUsecases,
          '${element.toString().snakeCase}_use_case.dart');
      final bloc = join(pathPresentationBloc, element.toString().snakeCase);
      removeFile(body);
      removeFile(response);
      removeFile(entity);
      removeFile(useCase);
      removeDir(bloc);
    }
  } else {
    removeDir(join(pathPage, 'data'));
    removeDir(join(pathPage, 'domain'));
    removeDir(join(pathPage, 'presentation', 'bloc'));
  }
}