makeNavigationHub static method

dynamic makeNavigationHub(
  1. String className,
  2. String value, {
  3. String folderPath = pagesFolder,
  4. bool forceCreate = false,
  5. bool addToRoute = true,
  6. bool isInitialPage = false,
  7. bool isAuthPage = false,
  8. String? creationPath,
})

Creates a new Navigation Hub.

Implementation

static makeNavigationHub(String className, String value,
    {String folderPath = pagesFolder,
    bool forceCreate = false,
    bool addToRoute = true,
    bool isInitialPage = false,
    bool isAuthPage = false,
    String? creationPath}) async {
  String name =
      className.snakeCase.replaceAll(RegExp(r'(_?navigation_hub)'), "");

  // create missing directories in the project
  await createDirectoriesFromCreationPath(creationPath, folderPath);

  // create file path
  String filePath = createPathForDartFile(
      folderPath: folderPath,
      className: name,
      prefix: "navigation_hub",
      creationPath: creationPath);

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

  // add to router
  if (addToRoute == false) return;

  String classImport =
      "import '/resources/pages/${creationPath != null ? '$creationPath/' : ''}${name.snakeCase}_navigation_hub.dart';";

  await addToRouter(
      classImport: classImport,
      createTemplate: (file) {
        String strAuthPage = "";
        if (isAuthPage) {
          strAuthPage = ", authPage: true";
        }
        String strInitialPage = "";
        if (isInitialPage) {
          strInitialPage = ", initialRoute: true";
        }

        String routeName =
            'router.add(${name.pascalCase}NavigationHub.path$strAuthPage$strInitialPage);';
        if (file.contains(routeName)) {
          return "";
        }

        RegExp reg = RegExp(r'\}\);(?![\s\S]*\}\);)');

        return file.replaceFirst(reg, "  $routeName\n});");
      });
}