createLocator method
Implementation
void createLocator(
String pathPage,
String pageName,
String apiName,
) {
final apiClassName = apiName.pascalCase;
final path = pathPage;
DirectoryHelper.createDir(path);
if (!exists(join(path, 'locator.dart'))) {
join(path, 'locator.dart').write('''import 'package:core/core.dart';
import 'presentation/cubit/${pageName.snakeCase}_cubit.dart';
void setupLocator${pageName.pascalCase}() {
// *Cubit
locator..registerFactory(() => ${pageName.pascalCase}Cubit(),);
}''');
}
String data = File(join(path, 'locator.dart')).readAsStringSync();
final isDataDatasourceAlready =
RegExp(r'remote_data_source\.dart').hasMatch(data);
final isDataRepositoryAlready =
RegExp(r'repository_impl\.dart').hasMatch(data);
final isDomainRepositoryAlready =
RegExp(r'repository\.dart').hasMatch(data);
data = data.replaceAll(RegExp(r"import\s?'package:core\/core.dart';\n?\n?"),
'''import 'package:core/core.dart';
${!isDataDatasourceAlready ? '''import 'data/datasources/${pageName}_remote_data_source.dart';''' : ''}
${!isDataRepositoryAlready ? '''import 'data/repositories/${pageName}_repository_impl.dart';''' : ''}
${!isDomainRepositoryAlready ? '''import 'domain/repositories/${pageName}_repository.dart';''' : ''}
import 'domain/usecases/${apiName}_use_case.dart';
import 'presentation/bloc/$apiName/${apiName}_bloc.dart';''');
data = data.replaceAll(RegExp(r';?(\s+)?}', multiLine: true), '''
// *Bloc
..registerFactory(() => ${apiClassName}Bloc(useCase: locator()),)
// *Usecase
..registerLazySingleton(() => ${apiClassName}UseCase(repository: locator()),)
${!isDataDatasourceAlready ? '''
// *Repository
..registerLazySingleton<${pageName.pascalCase}Repository>(
() => ${pageName.pascalCase}RepositoryImpl(remoteDataSource: locator(),),
)''' : ''}
${!isDataRepositoryAlready && !isDomainRepositoryAlready ? '''
// *Datasource
..registerLazySingleton<${pageName.pascalCase}RemoteDataSource>(
() => ${pageName.pascalCase}RemoteDataSourceImpl(http: locator(),),
)''' : ''}
}''');
join(path, 'locator.dart').write(data);
}