makeEvent static method

dynamic makeEvent(
  1. String className,
  2. String value, {
  3. String folderPath = eventsFolder,
  4. bool forceCreate = false,
  5. bool addToConfig = true,
})

Creates a new Event.

Implementation

static makeEvent(String className, String value,
    {String folderPath = eventsFolder,
    bool forceCreate = false,
    bool addToConfig = true}) async {
  String name = className.replaceAll(RegExp(r'(_?event)'), "");

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

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

  String classImport = makeImportPathEvent(name.snakeCase);
  await MetroService.addToConfig(
      configName: "events",
      classImport: classImport,
      createTemplate: (file) {
        String eventName = "${name.pascalCase}Event";

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

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