makePage static method

dynamic 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 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.replaceAll(RegExp(r'(_?page)'), "");

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

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

  await _makeDirectory(folderPath);
  await _checkIfFileExists(filePath, shouldForceCreate: forceCreate);
  await _createNewFile(filePath, value, onSuccess: () {
    MetroConsole.writeInGreen('[Page] ${name.snakeCase}_page 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 = ", authPage: true";
        }
        String strInitialPage = "";
        if (isInitialPage) {
          strInitialPage = ", initialRoute: true";
        }

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

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

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