makePage static method

Future<void> makePage(
  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 Page.

Implementation

static Future<void> makePage(
  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'(_?page)'), "");

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

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

  await _makeDirectory(folderPath);
  await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
  await _createNewFile(
    filePath,
    value,
    onSuccess: () {
      final linkText = '${name.snakeCase}_page';
      final link = MetroConsole.hyperlink(linkText, filePath);
      MetroConsole.writeInGreen('[Page] $link created 🎉');
    },
  );

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

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

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

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

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

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