makeProvider static method

Future<void> 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 Future<void> 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: () {
      final linkText = '${name.snakeCase}_provider';
      final link = MetroConsole.hyperlink(linkText, filePath);
      MetroConsole.writeInGreen('[Provider] $link 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 = \{([^}]*)\};',
      );
      final match = _getFirstRegexMatch(reg, file);
      if (match == null) return file;
      String template =
          """
final Map<Type, NyProvider> providers = {$match
$providerName: $providerName(),
};""";

      return file.replaceFirst(reg, template);
    },
  );
}