makeProvider static method

dynamic makeProvider(
  1. String className,
  2. String value, {
  3. String folderPath = providerFolder,
  4. bool forceCreate = false,
  5. bool addToConfig = true,
  6. String? creationPath,
})

Creates a new Provider.

Implementation

static makeProvider(String className, String value,
    {String folderPath = providerFolder,
    bool forceCreate = false,
    bool addToConfig = true,
    String? creationPath}) async {
  String name = className.replaceAll(RegExp(r'(_?provider)'), "");

  String filePath = '$folderPath/${name.snakeCase}_provider.dart';

  await _makeDirectory(folderPath);
  await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
  await _createNewFile(filePath, value, onSuccess: () {
    MetroConsole.writeInGreen(
        '[Provider] ${name.snakeCase}_provider created 🎉');
  });

  if (addToConfig == false) return;

  String classImport = makeImportPathProviders(name.snakeCase,
      creationPath: creationPath ?? "");

  await MetroService.addToConfig(
      configName: "providers",
      classImport: classImport,
      createTemplate: (file) {
        String providerName = "${name.pascalCase}Provider";

        RegExp reg =
            RegExp(r'final Map<Type, NyProvider> providers = \{([^}]*)\};');
        String template = """
final Map<Type, NyProvider> providers = {${reg.allMatches(file).map((e) => e.group(1)).toList()[0]}
$providerName: $providerName(),
};""";

        return file.replaceFirst(
            RegExp(r'final Map<Type, NyProvider> providers = \{([^}]*)\};'),
            template);
      });
}